• Register
  • Login
  • looking for something?

    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;
    }
    
    Find more projects
    

    6 Responses to "Control a Relay with Arduino – Tutorial #5"

    subscribe to newsletter
    1. Richard Knight says: on July 17, 2013 at 11:38 pm

      I love simple electronic projects but do not see the point of useing a Complex IC to do the job of a of a 1g 1w switch.
      Usually the projects are very good,educational and useful. But there seems to be a trend to use ICs and micro processors just to find a use for them.
      Anyway keep up the good work
      Kind regards Richard Knight (retired electrician and lifelong electronics geek).

      Like or Dislike: Thumb up 0 Thumb down 0

      • Hi Richard, you are right, but we will get into more complex things in the future, things that usually will require a lot of components, but by using a microcontroller the circuit and the currenct consumption will be much smaller.

        Like or Dislike: Thumb up 0 Thumb down 0

    2. Hi Martin
      I have a quick question for you. What is the purpose of using 10k resistor between pin 8 and ground ? Marek

      Like or Dislike: Thumb up 0 Thumb down 0

      • Because when nothing is connected to a pin digitalRead() gives random results. When connected to ground digitalRead() == LOW, when connected to +5V digitalRead() == HIGH.

        Like or Dislike: Thumb up 0 Thumb down 0

    3. Hello, I am looking for a way to combine these two examples. For example, press a button, relay stays on for 20 seconds, relay turns off. However, I would like it to also turn off with a second button press. Could you write some code as an example of this? I am new to Arduino and it would help me very much.

      Thank you for the two great examples above!

      Like or Dislike: Thumb up 0 Thumb down 0

    4. hello…
      nice project.
      how about adjust the relay ON time with potentiometer.

      Like or Dislike: Thumb up 0 Thumb down 0

    What do you think about this article? Leave a comment!