Friday, March 4, 2011

Controlling a 12V DC motor/fan with an Arduino

Most of the Arduino runs on 5V and driving low power actuators like LED is fine with a  resistor but how do I control relays / motors / pump / fans that runs more than 5V ?

I get a lot of beginner questions like below :-

1. How do I switch ON & OFF  a 12V DC fan from an Arduino ?
I was told I need a relay, what type of relay should I get ?

2. How do I control the speed of a 12V DC motor or fan from Arduino ?

Let's look at these scenarios one by one so that I can explain them in more details. Firstly, even if a motor or pump or fan (anything with coils inside) is 3V or 5V, do NOT power it directly from the Arduino pins as the Arduino pins can only supply a maximum of 40mA. As per my previous article on Arduino pins, they are not designed to drive a DC motor or coils. 

Always use a separate power source to drive these high load components and have a relay to switch them.

Question 1:-

How do I switch ON & OFF a 12V DC fan from an Arduino ? 
I was told I need a relay, what type of relay should I get ?

A 12V DC fan or motor have coils inside it. You would need a relay to control it. A relay is an electrically operated switch to turn on or off a device. Most relay use an electromagnet to operate a switching mechanism mechanically. You could hear a "click" when the relay switches on when it magnetize the coil and turn on the switch. Below is an image of a 12V Single Pole Double Throw (SPDT) relay housed in a transparent casing. You can see the copper wires winding or coil clearly.

12V SPDT relay

The advantage of a relay is that they can allow high flow of current and voltage across them but the disadvantage of a relays is that it is a mechanical device with springs, so the switching speed is much slower than semiconductors ( called switching speed in the specs ) and have wear and tear of the mechanical parts. They are also more expensive to manufacture compared to semiconductors switches.

Below is my sample circuit using an Arduino to drive a 12V DC Fan with a 12V relay.

- Arduino
- optoisolator 4N35
- 12V SPDT Relay
- 12V PC fan

4N35 optoisolator

An optoisolator is a chip that combined two circuits ( a LED & NPN phototransistor ) into an integrated circuit to isolate two different voltage. The Arduino is on 5V and the relay & DC fan is on 12V. According to the 4N35 datasheets, it can support up to 70V from pin 4 to 5.

See the circuit diagram below :-

Arduino 4N35 12V Relay

When pin 12 is turn ON or HIGH, it will triggered the LED between pin 1 & 2 inside the 4N35 optoisolator (6 pins white chip) to turn on the phototransistor ( pin 4 & 5) and  complete the circuit and send 12V power into the relay (black colour). When the coil is energize, you will hear a click and switch on the motor. The red wires are connected to 12V power supply.

Using this method, it can only switch on or off a circuit but could not control the variable speed of the motor. A second method will use a semiconductor, NPN Power Transistor model TIP122 to control the speed of the DC motor using PWM from pin 9.

