Relays reverse a motor on an Arduino Uno
Two motors turned both ways by relays: one on a two-channel board wired as an H bridge, one on a bare DPDT relay switched by a transistor. The whole build, an Arduino Uno and 7 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.
IntermediateRuns 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.
// Relay board: three ways to switch things a pin cannot.
//
// pins 2 to 5 IN1 to IN4 of the four-channel board: four lamps
// pins 6, 7 IN1 and IN2 of the two-channel board: motor A both ways
// pin 8 the DPDT relay's transistor, through 1 k: motor B both ways
//
// Two circuits run this sketch: "Relay board" wires the lamps, and "Relays
// reverse a motor" wires the two motors. Whatever a circuit leaves unwired
// is simply a pin toggling with nothing on it.
//
// # Active low
//
// The relay boards are active low: an optocoupler's LED sits between VCC and
// each IN pin, so a channel is ON when its pin is LOW. relayOn() hides that,
// and setup() writes every pin HIGH *before* making it an output, because the
// other order leaves each pin low for an instant and every relay clicks on at
// power-up.
//
// # Two relays make an H bridge
//
// Motor A sits between the two channels' COMs. Each NC is on ground and each
// NO on 5 V. Both released: both ends on ground, which is a brake. Channel 1
// in: that end goes to 5 V and the motor turns. Channel 2 in instead: the
// other way. Both in: both ends on 5 V, a brake again, and never a short,
// because each COM is only ever on one contact.
//
// # One coil, two poles
//
// The DPDT relay does the same with one coil, because its two poles move
// together: the motor between the COMs, the supply crossed over between the
// NOs and the NCs. It is a bare relay, so its 40 mA coil hangs off 5 V
// through a 2N2222, with a 1N4007 across it for the kick.
//
// Relays are slow: 10 ms to pull in, 5 ms to let go. Nothing here asks more of
// them than a few clicks a second.
const int LAMPS[] = {2, 3, 4, 5};
const int MOTOR_A[] = {6, 7};
const int DPDT = 8;
void relayOn(int pin, bool on) {
digitalWrite(pin, on ? LOW : HIGH);
}
void setup() {
for (int pin = 2; pin <= 7; pin++) {
digitalWrite(pin, HIGH); // off, before it is even an output
pinMode(pin, OUTPUT);
}
pinMode(DPDT, OUTPUT);
digitalWrite(DPDT, LOW);
Serial.begin(115200);
Serial.println("Mokxi Uno: relays three ways");
}
void chase() {
for (int i = 0; i < 4; i++) {
relayOn(LAMPS[i], true);
delay(250);
relayOn(LAMPS[i], false);
}
}
void loop() {
Serial.println("motor A forward, motor B forward");
relayOn(MOTOR_A[0], true);
digitalWrite(DPDT, LOW);
chase();
Serial.println("both motors braked");
relayOn(MOTOR_A[0], false);
delay(600);
Serial.println("motor A reverse, motor B reverse");
relayOn(MOTOR_A[1], true);
digitalWrite(DPDT, HIGH);
chase();
relayOn(MOTOR_A[1], false);
delay(600);
}
Parts list
11 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
- 2 × DC motor
- 1 × Relay board, two channels
- 2 × Power
How it is wired
13 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; DPDT relay pin NO1; DPDT relay pin NC2; Relay board, two channels pin NC1; Relay board, two channels pin NC2
- Arduino Uno R3 pin 8; Resistor, 1k Ω pin 1
- Arduino Uno R3 pin 7; Relay board, two channels pin IN2
- Arduino Uno R3 pin 6; Relay board, two channels pin IN1
- Resistor, 1k Ω pin 2; Transistor (NPN or PNP) pin B
- Transistor (NPN or PNP) pin C; Diode pin A; DPDT relay pin COIL2
- Supply: Diode pin C; DPDT relay pin COIL1; DPDT relay pin NC1; DPDT relay pin NO2; Relay board, two channels pin NO1; Relay board, two channels pin NO2
- DPDT relay pin COM1; DC motor (1) pin 1
- DPDT relay pin COM2; DC motor (1) pin 2
- Ground: Relay board, two channels pin GND
- Supply: Relay board, two channels pin VCC
- Relay board, two channels pin COM1; DC motor (2) pin 1
- Relay board, two channels pin COM2; DC motor (2) pin 2
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.