Introduction: DIY: Relay Switch Motor Controller - Arduino

Hello everyone,

In this tutorial, we will be building a motor controller using two relay switches and an arduino.

Components used in this project:

SainSmart UNO R3: http://www.amazon.ca/gp/product/B00E5WJSHK?psc=1&...

9v battery: http://www.amazon.ca/Tenergy-Charger-250mah-Recha... http://www.dx.com/p/dd15-dc-9v-battery-button-pow...

M-F jumper wires: http://www.amazon.ca/Accmart-Flexible-Breadboard-...

Motor: http://www.amazon.ca/6760RPM-Rotary-Speed-bornes-...

5V relay switch: http://www.amazon.ca/SainSmart-2-CH-2-Channel-Rel...

Youtube version:

Step 1: Connect the Motor & Power Supply

Connections:

Motor positive - Relay switch 1 COM input

Motor negative - Relay switch 2 COM input

9v battery positive - Relay switch 1 NO & Relay switch 2 NO

9v battery negative - Relay switch 1 NC & Relay switch 2 NC

Each relay switch has three inputs

NO (Normally open) ,COM (Common) and NC (Normally closed)

In this project, both wires from the motor will connect to both of the COM (middle) inputs of the relay switches.

The positive wire from the 9v battery will connect to both of the NO (top) inputs.

The negative wire from the 9v battery will connect to both of the NC (bottom) inputs.

This external power supply (9v battery) will be used to power the motor. This can be changed to control the speed of the motor.

Step 2: Connect the Arduino

Connection:

Arduino 5v pin - Relay module VCC pin

Arduino GND pin - Relay module GND pin

Arduino pin#7 - Relay module IN1

Arduino pin#8 - Relay module IN2

After we finish connecting the motor, all we need to do is power the relay switch module itself. using the arduino, we can connect the wires and send signals to activate the relay switches.

Step 3: Upload the Code:

#define CW 7 //CW is defined as pin #7//

#define CCW 8 //CCW is defined as pin #8//

void setup() { //Setup runs once//

pinMode(CW, OUTPUT); //Set CW as an output//

pinMode(CCW, OUTPUT); //Set CCW as an output//

}

void loop() { //Loop runs forever//

digitalWrite(CW,HIGH); //Motor runs clockwise//

delay(1000); //for 1 second//

digitalWrite(CW, LOW); //Motor stops//

digitalWrite(CCW, HIGH);//Motor runs counter-clockwise//

delay(1000); //For 1 second//

digitalWrite(CCW, LOW); //Motor stops//

}

//end of code//

Download the code here:

https://drive.google.com/open?id=0Bwb-DkQkOX3jfkdI...

Step 4: Done!

If everything was done correctly, you should end up with a motor that spins clockwise for 1 second and counter-clockwise for 1 second.

As always, you can modify the code and change the voltage for your own projects.

If you want to see this project in action, please click on the link below:

Comments

author
pavan kumarm (author)2017-03-03

Can I use an electromagnet in the place of a dc motor for changing the polarity? Also im facing the problem with the repulsive forces between electromagnets. Will u pls suggest ?

author
Malik Attau (author)2017-01-18

I m using this ioio otg in project of home automation if i use pin 1 to 30 to connect load with the help of relays it will effect the performance of IOIO OTG or not ?? current will effect IOIO OTG ?

author
Malik Attau (author)2017-01-18

i m using same relay for my home automation project but the problem is that when i give 5 v from ioio otg to chnel 1 relay does not work but hen i give grnd pin to the chanel 1 pin relay operates what is the problem can anyone tell me plzz

author
shriharis1729 (author)2016-05-04

Can anyone tell how the same would work with a single relay channel. Or will it not ?

author

it may help you

author
nupurpur (author)2016-02-12

I connected the circuit perfectly. The relays seem to work fine,although the motor doesn't rotate.What could be the reason?

author
JUNEZ RIYAZ (author)nupurpur2016-08-11

go through this

https://www.youtube.com/watch?v=2LqPKpQfyTE

author
samarthcars (author)2015-12-09

Sir I have an arduino ,motor sheild and a ping sensor.. I want cary a heavy load by my 12v motors but my notor sheikd don not give current output more than 1 amp.. But to carry load i need atleast 20 amp bow how can i do this

author
nayeem_ruet_eee (author)2015-11-25

How can I find out which relay is required for my dc motor?
Relay is used only for dc motor?
Thanks.

author
EuH1 (author)2015-08-23

Can I know how to control the speed of the motor?

author
praxmehta (author)2015-07-24

i have 12v motor then what me do ?

author
marsekal (author)2015-06-21

good very good

author
chintans (author)2015-06-12

Hey SL,

Good and simple tutorial.

Hey webweave,

Nice find.

Can any1 tell how fast it can move the motor cw-ccw?

I'm thinking of something to make from this.

author
webweave (author)2015-05-21

You really should draw out a schematic and you'll see the problem with this setup. First, when you reverse the motor that is already spinning, DC motors generate power so a voltage from the motor will be present at the relay which will resist the reversed voltage, this surge over time will likely destroy the motor and possibly the relays due to excessive voltage. And second if you accidentally throw both relays in the same direction it will cause a short. A better method is to use one relay to reverse the motor and the other relay to turn it on and off, that way you can build up a delay in your arduino to allow the motor to stop spinning before you reverse the polarity and there is no combination of relays that will cause a short circuit. MOVs are also a good idea to prevent arcing of your relays.

motor switch.png
author
Simple-Circuits (author)webweave2015-05-21

So, I used this guide to create the motor controller. Im not sure if I missed a step but the circuit seems to be working fine without any problems.

would this setup be problematic if i use higher voltage?

is it possible to add a delay to this setup to stop the motor from spinning, using the arduino?

Screen Shot 2015-05-22 at 1.28.06 AM.png
author
webweave (author)Simple-Circuits2015-05-23

This looks good. To stop just go to condition one or four of your logic diagram for the amount of time it takes your motor to stop spinning.

author
madhavdivya (author)2015-05-21

Hi Simple-Circuits, Great one. Looking at your 'ible, I got an idea to implement tracking system to my solar panel. Software is my nemesis. I can go nowhere with it. I started depending on graphical programming tool and developed the logic and simulated it in Flowcode. Works well. This is where I am stuck. When I try to put it on the micro controller - just does not work. Can send you the code as it is in Flow Code. Can you help ? Thanks in advance.

author
llucop (author)2015-05-21

Great -able! Just a quick one, can you drive an AC motor the same way?

TIA

author
hithisishal (author)2015-05-21

Nice explanation, but why did you choose to use relays, rather than an h-bridge? Is there an application where there is an advantage to running a motor with relays?

author

For my application, the new ping library was not working with the adafruit motor shield so i decided to use relay switches as an alternative motor controller.

As for this project, the use of relay switches seemed easier for me to explain rather than an h-bridge, which has more room for error.

author
tinaciousz (author)2015-05-20

Wonderful documentation!

author
seamster (author)2015-05-19

Excellent tutorial... great photos and very simple to follow! Thank you!

About This Instructable

82,469views

550favorites

Bio: Youtube: www.youtube.com/channel/UCh5aXlcWXmVQ4ipj0y3qjbw?&ab_channel=SimpleCircuits
More by Simple-Circuits:USB Powered Laptop ChargerArduino - Laser Tripwire Alarm SystemT.A.P Bot 2.0 - The Wireless S.W.I.P.E Bot
Add instructable to: