STANNUM
Open the simulator

Tutorials / Output

Relay, lamp and the mains

The electromechanical switch that lets a 3.3 V pin drive a lamp on the mains — with the AC side kept away from the board.

Parts in the example:
  • Button
  • Relay
  • AC supply
  • Lamp
The Relay block as it appears on the canvas.

What it is

A relay is a switch operated by an electromagnet. The coil side is low voltage and is driven by a GPIO; the contact side carries whatever the load needs — a lamp on the mains, a pump, a motor — and never touches the board. Each channel has three terminals: COM (common), NA (normally open: closed to COM while the coil is energized) and NF (normally closed: closed to COM while it is not).

In the simulator

The block has one IN pin per channel and, on the load side, the COM/NA/NF terminals of each. A channel's square lights when its coil is energized. To make it do something, the catalog has two more parts: the Lamp, and the AC supply — the wall outlet, with LINE and NEUTRAL. The lamp lights when its two terminals reach line and neutral through closed contacts.

Pins

PinWhat it is
IN1signal — wire it to a GPIO of the board
IN2signal — wire it to a GPIO of the board
COM1
NA1
NF1
COM2
NA2
NF2
VCCpower for the coil driver — to a 3V3 symbol
GNDground — to a GND symbol

Lamp

The Lamp block on the canvas.
PinWhat it is
1one terminal of the filament — to a relay contact or to NEUTRAL; the lamp has no polarity
2the other terminal — to NEUTRAL or to a relay contact

AC supply

The AC supply block on the canvas.
PinWhat it is
LINELINE, the live wire of the outlet — to the relay's COM, never to the board
NEUTRALNEUTRAL — to the lamp's other terminal, never to the board

Wiring

The circuit below is the example Lighting with a relay and lamps from the lab — open it with Project → Open example… and it comes ready to run.

The wired circuit, as the lab draws it.
Relay pinGoes to
IN1D26 on the board (GPIO 26)
IN2D27 on the board (GPIO 27)
COM1LINE of the AC supply
NA11 of the Lamp
NF1left unconnected
COM2LINE of the AC supply
NA21 of the Lamp
NF2left unconnected
VCCa 3V3 symbol
GNDa GND symbol
Lamp pinGoes to
1NA1 of the Relay
2NEUTRAL of the AC supply
AC supply pinGoes to
LINECOM1 of the Relay
COM2 of the Relay
NEUTRAL2 of the Lamp
2 of the Lamp

This is the electrician's circuit: line from the AC supply to COM; the lamp between NA and neutral. Energize the coil and COM–NA closes: the lamp is across the mains. Note that nothing from the right side of the drawing goes anywhere near the board — the AC supply's setting (220, 127 or 110 V) changes labels and warnings, never a voltage on a GPIO.

Settings

Double-click the block's title bar to open its card, then the Settings tab. Changes apply to the running simulation right away.

The Settings tab of the Relay.
SettingAcceptsDefaultNotes
Channels1 to 41Off-the-shelf modules come with 1, 2, 4 or 8 relays on the same board. Lowering it deletes the wires of the channels that stop existing.

AC supply

The Settings tab of the AC supply.
SettingAcceptsDefaultNotes
Mains voltage220 V · 127 V · 110 V220 VIt only changes the label and the warnings: the lamp lights the same. It NEVER goes near the GPIOs.

Code

sketch.cpp
#include <Arduino.h>

// Channel 1: switch (the button toggles). Channel 2: automatic blinker.
// HIGH on IN energizes the coil and closes COM-NA — the lamp lights on 220 V
// WITHOUT the 220 V going anywhere near the ESP32: that is the point of a relay.
const int BOTAO = 4, LUZ = 26, PISCA = 27;
bool acesa = false;

void setup() {
  Serial.begin(115200);
  pinMode(BOTAO, INPUT_PULLUP);
  pinMode(LUZ, OUTPUT);
  pinMode(PISCA, OUTPUT);
  Serial.println("press the button to turn the light on");
}

void loop() {
  static bool antes = HIGH;
  bool agora = digitalRead(BOTAO);
  if (antes == HIGH && agora == LOW) {      // edge: pressed
    acesa = !acesa;
    digitalWrite(LUZ, acesa);
    Serial.printf("light %s\n", acesa ? "ON" : "off");
    delay(30);                              // simple debounce
  }
  antes = agora;

  digitalWrite(PISCA, (millis() / 1000) % 2);   // channel 2 blinks at 0.5 Hz
}

Channel 1 is a toggle: the button's falling edge flips a boolean and writes it to the coil pin. Channel 2 blinks by itself with millis(). digitalWrite(pin, HIGH) energizes a channel.

Run it

Press Build and run. The first build of a project takes a while; after that, only what changed is rebuilt.

The simulation running: the canvas reacts and the serial monitor shows what the code prints.

Press the button on the canvas: channel 1 lights, its COM–NA contact closes and the first lamp lights. The second lamp blinks every second on its own. The serial monitor logs each toggle.

How the simulation models it

Try this

  1. Move the lamp from NA to NF and predict what happens before running: the lamp should be on at rest and go off when you press.
  2. Wire LINE straight to a GPIO and read the warning. Then undo it.
  3. Use a PIR sensor instead of the button to light the lamp for 5 seconds after motion.

See also