Arduino Control Relay Sketch

Article from: arduino-control-relay

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

  1. /* sketch
  2. turn on a fan using a relay and a button
  3. */
  4. int pinButton = 8;
  5. int Relay = 2;
  6. int stateRelay = LOW;
  7. int stateButton;
  8. int previous = LOW;
  9. long time = 0;
  10. long debounce = 500;
  11.  
  12. void setup() {
  13. pinMode(pinButton, INPUT);
  14. pinMode(Relay, OUTPUT);
  15. }
  16.  
  17. void loop() {
  18. stateButton = digitalRead(pinButton);
  19. if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
  20. if(stateRelay == HIGH){
  21. stateRelay = LOW;
  22. } else {
  23. stateRelay = HIGH;
  24. }
  25. time = millis();
  26. }
  27. digitalWrite(Relay, stateRelay);
  28. previous == stateButton;
  29. }


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.

  1. int pinButton = 8;
  2. int Relay = 2;
  3. int stateRelay = LOW;
  4. int stateButton;
  5. int previous = LOW;
  6. long time = 0;
  7. long debounce = 500;
  8. int stayON = 5000; //stay on for 5000 ms
  9.  
  10. void setup() {
  11. pinMode(pinButton, INPUT);
  12. pinMode(Relay, OUTPUT);
  13. }
  14.  
  15. void loop() {
  16. stateButton = digitalRead(pinButton);
  17. if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
  18. if(stateRelay == HIGH){
  19. digitalWrite(Relay, LOW);
  20. } else {
  21. digitalWrite(Relay, HIGH);
  22. delay(stayON);
  23. digitalWrite(Relay, LOW);
  24. }
  25. time = millis();
  26. }
  27. previous == stateButton;
  28. }