Control Speed of DC Motor

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: Control Speed of DC Motor

Post by blmorris » Wed Aug 27, 2014 5:01 pm

For your small-motor application I could suggest either of the following circuits depending on whether you have any BJTs or MOSFETs laying around:
small-motor.png
Simple circuit to drive small motor
small-motor.png (11.34 KiB) Viewed 11562 times
In the BJT version the 1K resistor limits the base current to a reasonable level; in the MOSFET version the 10K resistor prevents any floating voltage from turning on the MOSFET before the PWM pin is initialized. The diode is there to prevent inductive/back-EMF currents from blowing up the transistor when it turns off, for a small enough motor it may not be necessary.

-Bryan

User avatar
UltraBob
Posts: 43
Joined: Mon Jul 28, 2014 1:18 pm
Location: Zushi, Japan
Contact:

Re: Control Speed of DC Motor

Post by UltraBob » Thu Aug 28, 2014 8:24 am

Dave:

I'm trying to control something with these exact motors in it:
Even small motors like these: http://www.pololu.com/product/114/specs have a stall current of 2.1A so you want your motor driver to be able to drive that much current. Something like http://www.pololu.com/product/1212 would work for those motors.
(Basically two of the single motor gearbox combos, rather than the package with two in the same case.)

Looking at the motor driver you suggested, I think you would actually have to go for something like http://www.pololu.com/product/1213 instead since you are driving two motors, but those motors are supposed to be driven at up to 3V. It looks like the VIN on that driver is 5V-28V, so would that actually work without putting more than the recommended voltage through the motor?

Is there a better way? Basically I would like to be able to control the speed of those two dc motors, forwards and backwards to power tank tracks.

Thanks as always for your advice, I think I'm finally going to have a little time to work on this again after a few weeks away from it.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Control Speed of DC Motor

Post by dhylands » Thu Aug 28, 2014 9:10 pm

UltraBob wrote:Dave:

I'm trying to control something with these exact motors in it:
Even small motors like these: http://www.pololu.com/product/114/specs have a stall current of 2.1A so you want your motor driver to be able to drive that much current. Something like http://www.pololu.com/product/1212 would work for those motors.
(Basically two of the single motor gearbox combos, rather than the package with two in the same case.)

Looking at the motor driver you suggested, I think you would actually have to go for something like http://www.pololu.com/product/1213 instead since you are driving two motors, but those motors are supposed to be driven at up to 3V. It looks like the VIN on that driver is 5V-28V, so would that actually work without putting more than the recommended voltage through the motor?

Is there a better way? Basically I would like to be able to control the speed of those two dc motors, forwards and backwards to power tank tracks.

Thanks as always for your advice, I think I'm finally going to have a little time to work on this again after a few weeks away from it.
Yeah using the dual motor controller makes more sense if you have 2 motors.

As for voltage, you can reduce the PWM percentage to effectively get a lower voltage.

The motors are inductors, and even though you're sending in a square wave, the currents take time to ebb and flow, so what the motor actually winds up seeing is more like a variable voltage.

So if you put 5V into your motor driver, and drive it at 50%, then what the motor sees is 2.5V
The 4QD website has lots of really useful background information if you want to understand more: http://www.4qdtec.com/pwm-01.html

Having said all that, I'd have absolutely no qualms overdriving those motors at 5V, especially if you keep your gearbox lubricated. I suspect you'll find that you don't need to use 100%.

User avatar
yllumi
Posts: 37
Joined: Tue Aug 19, 2014 8:41 am
Location: Bandung, West Java, Indonesia
Contact:

Re: Control Speed of DC Motor

Post by yllumi » Fri Aug 29, 2014 8:05 am

dhylands wrote:That's whats available for now.

I have something working that hasn't been merged yet. I just need to code up the teensy version to make sure it works on both:
https://github.com/micropython/micropython/pull/815
I've tried some of your example code, but it's not working with my pyboard. Is there something that I missing?

User avatar
yllumi
Posts: 37
Joined: Tue Aug 19, 2014 8:41 am
Location: Bandung, West Java, Indonesia
Contact:

Re: Control Speed of DC Motor

Post by yllumi » Fri Aug 29, 2014 8:08 am

blmorris wrote:For your small-motor application I could suggest either of the following circuits depending on whether you have any BJTs or MOSFETs laying around:
small-motor.png
In the BJT version the 1K resistor limits the base current to a reasonable level; in the MOSFET version the 10K resistor prevents any floating voltage from turning on the MOSFET before the PWM pin is initialized. The diode is there to prevent inductive/back-EMF currents from blowing up the transistor when it turns off, for a small enough motor it may not be necessary.

-Bryan
That's the simpler solution than using L293D. But for this time I still stuck making PWM output :D

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Control Speed of DC Motor

Post by dhylands » Fri Aug 29, 2014 2:31 pm

yllumi wrote:
dhylands wrote:That's whats available for now.

I have something working that hasn't been merged yet. I just need to code up the teensy version to make sure it works on both:
https://github.com/micropython/micropython/pull/815
I've tried some of your example code, but it's not working with my pyboard. Is there something that I missing?
The sample python code needs a firmware image built using the code on the PR to work (because the PR hasn't been merged into the main python codebase yet).

User avatar
yllumi
Posts: 37
Joined: Tue Aug 19, 2014 8:41 am
Location: Bandung, West Java, Indonesia
Contact:

Re: Control Speed of DC Motor

Post by yllumi » Sat Aug 30, 2014 10:47 am

I'm still stuck in generating PWM output. I have tried the sample code like in here http://wiki.micropython.org/contributedmodules/pwm but I got no output.
I have tried to check the signal using my multimeter but it is nothing changes. There is no voltage or current. I have updated the firmware to the latest and I am using pin X2. Any ideas?

PinkInk
Posts: 65
Joined: Tue Mar 11, 2014 3:42 pm

Re: Control Speed of DC Motor

Post by PinkInk » Sat Aug 30, 2014 3:58 pm

Yllumi, the pwm module I put up on the wiki worked when I last tried it, which was several weeks ago.

It's possible that micropython has changed in the interim in ways that preclude it working on more recent builds. In particular dhyland has been working on pyb.timer, I note heading in a direction which will make my temporary pwm hack obsolete.

I'll try and check the module, and fix it if possible, in the next day or so, with a current micropython build.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Control Speed of DC Motor

Post by dhylands » Sat Aug 30, 2014 4:50 pm

I just confirmed that it still works.

I downloaded the pwm.py from: http://wiki.micropython.org/contributedmodules/pwm

and if I do:

Code: Select all

Micro Python v1.3.1-14-gca6d75f on 2014-08-30; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> import pwm
>>> p=pwm.PWM(pyb.Pin.board.Y2)
>>> p.duty(128)
then I see a 50% duty cycle on pin Y2.

When you said you measured using your multimeter, were you using the ground pin next to X2? There is a row of 4 ground pins and 4 VIN pins right next to X1-4

PinkInk
Posts: 65
Joined: Tue Mar 11, 2014 3:42 pm

Re: Control Speed of DC Motor

Post by PinkInk » Sun Aug 31, 2014 2:58 am

Thanks! Didn't think what you'd added was likely to have broken it, and was planning to try to use some of the informational stuff you've added to timer to reduce size.

Post Reply