Arduino Controlling Relay

Intro
Relays adds control from a low level Arduino to a very high level current. A relay is a electromagnet that attracts a metal piece making a closed connection for the circuit. It can be looked at like a switch. The reason why a relay is so great is because the switch has no physical connection to the Arduino, so it cant be damaged in anyway. Relays range from a low 12v DC to 240v AC. You can see how useful this could be if you are using 3.3v to 5v DC from an Arduino.

History
The best way to fully understand how things work is by starting from the beginning. On 1835, a British scientist named Joseph Henry invented the relay that consisted of a electromagnet and 3 bars. Unfortunately there was no practical use for the relay at this time. Until communications with phones and telegraph stated to boom. They used them with rotary dial phones, switch boards, and with mores code. Relays added a benefit because it is much smaller and reliable than other mechanical methods like gears. This lead to the use with calculations because they can use logic(on/off) for the first time.

How to build
Micro-controller:
Arduino

Components:
Resistor (1K)
Diode (1n4004)
Transistor (2n3904)
Relay (QUAZ-SS-150D)

Objects:
Jumper Wire
Bread Board

Here is the Schematic that we are going to use:


Here is the beadboard setup:


Remember relays have diffrent setups, this maybe what yours looks like. Always look at your data sheets before you hook up your relay.


What this code will do is take an incoming byte of 1(On) or 0(Off). That you type in your serial monitor.

int incomingByte = 0;   // for incoming serial data

void setup() {
  pinMode(8, OUTPUT);
  Serial.begin(9600);
}

void loop() {
// send data only when you receive data:
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    
    if(incomingByte == 49) {
      digitalWrite(8, HIGH);
      Serial.println("Relay On");
    }
    else if (incomingByte == 48) {
      digitalWrite(8, LOW);
      Serial.println("Relay Off");
    }
  }
}

This code simply turns the relay on and off

void setup() {
  pinMode(8, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(8, HIGH);
  Serial.println("Relay On");
  delay(500);
  digitalWrite(8, LOW);
  Serial.println("Relay Off");
  delay(500);
}



Writer

I've started with programming and electronics at a very young age, and it has stuck with me from then on.
I figured that it's about time I start showing my technics and projects. Mainly just to show how fun this can be. Tyler VanProoyen - Founder

subscribe to our newsletter.