Sunday, November 23, 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 (click on the image to enlarge):

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:

Schematic & Code on Google Docs
Source code in CodeVision format (C file)

Download datasheet: RFD3055



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:

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!

mhc said...

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


"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.


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.


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:

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!!


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:

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

Anonymous said...

sir plaese can u tell how to code avr (ATmega 32 )using motor control by PWM.

Siddartha-INFINITUM. said...

I am using a ATmega8 microcontroller, L293 H-Bridge IC, and two DC motors of 12 volt.
Can you please tell me the pic connections between the L293 and ATmega8 ..??
I ams stuck in it..
Will the schematic given by give some hint for my query??
Much thanks.

sogol said...

thank u for the circuit .
i have aporoject controlling gearbox of the car with mikrocontroller avr and in must be sencetive velocity of the motor and slope i want control my velocity of the motor with pwm or like this poroject with mikro but i want some span for my gear can i use it hear?how change the shematic if i use as 4 switch or five for forward gear and 1 for reverse?how is the connection of it in mikro?
thank u if u answer me

al said...

please give me source code...how to generate clock internal from atmega8.because are you schematic not found external crystal....thanks...dharmani are you play again robocon 2010,this theme was upload,your search and upload in your blogger

CC Dharmani said...

Hi al,
the source code can be downloaded along wiht the schematic from the link in the post itself (check at the end of the post).
For using internal 1MHz clock in AVR, you need not do anything, as the controller by default comes with internal clock enabled. You need to change the fuse bits if you want internal 2MHz, 4MHz, 8MHz or external crystal. Refer to 'Memory Programming' section of the AVR datasheet for details of fuse bits.

rahul said...

Hi mhc,
I like to make your Small Dc Motor Control by PWM Method Using MOSFET H-Bridge with Atmega8 Microcontroller project but here hex code of this project is not given,please if you have then send to me my Email id rally.555@gmail.com

Sunil said...

i want to use L293d instead of MOSFET wht will neccessary requirement and considerations?

wise said...

its nice project, i tried to make it. can u send me a hex file? because i tried to write down the code but it didnt work it out. thanks

Debasish said...

Sir I m working on a project "DC MOTOR SPEED CONTROL USING MICROCONTROLLER" n this looks like the perfect thing which I wanted.
But I have a couple of queries regarding this.
1) Can this ckt drive TWO dc motors of 12 V rating each?

2) I m having difficulty in finding the AVR burner. So can u plzzz suggest a burner ckt for the AVRmega8 chip used here??

Please answer my queries as soon as possible so that i can start working on it.
You can mail me at crackerjack.dev1988@gmail.com

Thanks in advance.

Anonymous said...

Hello all,
I am woking on a project which is PID control of a DC motor using HCS12 microcontroller using PWM and also I am using a L293D half bridge.

can you please give me the code for this so that i can submit my project on 7th of dec 2009.
I appreciate your help.
Thank you in advance.

chinni said...

sir here is my email-id

chinni said...

Hello sir,

could you please send me the code for DC motor PID control using PWM on HCS12 microcontroller and also am using L293D half bridge.
This is my senior thesis project and need to submit on 7 dec 2009.
I appreciate your help.
thanks again in advance.

Anonymous said...

You have used ICCAVR - compiler. Do you have some reason for that? I refer to use AVR studio with AVR GCC but it's not possible without errors. Reason for this is that ICCAVR is not free.

Anonymous said...

hi am working with an atmega 8...i have to control two dc motors using joystick...if u can help plz mail me on nonumanish@hotmail.com

salil said...

i need to control 2 motors using a joy stick and i have 3 inputs namely x axis and y axis and speed control and i am using 2 out put pwm channels

bahubali said...

sir am making a flying robot based on micro controller. so i need to control the dc motor speed. so is it possible to control 4 dc motors through micro controller?if yes please give ur cell number i ll contact you..

Abhishek said...

Iam making a project on dc motor speed control using bluetooth...
Can you please provide me the source code.
It will be quite helpful.

ashok said...

sir can i get the code in masm ? please reply for this.

CC Dharmani said...

Sorry, I don't have code in asm.

sanjeev said...

sir, i m glad to see tht u hav made such a good ckt. and u hav also provided so much information which could be very useful. but sir i wanna help from u. i m making a project which is the industrial fault monitoring system in which i m protecting the dc motor using over voltage , overcurrent and also by power failure relay. i m using ATMEGA 8535(quad configuration). sir will u please send me the whole ckt. diagram as well as the programming. i will be highly obliged to u.

