DIY: Relay Switch Motor Controller - Arduino





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:

9v battery:

M-F jumper wires:


5V relay switch:

Step 1: Connect the Motor & Power Supply


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


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:

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:



  • Microcontroller Contest

    Microcontroller Contest
  • Science of Cooking

    Science of Cooking
  • Pocket-Sized Contest

    Pocket-Sized Contest

We have a be nice policy.
Please be positive and constructive.




I need a similar motor controller. But I only need a controller that will start the motor for 5 minutes then shuts it off. I would need a button to start it each time. All small items, nothing that takes a lot of room. Smaller is better! How do I do this?

good video. Can you make a video on how to control the motor speed using relay and arduino?

hi thanks for this very clear video. even for me ( a nono in electric )
i got it to work fine for me. i am making a solar tracking device with a 12 volt (drill) dc motor. you say in your video that you can put power directly on the motor for more speed. could you explain this a bit more for me ? scheme perhaps ?
thanks in advance.
kind regards JP ,the netherlands

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 ?

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 ?

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

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

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

go through this