Using relay switch with Arduino
The article discusses how to operate a relay switch using an Arduino Uno.
Disclaimer
This post does not delve into the operation of relay switches.
Working sample
Before we continue, let's see what our final result will look like.
Items needed
- Arduino Uno
- 5V Relay switch
- Small DC motor
- 5 V DC power supply
Pre-requisites
This post assumes you already have,
1. Arduino Editor installed on your laptop.​
2. Your laptop connected to Arduino using the USB cable.
3. Basic familiarity with Arduino programming using C++.
Technical details
How relay switch works?
This post does not delve into the operation of relay switches. There are plenty of resources available on the internet that cover this topic extensively.
Schematic & Wiring diagrams
This is how the final setup looks like:
How-to steps
Step 1: Connecting Arduino to relay switch
Step 2: Connecting power supply to motor through relay switch
Step 3: Arduino C++ code
No additional libraries need to be downloaded. The code is straightforward.
C++ code
const int relayActivationPin = 9;
void setup() {
Serial.begin(9600); //added by hari
pinMode(relayActivationPin, OUTPUT);
}
void loop() {
int counter = 0;
while (counter < 5){
counter++;
delay(3000);
Serial.print("RelayActivated...");
digitalWrite(relayActivationPin, HIGH);
delay(3000);
Serial.println("...and deactivated.");
digitalWrite(relayActivationPin, LOW);
}
}
Step 4: Testing the relay switch
Now that the setup should be ready, running the C++ code should activate the DC motor for a few seconds and then stop. This cycle should repeat continuously.
Closing note
I hope this post provides you with the essential details for connecting an Arduino with a relay switch to activate or deactivate a DC motor. For a demonstration, please refer to the accompanying video found at the top of this page.
Thanks,
Music by QubeSounds from Pixabay