Ekhtiar said...

hey sir,
i am trying to build the mosfet h -bridge project you have done. but i want to control a 12 v motor so i have to use some sort of driver right? what about uln2003? can you please enlighten me if i am right? i think i should just use uln2003 in between microcontroller and mosfets. one thing i am confused about uln2003 that it doesnt have any biasing port. so dont i need to supply it with any power?

and secondly i wanted to ask, when i try to run your C program in avrstudio4 it says i dont have imov8.h and macros.h file so it gives me about 33 errors? any suggestion how i get that to work? many thanks in advance.

Ekhtiar said...
This comment has been removed by the author.
Anonymous said...


I'm getting below three errors while building this project

1) error: 'CLI' was not declared in this scope
2) error: 'SEI' was not declared in this scope
3) error: '::main' must return 'int'

I'm using AVR GCC with AVR Studio


CC Dharmani said...

Hi Nilesh,
replace CLI & SEI with cli() & sei() respectivey. Declare int main() in place of void main() and at the end of main function, put return 0;

for cli & sei, you need to include the header file avr/interrupt.h

NVaghasana said...

Hello Sir,

Thank you.

I have successfully build the project now.

It will work if I place pot in place of switches?

Is it possible to drive multiple DC encoder motors with the code? or any reference link. I want to drive 5 motors with joystick.


Student said...

THANK YOU for your program, but I have trouble with the header: macros iom8v I did not find in avr
can you make it online for download

addy1985 said...

I really liked your project i'm working on it.I'm unable to find atmeg8, but i've got atmeg8L which can take voltage of 3.8 volts.What modifications do I need to make,like putting up a voltage regulator in the circuit.
Could you also help me with the Hex file.I need the .HEX file in order to burn the program.Can you please provide me with that to.
I realy appreciate the help provided.Thank you.

addy1985 said...

I really liked your project i'm working on it.I'm unable to find atmeg8, but i've got atmeg8L which can take voltage of 3.8 volts.What modifications do I need to make,like putting up a voltage regulator in the circuit.
Could you also help me with the Hex file.I need the .HEX file in order to burn the program.Can you please provide me with that to.
I realy appreciate the help provided.Thank you.

Anonymous said...

The use of an old(?) tape recorder motor may mean there is an motorinternal speed control who may interfer with your or anybody elses designs.
Just mentioning ;-)

About Power MOSFETs and driver circuitry I guess there is no need in low switch speed apps right?
I mean, isn't the perfectly insulated gate consuming any power at all?
I guess I missed something?
/ Roger

CC Dharmani said...

email id??

Here the in-built motor controller of the tape-recorder was disconnected, only the motor was used, so no interference.
Didn't get the second part of the question. The MOSFET h-bridge is required for direction control and also for providing higher current to the motor, which a microcontroller can't do directly. The gate is insulated, but the power consumption in mosfet is still there as high current flows from drain to source terminals.

addy1985 said...

Email ID:adnan_sh85@hotmail.com

Thanks alot

Anonymous said...

hello sir,
I try to do something like that but this time with atmega8435.
I copied the code but I happen not turn the engine over a 4 V change I made in the program that fits with my microcontroller atmega8535 arrived yet and I do not know how I did so you could help me

billhacEV said...

I think this is a great circuit, im just learning avr so I would like to know if there is a way to change the speed control from the buttons to a 5k pot, and would i have to change the programming code?

Levy said...

Dear Sir,

I'm a beginner in programming microcontrollers. I made your motorcontrol design, but I met a few problems. After programming the microcontroller but i cannot change the direction of the motor, and it rotates only with a constant speed in the same direction even if I switch the switch. And I cannot change the speed of it.

Thanks for your reply.

Nate said...

One other thing you should do is pull up all of your switches with a 10k resistor tied to the 5V line. By leaving them in a floating state when they are off you can sometimes get an erroneous signal.

CC Dharmani said...

Hi Nate, the AVR has got in-built pull-up resistors for every port pin. They are enabled here in the program for the pins where switches are connected.

Mostafa Ghadamyari said...


Thank you for your project !
I am going to build the project myself.
Now, I am simulation the project on Proteus.

Two questesions :
The problem is here that RFD3055 is not available in Proteus library.