On Part 2 of the article, I will explain on how to control the speed of a 12V DC fan using PWM.


  1. Hi stan001,

    Firstly, an excellent tutorial!

    I just received my Arduino Uno yesterday, so forgive me for the dumb questions (to follow) as I'm very new to this.

    I would like to do something similar to what you have done in this tutorial.
    Instead of triggering a 12V DC fan, would like to trigger a 12V DC strike plate lock (for a security gate).
    I presume that what you have done in this tutorial will suite my needs perfectly, simply by swapping out the fan for the lock?

    Can you perhaps re-upload the enlarged image of your diagram, the current image is of low quality and it's difficult to see.


  2. Hi,

    Pls post the questions to the facebook group on the Useful Links above, there are project pictures and more interactions compared to this comments notes...

  3. How would I control a DC motor with a real time clock. I would like to have preset times to operate the motor. Then I would like to preset a time to reverse polarity and run the motor in the opposite direction. Thank you Jeffrey.

  4. Hi. Please correct your Arduino pin power statement - your decimal point is off. This text should say
    "Arduino pins can only supply a maximum of 40mA or 0.04 Amp".

  5. >>Arduino pins can only supply a maximum of 40mA or 0.4 Amp

    This is a rather old blog entry but I thought you'd want to fix this line anyways.

    40mA is 0.04A, not 0.4A


  6. new to this topic i did not realize that there's a chip for this. i was using my IR emitter detector diode. but it was giving too weak output. i could light a small led but not a 6v motor. even though the battery was 6v. big thanks for the tip

  7. Hi there?
    I am also new to this!
    But since the opto-isolator can stand high voltage (upto 70V)
    if we for ex. need to switch ON or OFF something wich is already have its own 12 V supply
    ( you can imagine any 12 V circuit with LED as load but somewhere a open switch with we can then feed into Emitter and Collector of Opto-isolator accordingly.)
    So can we just use Opto-isolator as a switch ON or OFF
    if required?

    (I am sorry I am not good in explaining hopefully you understood what I mean.)

    In facts I want to use arduino to do nothing more than switching ON and OFF a certain Switch which hence completes (closes) a certain circuit.
    will this right?


  8. @ScottInNH, thanks, error corrected.

  9. @Kase, if you just wanted to switch ON / OFF something on a 12V, NO, it does not work as the current passing thru the optoisolator (pin 4 & 5) might be too HIGH for the transistors inside to handle.

    From the 4n35 datasheets, the collector max current is only 100mA.

  10. @jeffery, yes, you can control the fan speed by the time of the day. If you would also like to reverse the polarity, you would also need a H-bridge to do that.

  11. hello..
    I just wanted to know if you could help with the process to make the circuit for a line bot..
    i mean a detailed explanation as to how to connect the Dc motors, relay, pot and everything? :)

  12. Pls post yr questions on my fb group page...

  13. hello im trying to conect a 12v pump but i dont get how to connect the relay ?
    in the foto wich one is the collector, emitter, etc.

    heres the code im using:

    * Trigger a relay

    int pumpPin = 12; // relay

    unsigned long time;
    unsigned long lastPumpTime;

    int pumpPeriodHours = 6; // hours between pumps
    int pumpPeriodSec; // seconds between pumps

    int pumpDurationSec = 10; // seconds the pump is running

    int statusPeriod = 5000; // ms time between status blinks-- to let you know it's alive

    void setup()
    pinMode(pumpPin, OUTPUT);
    digitalWrite(pumpPin, LOW);
    pumpPeriodSec =30; //pumpPeriodHours * 60 * 60; // seconds between pump starts
    //pumpPeriodSec = 30; // for setting short periods when testing

    void loop(){

    lastPumpTime = millis();

    // Activate the pump
    digitalWrite(pumpPin, HIGH);
    digitalWrite(pumpPin, LOW);

  14. I use the optoisolator to control the relay...

    See this links on how to connect the reed relay :

    Future questions, pls post to my fb page... it is more interactive there with more people to help you with your questions...

  15. can i know what ohm of resistor use ?

  16. Like resistors on a LED, 220R or 1kR will do...

  17. I appreciated your kind way of knowledge.Great Stuff Here..hope more to come.

  18. Hi! Thank you very much for this tutorial. However I still do not understand something. Why do you need the optoisolator? Why not enough to send the arduino 12 pin on HIGH to the relay (5volts) and then the relay closes the other circuit (12 volt) and the motor is switched on?

  19. If yr relay is 5V, yes you can do that... but I will put an optoisolator to isolate the relay circuit from my Arduino circuit to protect the Arduino pins from reverse current when the relay is switched off as the current will run thru the arduino pins to ground...

    Just good electronic practice...

  20. Thank you for the quick reply. As I am a very beginner I might be wrong. So I was not specific previously.

    So what I wanted to write is:

    If I have a relay which requires 5V that is needed to activate the armature and the maximum voltage it can handle is above 12V then it would work without damaging the arduino, right?
    And I do not understand why do yo write you want to isolate the two the circuits. In the relay there are isolated, aren’t they? One circuit is connected to the electromagnet (arduino) the other circuit is connected to the armature (12V battery). The electromagnet and the armature are isolated, so the two circuits are also isolated. Or am I wrong?
    Thank you for your reply in advance, as I know that I need to learn a lot…:)

  21. What I meant was those Arduino/Atmega328 digital pins are very sensitive and low powered, 40mA only... and you would not want to damage those pins in any way...

    You can drive a 5V relay without any issues most of the time but for good practice (not always being followed) is to drive a transistor/optoisolator so that the 5V power rail will directly drive the 5v relay without having the current doing thru the Arduino pins...

    1. Understood and thank you very much for the reply. Just to see if I understand it correctly..a diode wouldn't also do it?

      Thank you!

    2. Yes, the most common way I have seen to protect electronics from a relay coil's counter-EMF is to connect a diode across the coil with its cathode toward the (+) supply. This is called a "clamping diode" as it clamps any harmful counter-EMF spike to a mere 0.7 volts.

  22. This comment has been removed by a blog administrator.

  23. This comment has been removed by a blog administrator.



Related Posts Plugin for WordPress, Blogger...