Driving a relay with Arduino

Project Video
Project Details

This video shows how to drive a relay using transistor turn ON and turn OFF using Arduino code.

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

Project Code

 // 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