Relay on pin 8 through a transistor, on an Arduino Uno
Pin 8 drives an NPN transistor through 1k, the transistor pulls in a 5 V relay with a diode across its coil, and the relay’s contact lights an LED once a second. The whole build, an Arduino Uno and 6 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.
Beginner Runs in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A relay: pin 8 switches a transistor, the transistor switches the coil.
//
// pin 8 -> 1k resistor -> the transistor's base; emitter -> GND
// collector -> one end of the relay coil; the coil's other end -> 5V
// a diode across the coil, its stripe toward 5V
// relay COM -> 5V, relay NO -> 220 ohm -> LED -> GND
//
// The coil needs more current than a pin can give, so the pin only switches
// the transistor and the transistor carries the coil's current. When the
// transistor lets go, the coil's magnetic field collapses and kicks back a
// voltage spike; the diode gives it somewhere harmless to go.
//
// The relay clicks in for a second, and the LED on its contact lights, then
// clicks out for a second.
const int RELAY = 8;
void setup() {
pinMode(RELAY, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: relay on pin 8");
}
void loop() {
digitalWrite(RELAY, HIGH);
Serial.println("relay in: LED on");
delay(1000);
digitalWrite(RELAY, LOW);
Serial.println("relay out: LED off");
delay(1000);
}
Parts list
8 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Resistor, 1k Ω
- 1 × Transistor (NPN or PNP)
- 1 × Diode
- 1 × DPDT relay
- 1 × Resistor, 220 Ω
- 1 × LED, green
How it is wired
8 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; Transistor (NPN or PNP) pin E; LED, green pin C
- Arduino Uno R3 pin 8; Resistor, 1k Ω pin 1
- Arduino Uno R3 pin 5V; Diode pin C; DPDT relay pin COIL1; DPDT relay pin COM1; DPDT relay pin COM2
- Resistor, 1k Ω pin 2; Transistor (NPN or PNP) pin B
- Transistor (NPN or PNP) pin C; Diode pin A; DPDT relay pin COIL2
- DPDT relay pin NO1; DPDT relay pin NO2; Resistor, 220 Ω pin 1
- DPDT relay pin NC1; DPDT relay pin NC2
- Resistor, 220 Ω pin 2; LED, green pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.