Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Gear motor speed control with a PIC24

Status
Not open for further replies.

Shipton

New Member
Hey there

So I'm making a robot that speed matches/avoids collision. It's using an ultrasonic sensor with PWM to detect things in front of it. This is being fed into a PIC24HJ32GP202.

I have two gear motors running on an L293D H-bridge. I was wondering how I would implement some kind of variable speed control? I was thinking, perhaps, of some form of variable voltage control so I can adjust the amount of voltage going into the H-bridge supply; that would supply some rudimentary form of speed control.

Because they're gear motors, PWM wouldn't really work since they don't have enough inertia to overcome the stopping power of the gears.

Any ideas?
 
Oh really? Excellent then. Do you know of anywhere I can read up on using it, especially re: how to determine pulse widths etc?
 
Oh really? Excellent then. Do you know of anywhere I can read up on using it, especially re: how to determine pulse widths etc?

When I wrote code to do this I googled to find out what frequency (pusle width) to use. The reality is that a wide range will work but some are more efficient then others. What is best depends on your system.

Here is one web page on the subject
 
Unless I missunderstood what you wrote, You can control the H bridge with an analog/variable voltage but you lose part of the advantage of the H-bridge. By driving the bridge with a pulse, the bridge is very efficient and it can control a large motor( high current) without the need for a large heat sink. Pulse Width Modulation (PWM) Switches on and off quickly so the MOSFET doesn't have to dissipate as much power. example: Using an analog voltage in a 12 volt system running the motor at half speed, you will have 6 volts across your MOSFET. Power is I*E. Using PWM, the MOSFET is turned on 1/2 of the time. WHen it's on only a small amount of voltage is across your MOSFET. Another advantage of the PWM is torque. You will have better torque at half speed, because you're slamming the motor with the full voltage and current. Also, if you use an analog voltage to control the bridge, you need to have a feedback loop to control the voltage out of the bridge. You add a lot of complexity, lose a lot of power, increase the size (and cost of you MOSFETs, heatsinks, cooling, etc.). You need a Hi side Lo side driver to drive the N channel MOSFETS. P channels are more expensive and they don't have as low of a RDSon. A MOSFET with and RDSon of .005 ohms ( 5 milliohms) will have .5v across it at 100 amps. ( E=I*R) 100amps * .005= .5 volts Power = I*E = 100*.5 = 5 watts. If this is a 12 volt system, you're controlling 100*12 =1200 watts of power. Your efficiency is 99%. Yes, you are going to lose more power than that due to switching losses. Rload = 100 ohm /12 volts= .12 ohms So, in your analog system, at half voltage out I =12/.12 = but looking at your 12 volt system, 1/2v out would give you the following 6v/.12ohms= 50 amps power wasted across the MOSFET is I*E= 50amps*6volts= 300Watts. That's a lot of heat and if you're running on batteries, you are really wasting a lot of run time.
 
Erm, all the above post really accomplished was confuse the hell out of me. I appreciate it, grassm, but, missed the point... I think. Maybe?
 
You can either switch the PMOS or the NMOS. It is unnecessary to switch both, and doing so increases switching losses.

The PWM freq has a practical upper limit created by switching losses in the transistors. IIRC there's increases in core losses and core heating at higher freq too.
The PWM freq has a practical LOWER limit based on what produces excessive noise and vibration.

IIRC something around 5KHz -20KHz is common. It will produce audible noise in the motor, but taking the freq above audible range is often too high.

It's a GOOD idea to use a motor tach. In general, a DC motor produces a fixed no-load speed at a fixed PWM duty and voltage. But, at slower speeds, the motor produces less torque and is more easily slowed. With 2 separate wheel motors, even slight differences in drag can create a difference in wheel speeds and make it go in circles.
 
Last edited:
Wow, this got really complicated really quickly.

You guys need to keep in mind you're talking to a newbie to the whole uC robot thing. This is a university project.

I'm using a PIC24HJ driving an L293D H-bridge, attached to two 3.6-4.5V gear motors.

As for switching PMOS and NMOS, analog feedback loops, switching losses and all that, that's way over my head. I just want to know how to control the speed of the motors by pulsing the LO signal to turn them on via the L293D.

