BLOG > Tutorial 5 for Arduino: Motors and Transistors

This tutorial was featured on the official Arduino blog on 3/9/2011

This week, we’ll finally be using the Arduino to control some motors!  First up, we’ll control a standard DC motor running off a 9V battery with the help of an NPN transistor and a PWM (Pulse Width Modulated) signal from the Arduino.  Next, it’s onto 180 degree servo motors, which we will power on an independent 5V rail using a voltage regulator.  We’ll control the servo using a PWM signal from the arduino, and I’ll show you how you can turn analog distance readings into precise angles for the servo to turn to!

I mention in the video that you can control DC motor direction using an H-Bridge.  If you want to wire that up, take a look through this Wikipedia article, and read through this tutorial.

If you want to learn more about analog input filtering in software, check out this example code.

The Arduino Contest has been extended!  So don’t forget to submit your project.


You can download the files associated with this episode here:

GNU GPL License Distributed under the GNU General Public (Open-Source) License.
Please Attribute and Share-Alike.


301 comments

  1. hi, i saw ur video this is really hot stuff. i am new user of arduino. plz tell me how i program three servo. can i directly connect servos to arduino board to operate it. Is voltage regulator and capacitor required for it as u mention it in ur MOTOR tutorials-5, if it is, then what’s the value of regulator and capacitors. also provide information (with diagram) how to connect three servos to arduino uno.
    there is one question, i want to use two geared motor to arduino. how can i control the speed of motors individually. plz give the design of it with parts details.
    if u provide all above information with tutorials, it’s my pleasure.
    i am very thankful to you.

    1. I can’t draw out full schematics for every request. Hook up three servos using a 5V regulator to power all of them via 9V battery as I showed in the video. Then just hook their control wires right up to PWM pins on the arduino, and use the code I demonstrated.

  2. Hi, Jeremy
    I’m so happy to find your site, today I saw all your videos, I learn a lot. Thank’s to share your tutorials. Everything was very clear to new people like me. I’m interesting in learn all about arduino.
    Arduino site has a analog write with 12 LEDs example. This example fades 12 LEDs up and the down, one by one, on an Arduino Mega board.
    I want to light a walkway with this example. My questions: I know the operating voltage for all arduino boars are 5V. Instead of this 12 leds HOW I CAN conect a 12 car led strip (12V- 24 leds each strip) ? Can I use another arduino board to make my proyect ?
    Thank you again!!

  3. i ordered the IR sensor you used in this video, however it did not come with the cable. Which cable is needed?

  4. My motor seems not to be responding in the expected fashion and I cant see why, the problem is that it barely spins up and doesnt seem to increment or stop at all. When i place an led in the position of the motor it steps up and down, I have also tested the motor and this also appears to be fine. Any ideas?

    Thanks for your time time (again, promise I wont make this too regular) ;)

  5. Jeremy: your video tutorials for the Arduino are fantastic. Crisp and to the point but very thorough. You are a great teacher. Thank you for sharing your expertise :-).

  6. Hi Jeremy,

    If i wanted to add an LED to the circuit that uses the same pwm data as the motor how might I do that? Ive tried placing one between the pin and the transistor but this stops the transistor working.

  7. Sorry it doesnt stop the transistor working but doenst light up. If I remove the connection to the transistor however the LED works.

  8. Ha whilst I know what that means in theory im struggling to manifest it on my breadboard, very frustrating.

  9. Hi Jeremy, tried this but still no joy. I think this is right but here is an image of the breadboard as is.

    Can you see the error?

  10. No probs i fixed it in the end, but thank you all the same. My midi controlled magic lanterns project is nearly finished, cant wait to share it with you!

  11. Hi,
    You are great at How-To’s. Thanks. I have a question about the external power supply. What would be the differences between the set up you have for the 5v versus using a 5v battery pack(actually 6v made of 4 AA batteries). That is much simpler and I already have that. I am trying to make a robot arm using the arduino as the control of the 5 servos and an trying to do it as simple as possible. Thanks.

  12. I have been watching your tutorials and they have helped me a lot. My project involves using an arduino board to control a RC toy car. The toy car that I am using does not seem to use a servo motor for steering as I had expected. I noticed the steering is controlled by a motor and a gear assembly. If it is in fact using a DC motor, how can I program the controls for the arduino? The link below shows the gear in operation. Thanks for your help!

  13. ok I tried to do the project where you make the motor scale up and then back down. I got it to work a few times but now it won’t work at all. Now it just runs really high and i end up burning out transistors. Then sometimes it won’t run at all or will just ramp down from a high speed to off. Any help would be very much appreciated.

    1. What kind of motor is it? Is it the same kind of transistor as the one that I used? It might not be able to handle the current you’re putting through the motor.

      1. well i’m using a 1.5 to 5 volt DC motor. I bought the transitor at radio shack and it is an MPS 2222A NPN transistor.

          1. well I did do it how it looked in the video, but it was kinda hard to tell. The diode is supposed to be pluged into the 9v power line to the power input line of the NPN resistor, correct? and same with the diode?

  14. Hey Jeremy, AMAZING TUTORIAL! I have a problem with the motor. The voltage regulator is converting th 9v to 5v but once I connect my servo motor, the voltage drops to 2V. I brought this servo. http://www.maplin.co.uk/standard-servo-3739

    I would be happy to take a picture of my circuit if you are unable to slove my issue! Thankyou!

    1. What regulator are you using? How much current can it source? That servo can draw up to half an amp. If that’s more than the regulator can source, you might run into problems.

      1. STMICROELECTRONICS – L4940V5 – IC, V REG LDO +5.0V, 4940, TO-220-3 is the voltage regulator i am using (from farnell). The website says that is can supply 1.5A. After playing around with the circuit, i noticed that i had a 5v output only when i connected the ground pin of the servo directly into the ground of the arduino. That still didnt solve the issue however. I currently have a wire that connects all the grounds on my breadboard to the ground on my arduino, which gives 2v from the output of the voltage regulator again…I connect my servo directly to the arduino 5v supply and it works so there is either an issue with my wiring or the voltage ragulator still..

        1. Ah wait, so because my voltage regulator is supplying a 1.5A means that the servo can not handle it? I guess the only solution is to buy a voltage regulator that outputs the same current as the sevo can handle (0.55mA) ?

        2. No no, the 1.5A supply is fine, the servo will only take as much as it needs. It sounds like you must have a wiring problem. Can you post a picture?

  15. Awesome tutorial! However, I am trying to get a continuous servo motor to rotate 90 degrees, and then stop until it receives the next command. I am aware the the Write command will determine the direction and speed of the servo, but I am not sure what to use to control how far it will rotate.

  16. Hello, Jeremy
    i want to control a Brushless dc motor (BLDC) using Arduino Uno and the controller is supposed to be a 2.4Ghz transmitter and a reciever. I need your help and advice as to how to program a BLDC using a arduino uno and how to interface the transmitter and a reciever (2.4Ghz) to arduino uno and also is it possible to interface all without any other shield. I’ll be thankful for your help and assistance

  17. Hi sir jeremy,
    i just want to ask how can i run both dc motors using 6v supply with arduino uno. everytime i connect the arduino to usb of my pc, it work fine but when i disconnect it to pc and use the battery instead, it works fine with one motor running but with both motor on, it didn’t find any success. can you send me sample program for that?
    is this a good program? this is what i make.

    void loop()
    {
    stop_function();
    delay(2000);
    analogWrite(motorRightF, speed);
    delay(2000);
    stop_function();
    delay(1000);
    analogWrite(motorLeftF, speed);
    delay(2000);
    stop_function();
    delay(1000);
    analogWrite(motorRightF, speed); //not working here
    analogWrite(motorLeftF, speed); .
    delay(5000); .
    stop_function(); .
    delay(5000); up to here//
    }

    thank you very much in advance.

  18. Hi, i’m working with ESC’s for control 4 motor brushless, I need 4 PWM signal, and these have to be in sync, cause is for a quadricopter, the 4 motors, have to recive at the same instant the pulse.

    What can i do?

  19. Hello Jeremy!
    I’d just like to thank you for this video tutorial. You’ve done an excellent job of presenting all information as concisely as possible and the video is very high quality. I’m just letting you know that this particular video has helped me figure out exactly how to control a tactile feedback unit that I am controlling with an arduino mini.

    Also plus points for uploading schematics and source code.

  20. Hey, your videos are awesome and i’ve learnt lots of new stuff. I was wondering if you know an IC – H-Bridge for bigger motors which I would like to controll with the arduino.

    It’s 12 Vdc 2 Amp motors it’s for a medium sized robot would appreciate some help because i’ve been browsing around lookin’ for one for ages but cant find one.

    Thank you

  21. Hi, Jeremy
    I have a modified servo, so now it cannot stop at 180 degrees. Is there any way to control the speed of such a servo.
    Thanks in advance,

    Galina

  22. I’m using a different transistor and I notice that current still flows from the collector to emitter even when the arduino pin is disconnected. Why is this happening? I thought that the base tells it when to allow current to flow? I was hoping to stop and start the fan via code. I’m using a 12v DC fan which is powered by a regulated power supply and pin 9 from the arduino controlling it like in your video. I also noticed that I need to feed the fan a value of 0 via analogWrite for it to go full speed and 255 will bring it all the way down to its slowest speed for some reason.

    I was hoping to control the fan via arduino sketch and have it stop and start but I dont really know whats going on.

    I picked up a bigger transistor with heatsink as the other one I was using was getting pretty hot. Even with the heat sink this one get really hot too for some reason.Not sure why. only happens when I run the fan at a lower speed. According to the fan it draws about 50mA of current and the supply I’m using is a 12V, 300mA.

    1. IF the base is disconnected, then it’s called a “floating pin”. If you want it to be off by default, then you should use a 10K pulldown resistor on the base pin.

      Are you sure you are using an NPN transitor and not a PNP?

  23. Hi Jeremy!

    I really liked your tutorial. It helped me a lot in my current project. I wanted a clarification for connecting a servo motor to arduino.

    Suppose I am powering my arduino via USB and not using a power supply. In that case do I still have to use two power supplies for connecting components of the buffer circuit?

    Thanks in advance.

    1. Yes, you should – USB cannot supply that much current. It’s probably enough for one or two servos, but beyond that, you should use an additional source.

  24. Jeremy,

    Excellent videos. I’m a programmer (Java, ASP, SQL, etc) who is learning this stuff as a hobby/side-career, and your videos are very explanatory, thanks.

    I’ve successfully wired up my Uno to a Servo using a L293D. It works great and my little side project is humming along… I have a few questions, though…

    a)The Servo gets extremely hot after a while, even when it’s not in use. Any suggestions? I have it running off the 5V pin, and no external power sources, just USB. I read I should add resisters? I don’t understand why if the specs say it could handle 6V!

    b)If you have a very basic project that runs fine on USB’s 5V, are there any concerns it wouldn’t work if it ran unplugged from a PC and instead off a 9V DC?

    Thanks!

  25. Hi Jer, I wonder i am trying using your code for servo test, My servo was not performing consistently, It rotate in 20 degree incremental, However when it returned to initial, it start to jerking to the end, meaning it try to keep rotating when it is already in extreme end, forabout 3 times and then go to normal

  26. Hi Jeremy! Any tips on how I can make two servos work at the same time? I hope you can help me. Thanks in advance.

  27. I’ve tried to use the disconnect() command, but it appears that arduino does not have such command. Is it in a library that needs to be included? Can you give me an example on how to use it? Thanks!

  28. Hi there,
    Thank you for the clear and simple tutorial!

    I am starting a project which powers 20 servos (using arduino mega).

    Can I use a single 5V wall wart to power all of these servos?
    Im going to be using cheap turnigy TG9 servos, which draw 200ma when turning, and 600ma stalled.

    Thanks!

  29. i want a program how can i operate a dc motor by pwm…kindly send me program with discription as soon as possible.

  30. hi Jeremy .. i have a doubt , whats the relationship between the dutyclicle of the PWM and the minimum and maximum current across the DC motor, i mean if i want 1 o 2 Amp how can i generate this current levels and control them with the PWM

  31. Hey man much appreciate your video!

    I have a few small questions:

    You said server motor needs PWM to control the position, but how about Linear Actuators? My guess is that I can still use PWM to do this.. Please let me know!
    Also what is their a difference between PWM and PPM? or does it really matter because the width of the pulse simply determines the on time for the motor..

    Thank you so much!

  32. Hi,

    Thanks for the tutorial! I have a problem with my circuit. I followed your instructions but I am unable to run the motor. I am using a 2n3904 npn transistor. The motor doesnt run irrespective of the voltage provided and program code.

  33. Hi Jeremy, quick question….

    i’m using the IR distance sensor with the DC motor to switch on automatically and stay on until no motion is detected.

    shall i use 1) a for loop to gradually increase motor speed

    OR 2) the map function?

    because using the for loop, after every delay, if the IR detects motion, the motor will start to accelerate from 0 again instead of retaining a constant velocity. Thanks! great videos!

  34. Hey Jeremy, Just to drop a line and thank you very much for these tutorials. I’m just beginning to learn this Arduino stuff and your tips are great, friendly and helpful. Thanks.

  35. Hi,

    Thank you so much for the awesome tutorial. I am new to arduino. I want to use the arduino to control a motor that is connected to a breadboard. I want to have the ATmega328 from the Arduino Uno on the breadboard itself and running on batteries. How can I do that? Also since the motors are 3V motors, is there any way I can run the ATmega328 on a 3V battery supply?

    Thanks You so much for your help.

  36. Jeremy,

    Good tutorial. I need some advice in driving the motors. I have 30(1 k resistors), 4(1uf capacitors), L293d, Arduino Uno, 9 V battery, 7.4v lipo battery . Could you please tell me what things should i do to modify the following tutorial according to my need:

    http://itp.nyu.edu/physcomp/Labs/DCMotorControl

    i tried but my motor is not running at full power. If i connect 4 motors with my lipo battery , they are generating that much power which can fly them also in air.

    Have a look at the following quadcopter(not balanced):
    http://www.youtube.com/watch?v=1l6CK-2chwQ&feature=youtu.be

    Thank you :)

  37. Nice tutorial, got it from your schematics!
    Working on a line following bot, and found your tutorials very usefull!

  38. I have a motor that requires a 24VDC and 1amp, so i have an adapter for this (24VDC 1.25A) and was hoping to use a voltage regulator to reduce the voltage 9VDC so I could use the same source to power both my stepper and arduino board. Is this possible? If so I would I determine the size of capacitors to use as you did in the schematics?

    I want to do this so i can put an on off power switch between the wall adapter and my circuit.

    Any help would be appreciated.

  39. This tutorial don’t work for me. I try control my dc motor speed but it don’t move. Anyone else have same problem or knows how to fix it. Maybe anyone else have same problem or knows how to fix it?

  40. Pingback: AccelVibre = modifizierte WiiMote | morpheus Blog

Leave a Reply to sejuta mp3 Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Advertisement