Saturday, November 22, 2008

Simple PWM DC motor control using MOSFET H-Bridge with AVR ATmega8


Hi friends,
here is a very simple project of controlling a small DC-motor (taken from an old personal cassette player) with ATmega8. The ATmega8 is having three PWM channels, out of which two are used here. PWM waveforms are fed to MOSFET (RFD3055) H-bridge.
Here, direction is controlled using a two-position toggle switch and speed of the motor is controlled by two push-buttons, one for increasing the speed and other for reducing.
The schematic is geiven here:
When switch SW1 is closed, OC1A channel is active which will feed the PWM signal to Q1 & Q4 MOSFETs. The OC1B pin will remain low keeping the Q3 & Q2 in OFF condition. When SW1 is toggled to open position, OC1A pin will become low, making Q1 & Q4 OFF and OC1B will feed the PWM signal to Q3 & Q2, resulting in the change in the direction of current flow hrough motor. Hence, motor rotation direction will change.
The speed is controlled by Push-buttons S2 & S3. Pressing S2 will increase the speed in fixed steps. Similarly, pressing S3 will reduce the speed in fixed steps.

The closer look to the motor and the circuit:
Here on the bread-board, I've used two push-buttons but the direction control switch is replaced by a small wire which was connected to ground or kept open for changing the direction (since I was not having the toggle switch in stock at the moment). The bread-board also includes 6-pin In-System Programming connector which is not shown in the schematic.

(The InSystemProgramming (ISP) connector is shown in my earlier posts, hence omitted here!! But, if you are new to ISP, refer to my post DIY AVR Programmers to make one yourself!!)

The schematic and code for this PWM motor control can be downloaded from here:

Download Schematic & Code
Download Hex file

Download datasheet: RFD3055

Thanks!
---------
ccd@dharmanitech.com

25 comments:

mhc said...

I'm looking for that kind of schematic since a couples of weeks, I was all excited until I saw " The bread-board also includes 6-pin In-System Programming connector which is not shown in the schematic."

I know nothing on programming chip and I don't even know how to wire that 6 pin in-system Programming on the breadboard... ah well.

CC Dharmani said...

Hi mhc,
sorry for that! But,I've not shown it in the schematic just because I've shown it in my earlier ATmega32 based schematics! It is same for ATmega8, too! If you wanna make a programmer yourself, its really easy, just refer to my post "AVR Programmers", you'll find plenty of links!
Check out here:
http://www.dharmanitech.com/2008/09/diy-avr-programmers.html

Anyways, thanks for pointing it out, I'll just put up a link in the post itself!!
Thank you!!

Nigel said...

This looks like the perfect controller for a project I have in mind. I wonder though - how big a motor can this drive? I would like to drive a 24V motor with up to 10A. Is it possible with this circuit and some modification to the H-bridge? Would you be able to suggest some components for this type of load? Thanks!

CC Dharmani said...

Hi Nigel,
the MOSFETs used here are capable of driving upto 11A load and upto 60v across. So, certainly you can use a 12v or 24v motor. Only the size of capacitor may change depending on load to avoid voltage dips taking place at the power supply line.

Thank you!
Regards.

mhc said...

Thanks for your reply, It helps a lot! And thanks for the schematic as well!

Cheers,
mhc

"The worlds forgotten boy." said...

I am looking to do something like this with 40amp motors. Any Idea how this could be adapted to work?

CC Dharmani said...

Yes, the circuit can be adapted with power MOSFETs, having 50A or more current capacity. You can not drive them directly from uC pins, so you have to provide a small driver circuit between MOSFETs and uC (mostly it includes a transistor and few resistors).

If you search on net, you'll find plenty of manufacturers for power MOSFETs. DigiKey sells wide range of them.

Regards.

Anonymous said...

How much testing has this design received? I am surprised that N-channel MOSFETs on the high-side of the H-bridge works. Remember that N-channel MOSFET gate drive is relative to source. So in this case, you should be driving the high side gates with Vcc+10V (or whatever gate drive value you want to use). That is what makes using N-channel MOSFETs difficult.

Also, the RDS3055 is a poor choice for your application. It barely turns on being directly driven by a microcontroller at probably 4.5v (there is some loss at the output pin from Vcc). Look at figure 7 in the RDS3055 datasheet and you will see that it barely handles 1 amp like that.

What makes building an H-bridge from N-channel MOSFETs challenging is the high side drive. Most designs ultimately use a charge pump to create the high-side gate voltage, but since its relative to Vcc, switching it to the MOSFET has its own challenges.

You could replace the high side N-channel MOSFETs with P-channel logic-level MOSFETs (so they turn on properly with your 4.5V drive) and replace the low side N-channel with a logic-level N-channel. That would create an H-bridge that is much more robust.

Below is a link that does a decent job of detailing the issues. Building H-bridges is fun, but challenging. Lots of stuff will work on the breadboard for a few minutes, but use it for anything real and it will fail quickly.

Good luck.

http://www.modularcircuits.com/h-bridge_secrets1.htm

CC Dharmani said...

