Switching of Coil Relay Modules
Switching of Coil Relay Modules
Just Arduino Things | TW TechieTube
Try to switch on and off a Coil Relay using Arduino UNO in various methods.
Principle:
Components Required:
- Arduino UNO (1)
- 1 Channel 5vDC Relay Module (1)
- Breadboard (As per Requirement)
- Jumper Cables (As per Requirement)
- Arduino Bus Cable (1)
Construction:

Code:
//CODE: Auto-Loop code for the relay to switch in a 1-second interval
int EMSwitch = 13;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(EMSwitch, HIGH);
delay(1000);
Serial.println("EMSwitch is ON");
digitalWrite(EMSwitch, LOW);
delay(1000);
Serial.println("EMSwitch is OFF");
}
//CODE: Timer-based switching on the relay by user input
int EMSwitch = 13;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println("Enter number of seconds");
delay(10000);
const int n = Serial.parseInt();
if(n>0){
Serial.println("EMSwitch will be ON for the specified seconds");
digitalWrite(EMSwitch, HIGH);
delay(n*1000);
digitalWrite(EMSwitch, LOW);
Serial.println("EMSwitch was ON for the specified seconds");
}
else {
digitalWrite(EMSwitch, LOW);
}
}
//CODE: Counter-based switching on the relay by user input
int EMSwitch = 13;
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(EMSwitch, LOW);
}
void loop() {
Serial.println("Enter the number of times to switch on and off the relay:");
delay(10000);
int n = Serial.parseInt();
int x;
for(x=0; x<n; x++)
{ digitalWrite(EMSwitch, HIGH);
delay(1000);
digitalWrite(EMSwitch, LOW);
delay(1000);
Serial.println("cycle has occured");
}}
Comments
Post a Comment