Two AC lamps on an SSR board, on an Arduino Uno
A two-channel G3MB-202P solid-state relay board switches two 12 V AC lamps from an Uno: each channel is on while its pin is LOW, and the lamps take turns. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 30 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
What you'll learn
- Solid state relays have no clicking contacts
- Isolating a low voltage controller from AC
- Low level trigger inputs
- Zero cross switching
- Difficulty: Intermediate
- Time: About 30 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
A solid state relay (SSR) switches AC with no moving parts. A two channel Omron G3MB-202P board takes 5 V on DC+, ground on DC-, and control signals from pins 7 and 8. The relays' output screws sit in series with two 12 V AC lamps across a signal generator.
This board is low level trigger: a channel is on while its pin is LOW. The sketch turns on lamp 1 for a second, then lamp 2 for a second, and repeats.
It is a zero cross SSR, so it turns on and off near the zero of the AC wave, and a lamp starts on a whole half cycle. It only switches AC, never DC.
Parts list
4 parts, plus the jumper wires and a bench instrument. The parts retail for about $33; on Mokxi they're free, and so are the instruments.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × SSR board, 2-channel G3MB-202P
- 1 × Signal generator
- 2 × Filament lampIn Power Bank Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| SSR board, 2-channel G3MB-202P pin DC- | Arduino Uno R3 pin GND |
| SSR board, 2-channel G3MB-202P pin CH2 | Arduino Uno R3 pin 8 |
| SSR board, 2-channel G3MB-202P pin CH1 | Arduino Uno R3 pin 7 |
| SSR board, 2-channel G3MB-202P pin DC+ | Arduino Uno R3 pin 5V |
| SSR board, 2-channel G3MB-202P pin 2A | SSR board, 2-channel G3MB-202P pin 1A |
| Signal generator pin OUT | SSR board, 2-channel G3MB-202P pin 1A |
| Filament lamp (1) pin 1 | SSR board, 2-channel G3MB-202P pin 1B |
| Filament lamp (2) pin 1 | SSR board, 2-channel G3MB-202P pin 2B |
| Signal generator pin GND | Ground |
| Filament lamp (1) pin 2 | Ground |
| Filament lamp (2) pin 2 | Ground |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Two AC lamps on a two-channel solid-state relay board (Omron G3MB-202P).
//
// board DC+ -> 5V DC- -> GND CH1 -> pin 7 CH2 -> pin 8
// each relay's two screws in series with a lamp across the AC supply
//
// The board is "low level trigger": a channel is on while its pin is LOW.
// A zero-cross SSR switches AC only, and turns on and off near the zero of
// the mains, so a lamp starts on a whole half cycle. Here the supply is 12 V
// AC, so the lamps are 12 V ones.
const uint8_t CH1 = 7, CH2 = 8;
void setup() {
// HIGH first, so neither lamp flashes on at power-up.
digitalWrite(CH1, HIGH);
digitalWrite(CH2, HIGH);
pinMode(CH1, OUTPUT);
pinMode(CH2, OUTPUT);
}
void loop() {
digitalWrite(CH1, LOW);
delay(1000);
digitalWrite(CH1, HIGH);
digitalWrite(CH2, LOW);
delay(1000);
digitalWrite(CH2, HIGH);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Writes HIGH to both control pins first, then makes them outputs. That way neither lamp flashes on at power up.
void setup() {
// HIGH first, so neither lamp flashes on at power-up.
digitalWrite(CH1, HIGH);
digitalWrite(CH2, HIGH);
pinMode(CH1, OUTPUT);
pinMode(CH2, OUTPUT);
}loop sketch.ino
Pulls CH1 LOW for 1000 ms, sets it HIGH, pulls CH2 LOW for 1000 ms and sets it HIGH again. Only one lamp is on at a time.
void loop() {
digitalWrite(CH1, LOW);
delay(1000);
digitalWrite(CH1, HIGH);
digitalWrite(CH2, LOW);
delay(1000);
digitalWrite(CH2, HIGH);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Both lamps flash at power up This project
Keep thedigitalWriteHIGH beforepinModeinsetup. - A lamp never lights This project
Check the lamp and the relay output are in series across the AC supply, with the generator ground joined to the lamps. - A channel does not switch. The board is low level trigger, so the input must be pulled LOW to turn the relay on. SSR board
Drive the input LOW to switch on and HIGH to switch off. Set the pin HIGH at start so the load does not switch on at power up. - Mains wiring is unsafe, or a DC load is used on an AC-only relay. These relays are for AC loads, and mains voltage can be lethal. SSR board
Use only loads within the rating, and only AC with this relay. Fuse the load and enclose the wiring. If you are unsure, use a low voltage AC source. - The circuit under test shows clipped or distorted signals. Signal generator
A sine swinging below zero can exceed what an input allows. Add a DC offset or bias so the signal stays inside the supply range of the part. - Measurements are off or noisy. Signal generator
Join the generator's ground to the circuit ground. Keep amplitude and frequency within what the connected parts can handle. - The lamp is dim or does not light when driven from a board pin. Filament lamp
A lamp needs far more current than a pin gives (a pin is limited to about 20 mA). Switch it with a transistor or MOSFET from its own supply that matches the lamp's voltage rating. - The lamp's brightness lags or it burns out the switch when it first turns on. Filament lamp
A cold filament draws several times its running current at first. Size the switch for that surge, not just the steady current.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.