Hi "anonymous",
thanks a lot for the comments and very informative link on H-bridge.
If you look at the code which I've given, I'm controlling the motor appx. from 3.5v to 5v as till that level I don't get response from the motor (MOSFETs don't allow enough voltage to drive motor below that voltage, as you correctly pointed out). And the motor which I'm using is a 3v personal stereo player motor, so it runs even if you supply voltages little below 3v.

Recently I've posted a new project of RC car using the same H-bridge, but there I'm using 9v across the MOSFET and driving gate terminal with same uCpins. It's working, you can have a look:
http://www.dharmanitech.com/2009/01/ir-remote-controlled-car-pwm-motor.html

Thanks & Regards.

Anonymous said...

The car demo is nice and clearly your design is functioning for your needs so that is great. However, I would urge caution when trying to scale it up for anything larger (ie. the person asking to use it for 10A+ loads).

Have you tried just turning the bridge full-on? Your code appears to always use PWM, even at full. I am very curious whether you will still get continuous movement, or if the motor will jerk and then stop, or if it will oscillate (though it may be difficult to tell).

Also, how does the speed of the car at "full on" in your current design compare with connecting directly to the 9v source? In a properly functioning MOSFET H-Bridge, there should be almost no difference.

My theory as to how this is working (and the limitations): for the high-side MOSFETs, assume S=source, D=drain, G=gate

When the high-side is turned off, the voltage at S is indeed zero. Thus, 4.5v to G will exceed that and the device will start conducting (within the limits of the low-gate voltage). However, once it starts conducting, the voltage at S will rise bringing it closer to G and it will stop conducting starting the cycle over again.

So in summary, I think the low-side comes on partially (limited by the 4.5v drive) and the high-side is oscillating in an off to partial-on state and all this within your PWM cycles. So while it works, I am guessing it may be quite inefficient (only providing partial power and converting a fair bit to heat).

Keep up the experiments and designs-- they are great! If you are not there, there is a forum at sparkfun.com with lots of electronics enthusiasts that might be a good resource as well.

CC Dharmani said...

Hi 'anonymous',
thanks a lot for your comments, I appreciate your suggestions.
Yup, you're right there, with this design, I'm not transferring full battery power to the motor, it just satisfies the current project needs.
Main reason for using this H-bridge is the easy availability of the N-channel MOSFETs! I was already having them handy!!

Regards.

keen101 said...

Cool. I'm working on a similar project that will involve an ATMEGA8 as the brain of the speed controller. I'm a bit confused by your schematic though. IT seems that you are driving the gates open directly from the microprocessor, but one would think you would need a mosfet driver with a voltage multiplier?

CC Dharmani said...

Hi keen101,
you can go thru the earlier comments, you'll find it interesting. As 'anonymous' has commented, this is not an efficient way of doing this, driving the gate of the upper p-channel mosfets at such low voltage.
The circuit works, as I've shown in the small video of the next post on ,IR remote controlled car' which uses the same mosfet bridge. Though, if you want more efficient design, you can have the upper two mosfets replaced with p-channel mosfets. Or use motor driver ic like L293.

Anonymous said...

are you sure RFD3055 does not need mosfet gate driver?

CC Dharmani said...

It does not need and can be used as shown in the schematic, as long as you don't need high efficiency!

Anonymous said...

hi,very informative blog, must commend you for it. i'm doing my final year project tagged "multi-fan control panel with bluetooth remote control". i was trying to obtain variable speed control of 6 ac fans ( 3 speeds) using the the ATmega 8051. i encountered lots of problems majorly, a bulky cct. i had to make use of 18 relays, a very large tap transfomer close to 700w, running out of pins on the microcontroller, and finally moving 6 heavy fans from point to point.
i've decided to use 12v dc fans, varying speed by pwm but i cant find a microcontroller with up to 6 pwm output ports. please give me suggestions on either of the two cases or both thank you

CC Dharmani said...

If you want a small microcontroller, use 8-pin devices atmega48, atmega88 ot atmega168 with flash of 4k, 8k & 16k respectively.
If you want micro with more ports, use 64-pin atmega128 with 128k flash.
All these micros are having 6 PWM channels. There are some more in AVR family in various pin counts. Check out on atmel website:
http://www.atmel.com/dyn/products/devices.asp?family_id=607#760

Anonymous said...

thanks alot, that was very helpful.

CC Dharmani said...

hey, one correction, those first three micros i mentioned (mega48/88/168) are 28-pin devices, not 8-pin, typing error! :)

Mustafiz said...

That was a great help...THanks so much man keep up the best works...wish you all the best

Anonymous said...

Nice! Just a suggestion if you want to drive a 5V bridge. Provided the motor is small like drawing below 2A average, you can use two TC4421 driver ICs with no mosfets at all (ICs should be free samples from Microcip). For bigger motors and 5 to 80V there is either HIP4080 or HIP4081 from digikey but they have a price tag ;)

Anonymous said...

... ups TC4421 and a bridge _higher_ than 5V! :) and with higher efficiency and battery life than the 4-mosfet setup, too

CC Dharmani said...

Thanks for the suggestion, man! I'll have have a look at the datasheets of those ICs.

hahahaha said...

sir how to build the pwm dc motor using 8051 microcontroller

vidyasagar said...

sir how to build the pwm dc motor using 8051 microcontroller