Re: Motor drivers and arduino
Posted: 13 May 2021, 11:55
Hi Max
I've used quite a few TB6612FNG and I was initially the same as you, I assumed that when using PWM you needed to freewheel in the "off" state, but having read the datasheet for the TI DRV8838 (where there is no freewheel, only "brake") it explains why, so as long as you are using a PWM frequency above a few hundred Hz, then braking is correct. If you are trying to "unstick" a small dc motor, like on a model train, with a pwm frequency of say 75Hz, then freewheeling may be the better option.
And I don't use the PWM inputs on the TB6612FNG, just the normal AIN1/AIN2 and BIN1/BIN2 inputs to save Arduino outputs (I just leave PWMA/PWMB unconnected), why use 6 connections when 4 will do? The Toshiba datasheet recommends this method if you can generate PWM for both AIN1/AIN2 and BIN1/BIN2, which an Arduino can do, but some older microcontrollers can't. The pwm inputs are also used if you have the pwm generated by external circuitry, not the microcontroller. So I don't think you will have to change your code at all, just ensure when reversing or stopped, take both AIN1/AIN2 HIGH (or BIN1/BIN2).
I've not used any other protective circuitry on any of Tobe's Rand LR2 and HR1 galloping ghost actuators that I use and I've had no problems. The TB6612FNG is rated to 15V max, so you are OK directly on a 12V lead-acid battery, I notice that the L9110 is only rated to 12V max. A fully charged lead-acid battery goes to 13.6V, I wonder if this could be some of your problem with the L9110 getting damaged? I see on some sellers websites they recommend 9V maximum and 12V not to be exceeded for the L9110.
I've only spent 5 minutes looking at your Arduino sketch and while neatly written, it will run relatively slowly especially if you are running the serial debugging code. In my experience using the serial port at 9600 baud, is virtually useless for debugging when doing accurate timings with ppm pulses as it adds such a delay to the code - each character takes just over 1mS to transmit, all the time stopping anything else running. In fact, nearly all of the functions you've used in your code are "blocking" which really slows things down. What do I mean by blocking? Well, code in the loop() runs one after the other to the bottom and then repeat, but things like the pulseIn() wait for the ppm pulse to arrive and program execution only start again once the ppm pulse finishes. If you call pulseIn() while the ppm pulse is high, it has to wait for a complete 20mS for the start of the next ppm pulse to start timing. I know an extra 20mS doesn't sound much, but it all adds up. When I first started programming Arduino, I never found it very accurate as any interrupts in the background will stop it counting (such as the millis(), micros(), analogWrite() etc) and used other timing methods. For an idea, look at Phil_G or my galloping ghost recoder sketches, we use an interrupt based method. viewtopic.php?f=86&t=1102
Also, I know analogWrite() is easy to use, but it limits the pwm frequency to 490Hz (if I remember correctly), if you learnt how to use the TCNT0/1/2 you can choose pwm frequencies in the KHz, I use above 20KHz pmw on most motors so that any noise/sound the motor makes is above the frequency we can hear. There are many tutorials on the web/YouTube, so there is little point in repeating it here.
Cheers
Mike
I've used quite a few TB6612FNG and I was initially the same as you, I assumed that when using PWM you needed to freewheel in the "off" state, but having read the datasheet for the TI DRV8838 (where there is no freewheel, only "brake") it explains why, so as long as you are using a PWM frequency above a few hundred Hz, then braking is correct. If you are trying to "unstick" a small dc motor, like on a model train, with a pwm frequency of say 75Hz, then freewheeling may be the better option.
And I don't use the PWM inputs on the TB6612FNG, just the normal AIN1/AIN2 and BIN1/BIN2 inputs to save Arduino outputs (I just leave PWMA/PWMB unconnected), why use 6 connections when 4 will do? The Toshiba datasheet recommends this method if you can generate PWM for both AIN1/AIN2 and BIN1/BIN2, which an Arduino can do, but some older microcontrollers can't. The pwm inputs are also used if you have the pwm generated by external circuitry, not the microcontroller. So I don't think you will have to change your code at all, just ensure when reversing or stopped, take both AIN1/AIN2 HIGH (or BIN1/BIN2).
I've not used any other protective circuitry on any of Tobe's Rand LR2 and HR1 galloping ghost actuators that I use and I've had no problems. The TB6612FNG is rated to 15V max, so you are OK directly on a 12V lead-acid battery, I notice that the L9110 is only rated to 12V max. A fully charged lead-acid battery goes to 13.6V, I wonder if this could be some of your problem with the L9110 getting damaged? I see on some sellers websites they recommend 9V maximum and 12V not to be exceeded for the L9110.
I've only spent 5 minutes looking at your Arduino sketch and while neatly written, it will run relatively slowly especially if you are running the serial debugging code. In my experience using the serial port at 9600 baud, is virtually useless for debugging when doing accurate timings with ppm pulses as it adds such a delay to the code - each character takes just over 1mS to transmit, all the time stopping anything else running. In fact, nearly all of the functions you've used in your code are "blocking" which really slows things down. What do I mean by blocking? Well, code in the loop() runs one after the other to the bottom and then repeat, but things like the pulseIn() wait for the ppm pulse to arrive and program execution only start again once the ppm pulse finishes. If you call pulseIn() while the ppm pulse is high, it has to wait for a complete 20mS for the start of the next ppm pulse to start timing. I know an extra 20mS doesn't sound much, but it all adds up. When I first started programming Arduino, I never found it very accurate as any interrupts in the background will stop it counting (such as the millis(), micros(), analogWrite() etc) and used other timing methods. For an idea, look at Phil_G or my galloping ghost recoder sketches, we use an interrupt based method. viewtopic.php?f=86&t=1102
Also, I know analogWrite() is easy to use, but it limits the pwm frequency to 490Hz (if I remember correctly), if you learnt how to use the TCNT0/1/2 you can choose pwm frequencies in the KHz, I use above 20KHz pmw on most motors so that any noise/sound the motor makes is above the frequency we can hear. There are many tutorials on the web/YouTube, so there is little point in repeating it here.
Cheers
Mike