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.

No comments:

Post a Comment