Arduino Relay Module: Wiring, Code and Simulator

To build this Arduino Relay Module project you need an Arduino Uno and a relay module, a push button, a 220 Ω resistor and a yellow LED. A button on pin 2 toggles a relay on pin 7. Its COM and NO contacts switch a lamp on 5V. Make the 9 connections in the wiring table, upload the Arduino C++ code, and click the button: the relay clicks over and the lamp lights, with no Arduino pin driving it.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1Relay ModuleIN HIGH switches COM from NC to NO. VCC to 5V, GND to GND. The contacts are a plain switch in the circuit.
1push buttonConnects 1 to 2 while held. Pair with INPUT_PULLUP and wire the other side to GND.
1220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1yellow LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.

Wiring

#FromTo
1Arduino Uno 5VRelay Module VCC
2Arduino Uno GNDRelay Module GND
3Arduino Uno pin 7Relay Module IN
4Arduino Uno 5VRelay Module COM
5Relay Module NO220 Ω resistor leg 1
6220 Ω resistor leg 2yellow LED anode (A, long leg)
7Arduino Uno GNDyellow LED cathode (C, short leg)
8Arduino Uno pin 2push button 2.l
9Arduino Uno GNDpush button 1.r

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add a relay module, a push button, a 220 Ω resistor and a yellow LED from the parts palette.
  3. Wire 1. Connect Arduino Uno 5V to Relay Module VCC.
  4. Wire 2. Connect Arduino Uno GND to Relay Module GND.
  5. Wire 3. Connect Arduino Uno pin 7 to Relay Module IN.
  6. Wire 4. Connect Arduino Uno 5V to Relay Module COM.
  7. Wire 5. Connect Relay Module NO to 220 Ω resistor leg 1.
  8. Wire 6. Connect 220 Ω resistor leg 2 to yellow LED anode (A, long leg).
  9. Wire 7. Connect Arduino Uno GND to yellow LED cathode (C, short leg).
  10. Wire 8. Connect Arduino Uno pin 2 to push button 2.l.
  11. Wire 9. Connect Arduino Uno GND to push button 1.r.
  12. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  13. Run it. Press Run. Click the button: the relay clicks over and the lamp lights, with no Arduino pin driving it.

Arduino C++ code

// Relay: each button press flips the relay, and its contacts switch a lamp.
const int BUTTON = 2;
const int RELAY = 7;

void setup() {
  pinMode(BUTTON, INPUT_PULLUP);
  pinMode(RELAY, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  static bool on = false;
  static bool last = HIGH;
  bool now = digitalRead(BUTTON);
  if (last == HIGH && now == LOW) {
    on = !on;
    digitalWrite(RELAY, on);
    Serial.println(on ? "Relay ON: lamp lit" : "Relay OFF");
  }
  last = now;
  delay(20);
}

What to try

Click the button: the relay clicks over and the lamp lights, with no Arduino pin driving it.

Frequently asked questions

How do I wire the Relay Module project on an Arduino Uno?

Make 9 connections: Arduino Uno 5V to Relay Module VCC; Arduino Uno GND to Relay Module GND; Arduino Uno pin 7 to Relay Module IN; Arduino Uno 5V to Relay Module COM; Relay Module NO to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to yellow LED anode (A, long leg); Arduino Uno GND to yellow LED cathode (C, short leg); Arduino Uno pin 2 to push button 2.l; Arduino Uno GND to push button 1.r.

What parts do I need for this Arduino project?

An Arduino Uno and a relay module, a push button, a 220 Ω resistor and a yellow LED.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

A button on pin 2 toggles a relay on pin 7. Its COM and NO contacts switch a lamp on 5V.

More Arduino Uno projects