I am newbie to mosfets, Please can you help me to choose another mosfet models ? Just the voltage, amper of mosfets should be the same ?
I tried the 'IRHE57034' and also 'IRF6691' in circuit, but the motor is turning very slowly !

I think IC1 in picture has been used to supply the ATMEGA 5v power input, can I use a regulator (12v to 5v) instead of IC1 circuit ? Please suggest a model if possible !

Thank you
Best regards

Mostafa Ghadamyari said...


I'm Mostafa, Who posted the last comment.
I have finally built the circuit myself ! See it :

First I bought a P-Channel MOSFET (IRF9541) , then I found that it is impossible to use the P-Channel mosfets for the purpose, so I have lost 2$ , then I bought 4 new IRFZ44N Mosfet.
Everythings are good !

I have a question :
When I connect the motor to dc source , It turns very high ! But, its most power when it is on the H-Bridge is about 1/5 of that.
Should I try to increase the (248) which you have used in your program as maximum_counter_limit ?


CC Dharmani said...

Hi Mostafa,

ideally, the H-bridge is supposed to have upper mosfets as pmos, for higher efficiency. An H-bridge with all mosfets as nmos will need another bootstrap bias arrangement which feeds supply between upper and lower mosfets. You can refer to the datasheet of HIP4081, which is a motor driver and drives an H-bridge with all four mosfets as nmos.

If we don't add such extra circuit, we have to deal with loss of efficiency.

For better results, you can use readymade H-bridge ICs like L293, L298, which are easily available. The programming will be almost same as given here, but efficiency will be far better and also it'll reduce the wiring.

ece mini projects said...

Excellent project, saved me on time, thanks for sharing, keep going....

dineshji said...

plz write the equation that relates PWM time period and motor specification to calculate the corresponding speed.

Shagufta said...

Im building a PWM motor control unit with H- Bridge using ATMEGA32. I saw your programming and have got some doubt. It would be greatful if you clear it.
1.What is the need for assigning the values to TCNT1, ICR1, OCR1 and how do you specify the value.
I need to generate a pwm of 20 khz and 5volts, So what values should I substitute in TCNT1, ICR1, OCR1. Please help.

ECEGroup17 said...

Hi there,
This is great! it's exactly what I was looking for. Just a quick question, in your code, the header files that you have included in the source code are not present in the download link, just wondering if those header files are ones you have created personally or did you use built in ones.
I am getting several undefined variable errors and I think it is because of the declaration of variables within your header files that you have included.
If possible would you be able to put up those .h files as well?

ECE group 17

ThE power IS uLtIMaTe said...

ummm guys i dnt knw anything abt all these stuff but damn intrstd in these sot of things so can any one give a simple project which i would to strt plzzzz guys help me

Mounica said...

this project is good
but can i do the same using PIC micro controller.
if it can be used then what changes i have to make in this with respect to devices ,
(i.e) is it possible to just replace the existing Micro controller with PIC and
give me your suggestions as i have to complete it in 3 weeks.

Mounica said...

can i use potentiometer to rate the speed and a hall effect device to sense the speed from potentiometer instead of s2 & s3 speed increase and decrease buttons.
in this project what are the c1 & c2,
or any other suggestions
reply ASAP

gyt1s said...

hello. could you send the .hex file


Anonymous said...

please include a project on stepper motors

Suyash said...

can you send me the .hex file by posting it as a comment on my website..www.imsuyash.in

maxim said...

dear sir,
Its an Amazing thread I have never seen.very thanks.Sir if I use a 3phase motor what changes should
the program mode?I know the hardware
circuits,I need connect a sensor for
the motor start and stop


yash said...

please sir requesting you can provide the pcb layout or pcb diagram of the circuit please sir waiting for answer.requesting.

Anonymous said...

hello can you please let me know what is the c program that is been used in this project also can i use the same controller to control two motors
please reply me at

Anonymous said...

hello CC Dharmani sir,
sir can i have your email id to mail you some of my work? i really need your help in my project of pwm dc motor control using atmega8. kindly tell me your email id so i can send the details and documents. waiting for your kind response...
Hammad Amjad

Fetti said...

nice project. Would you mind to send me a hex-file, because compiling doesn´t work for me. Greetz.
C (c.hartmann@mail.com)

Anonymous said...

Put the resistors 1k between gate mosfet to gnd, and where is the diode to protect pin avr's??