X
arduino control relay schematic

Control a Relay with Arduino – Tutorial #5

In this quick Arduino tutorial I will explain how you can control a relay using the Arduino Board, one 1K and one 10K resistors, 1 BC547 transistor, one 6V or 12V relay, one 1N4007 diode and a 12V fan. When the button is pressed the fan will turn ON and will remain in this state until the button is pressed again.

Arduino Relay Sketch

/* sketch 
turn on a fan using a relay and a button
*/
int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 500;

void setup() {
  pinMode(pinButton, INPUT);
  pinMode(Relay, OUTPUT);
}

void loop() {
  stateButton = digitalRead(pinButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateRelay == HIGH){
      stateRelay = LOW; 
    } else {
       stateRelay = HIGH; 
    }
    time = millis();
  }
  digitalWrite(Relay, stateRelay);
  previous == stateButton;
}

Arduino Control Relay Schematic

arduino control relay schematic

How does the circuit works

When the button is pressed the Arduino board will put pin 2 in HIGH state, meaning 5V on pin 2. This voltage is used to drive the transistor that will switch ON the relay and the load (in our case the fan) will be powered from the main power supply.

You cannot use the 5V from the USB to power up the transistor and the LOAD because the USB port usually delivers only 100mA, and this is not enough to switch the relay and the LOAD. That is why you must use an external power supply (Vcc) that is between 7 to 12 volts to power up the Arduino board and the transistor + relay. The load uses its own power supply, for instance if you use a light bulb then you might connect it to the 110/220V mains or any other power source.

DO NOT connect in any ways the main power supply that drive the LOAD to the arduino and transistor circuitry!

Turn OFF the relay with delay

You can use this code example to introduce a delay in your circuit. The variable “stayON” is used to delay() the program execution with the desired amount of time. In our case after the button is pressed the relay will be switched ON and after 5 seconds will the turned OFF.

int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 500;
int stayON = 5000; //stay on for 5000 ms

void setup() {
  pinMode(pinButton, INPUT);
  pinMode(Relay, OUTPUT);
}

void loop() {
  stateButton = digitalRead(pinButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateRelay == HIGH){
      digitalWrite(Relay, LOW);
    } else {
       digitalWrite(Relay, HIGH);
       delay(stayON);
       digitalWrite(Relay, LOW);
    }
    time = millis();
  }
  previous == stateButton;
}

30 Comments

Join the conversation!

Error! Please fill all fields.
  • bsalawuadeniyi

    Hi, this is a very informative site. I need help with programing my arduino to be able to control a relay to be turn on and turn off a power supply and a sonicator. Thank you for your help nd time.

  • Martin Tepira

    Hey, thanks for tutorial but I got problem when try to use another pin for relay. If I use e.g. number 8 my relay starts click. Where could be problem?

  • dloniah

    I want to configure arduino as fm reciever to switch relay. What can I do ? thank you

  • Matt

    This is great. But how can I control this from another Arduino? For example I want to push a button and power the relay from a wireless connection.

  • Roger

    I read the purpose of this circuit and several questions, the purpose is to turn on a fan by pushing a button and then off again by pushing the button, My question is Why would you even use an Arduino?. Why not just put a switch on the fan?. What am I missing here?
    Cheers, Roger

  • will

    great post needed code for my interactive pellet gun target and this worked perfectly with only a small change to the timming.

    thanks

  • satishap

    Very nice project. Do anybody have smartphone and Arduino controlled ON/OFF A.C Appliance Projects..

  • Jao

    Nice post. I have some question. if the input is in high and timer is activated and start counting. what if the input will go to low again and back to high. does the delay timer be reset also.

  • Isuru Walpola

    Line 28 should be:
    previous = stateButton;

  • Vineet

    Really helpful idea. I have put it to use with simultaneous IR control to the relays. I’m using an adapter to power both arduino and relays. The problem I’ve been having is that my relay starts flickering ruthlessly , if I remove the 5 volts given to the debounced button or if I remove the pin connection of the debounced button. any help is appreciated.

Looking for the latest from TI?
Looking for the latest from TI?