Engineering · Raspberry Pi · Project 45 of 47 · Industrial automation · Advanced
Two-hand press control
A press may only run while the operator holds two buttons, both pressed within half a second of each other. This keeps both hands away from the danger zone.

CAUTION: 230 V single-phase mains. The relay in this project switches a mains load (Live and Neutral, 230 V AC, 50 Hz in India). Mains can kill. Practise with the simulator or a low-voltage lamp. Build the mains side only with a qualified electrician, in a closed insulated box, behind a fuse or MCB and a 30 mA RCCB. Never touch the relay screw terminals while anything is connected.
Machine safety. This project only shows signalling and logic. A real emergency stop must cut power through hard-wired safety-rated contacts and relays and must never depend on software or a Raspberry Pi.
Parts in this circuit
| Part | Type |
|---|---|
| Left hand | Push button |
| Right hand | Push button |
| Press clutch | Relay module |
| Ready | LED |
| Press running | LED |
Wiring, pin by pin
Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Coloured | Left hand - Signal | Pin 29 (GPIO5) |
| 2 | Black | Left hand - GND | Pin 6 (GND) |
| 3 | Coloured | Right hand - Signal | Pin 31 (GPIO6) |
| 4 | Black | Right hand - GND | Pin 9 (GND) |
| 5 | Coloured | Press clutch - IN | Pin 11 (GPIO17) |
| 6 | Black | Press clutch - GND | Pin 14 (GND) |
| 7 | Red | Press clutch - VCC | Pin 2 (5V) |
| 8 | Coloured | Ready - anode + | Pin 13 (GPIO27) |
| 9 | Black | Ready - cathode - | Pin 20 (GND) |
| 10 | Coloured | Press running - anode + | Pin 15 (GPIO22) |
| 11 | Black | Press running - cathode - | Pin 25 (GND) |
Python code (gpiozero)
from gpiozero import Button, OutputDevice, LED
from time import sleep, time
left = Button(5)
right = Button(6)
press = OutputDevice(17, active_high=False) # clutch relay
ready = LED(27)
running = LED(22)
SYNC = 0.5 # both buttons within 0.5 seconds
t_left = t_right = 0
armed = False
while True:
now = time()
if left.is_pressed and t_left == 0:
t_left = now
if right.is_pressed and t_right == 0:
t_right = now
if not left.is_pressed:
t_left = 0
if not right.is_pressed:
t_right = 0
both = left.is_pressed and right.is_pressed
in_sync = both and abs(t_left - t_right) <= SYNC
if in_sync and not press.value:
press.on()
print("Both hands in sync: PRESS RUNNING")
if not both and press.value:
press.off()
print("A hand was released: PRESS STOPPED")
if both and not in_sync and not press.value:
print("Too slow: release both buttons and try again")
sleep(0.5)
ready.value = not press.value
running.value = bool(press.value)
sleep(0.02)
How the code works
- The program notes the time at which each button went down. A button that is up resets its time to 0.
- in_sync is true only when both are pressed and the two press times are less than 0.5 s apart.
- Releasing either button switches the press off at once.
- A hand held down for a long time and a late second hand never starts the press.
You will learn
- Timing two events against each other
- Why safety controls use two hands
- Keeping a state until a condition ends
Try this
- Hold Left, wait 2 seconds, then hold Right. The press must not start.
- Release one hand while the press runs.
- Change SYNC to 0.2 s. Is it still comfortable for a person?
On a real Pi
- This is only a learning model. A real two-hand control is a certified safety relay (Type IIIA or IIIC to EN 574) with two contacts per button and monitoring.
- Safety circuits are hard-wired and certified. A Raspberry Pi program can never be the safety function.
- Place the buttons so one hand cannot press both, and so they cannot be bridged with a block.
Parts to buy
The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 5 mm LED, red | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 5 mm LED, green | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | 330 ohm resistor, 1/4 W One per LED | 2 | Robu.in Amazon.in ThinkRobotics |
| 7 | Tactile push button (12 mm, with cap) | 2 | Robu.in Amazon.in ThinkRobotics |
| 8 | 1-channel 5 V relay module with optocoupler 10 A 250 V AC contacts | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | 230 V AC small fan | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | Closed ABS enclosure (IP54) for the mains side | 1 | Robu.in Amazon.in ThinkRobotics |
| 11 | 3-way screw terminal block (rated 10 A) | 1 | Robu.in Amazon.in ThinkRobotics |
| 12 | MCB 6 A single pole and RCCB 30 mA (by an electrician) Mains protection | 1 | Robu.in Amazon.in ThinkRobotics |
| 13 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 14 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.