If all this is really necessary, it seems I'll have to find another way.
 
Well the L293D is a 4.5v-to-7v device, the motor drive is a separate voltage. Your voltage may be too low for the chip. The package does not have a lot of dissipation and can't drive a lot of current here.

But the H-bridge itself is no sweat. With a 4.5v Vdd1, it'll accept 3.3v logic high according to the spec sheet.
You'd set one output as the high side, another as the low side, and connect the motor as shown. Try 10KHz PWM speed. The connection type isn't really an H-bridge per se... but basically you're gonna use one pin as the direction and one pin as the PWM. Now note this: when the direction pin makes the output low, the motor speeds up as duty cycle increases. When the dir pin makes that output high, it's the other way around, 0 duty cycle is fastest and when duty is 100% the motor's stopped since both pins are high.

Unloaded motor speed is basically proportional to PWM duty.
 
Last edited:
I'm really rather confused what's going on here. The L293D is an H bridge, complete with clamping diodes. It's working fine. The motors have a suggested voltage 3.6-4.5V, but I'm currently running them with a 9V supply. I've tested them with 5V on the L293D and all of these configurations work. I'm using 5V to the logic comparison pin(s) of the 293. I have tested this all with a basic line-following algorithm with the PIC24, and everything works.

I do not want to change my setup, and I want to continue using the L293D.

So I'm confused as to where the rest of this discussion is directed. I'm merely asking how to implement PWM with the L293D, or specifically, if that is possible. I was hoping all I would have to do is pulse the HI/LO outputs from the pins on the PIC24 to the appropriate pins on the L293D.

As for "switching the PMOS or the NMOS," I have no idea what P/NMOS this is even referring to
 
Last edited:
So I'm confused as to where the rest of this discussion is directed. I'm merely asking how to implement PWM with the L293D, or specifically, if that is possible. I was hoping all I would have to do is pulse the HI/LO outputs from the pins on the PIC24 to the appropriate pins on the L293D.
That's all you need to do. What are you asking for? I'm assuming you're not asking someone to write the code for you. The motor connection is described in the L293D spec sheet.
PIC24HJ32GP202 is a 3.3v part. 3.3v as the "high" looks sufficient for the L293D according to spec sheet.

Be aware that a 9v batt doesn't have a lot of capacity, and it can't drive nearly as high a current as even AAA cells.
 
Oh, no, I don't want anyone to write the code for me. I was just wondering if PWM can be accomplished with the L293D. I imagine there's some upper and lower limit to the frequencies though, i.e., when the pulses change too slowly so the motor motion becomes choppy, and when they're too fast, so the motors don't turn on.

I wasn't sure if it really was as simple as pulsing the inputs.

Yeah I noticed how quickly my 9V got drained - I imagine the persistent switching doesn't help. Is there an alternative power source that'd be better? Say a lithium battery?

PS> Nice quote. Are you quoting Salinger or the Laughing Man?
 
Last edited:
The switching is not the issue. A 9v battery is only about 500mAH, even a high quality one. And realistically, it probably can't drive out more than 100mA before the voltage starts dropping a lot.

Motors are high drain items. Even small ones. While it's not possible to guess your current from here, I'd guess it's probably more than 1W. You need a bigger batt, not more voltage but more AH and thus more ability to prove amps.
 
G'day Shipton,
The SN754410NE is a modern replacement chip for the L293D and can supply twice the current like 1 amp instead of the L293D which can only do 500mA. In the datasheet is a 2 phase application schematic that will fit perfect for what you want to do. Just hook control A to one motor control B to the other motor, I'm sure that 24FwhateverallthosenumbersmeanandI'llhavefrieswiththat should have several PWM outputs so basically by having the 2 PWM outputs equal the motor will go straight, alter either output and the robot will turn. Instead of hooking the enable pins to Vcc hook them to an output pin for braking you may need to use a transistor to raise the voltage higher than 3.3 volts but check the datasheet.

Cheers Bryan
 
So what would be a good power source to replace a 9V battery with? I'm guessing some sort of Lithium power source?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top