Other Arduino Codes and Videos by Robojax

Driving a relay with Arduino

Driving a relay with Arduino

This code is to drive (turn ON and turn OFF) a relay using Arduino code.

This module has problem with voltage to relay. See my video Showing you how to solve the problem.

Components used in this module

 // This sketch is part of Video Tutorial showing how to control a 5V relay
 // please watch the video at this URL: https://youtu.be/N-fFk51Eb8s
 // Written by Ahmad Shamshiri for Robojax.com video on October 12, 2018 at 21:19 in Ajax, Ontario 
 // Introduction to 5V relay.
int relayPin = 2;// define pin for relay

void setup() {
  // Robojax.com relay tutorial 20181012
  pinMode(relayPin, OUTPUT);
  Serial.begin(9600);// initialize serial monitor
  Serial.println("Robojax.com Relay Module");
  digitalWrite(relayPin, HIGH);// turn relay ON
  delay(2000);// wait for boot

void loop() {
  // Robojax.com relay tutorial 20181012
  digitalWrite(relayPin, HIGH);// turn relay ON
    Serial.println("Relay ON");
  delay(300);// wait for 5 seconds
  digitalWrite(relayPin, LOW);// turn relay OFF
       Serial.println("Relay OFF");
  delay(1000);// wait for 3 secons


If you found this tutorial helpful, please support me so I can continue creating content like this. You can support me on Patreon ( Launched June 14, 2019): http://bit.ly/2ZnITLz

Or support me via PayPal