Fareed Mohmed← Project library

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.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Two-hand press control: the board, its power supply and the parts wired to the 40-pin header
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

PartType
Left handPush button
Right handPush button
Press clutchRelay module
ReadyLED
Press runningLED

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.

#ColourFrom (part and leg)To (header pin)
1ColouredLeft hand - SignalPin 29 (GPIO5)
2BlackLeft hand - GNDPin 6 (GND)
3ColouredRight hand - SignalPin 31 (GPIO6)
4BlackRight hand - GNDPin 9 (GND)
5ColouredPress clutch - INPin 11 (GPIO17)
6BlackPress clutch - GNDPin 14 (GND)
7RedPress clutch - VCCPin 2 (5V)
8ColouredReady - anode +Pin 13 (GPIO27)
9BlackReady - cathode -Pin 20 (GND)
10ColouredPress running - anode +Pin 15 (GPIO22)
11BlackPress 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

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.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, red1Robu.in Amazon.in ThinkRobotics
55 mm LED, green1Robu.in Amazon.in ThinkRobotics
6330 ohm resistor, 1/4 W
One per LED
2Robu.in Amazon.in ThinkRobotics
7Tactile push button (12 mm, with cap)2Robu.in Amazon.in ThinkRobotics
81-channel 5 V relay module with optocoupler
10 A 250 V AC contacts
1Robu.in Amazon.in ThinkRobotics
9230 V AC small fan1Robu.in Amazon.in ThinkRobotics
10Closed ABS enclosure (IP54) for the mains side1Robu.in Amazon.in ThinkRobotics
113-way screw terminal block (rated 10 A)1Robu.in Amazon.in ThinkRobotics
12MCB 6 A single pole and RCCB 30 mA (by an electrician)
Mains protection
1Robu.in Amazon.in ThinkRobotics
13Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
14Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 44. Sunshine meter with an LED barAll projects46. Bottle filling station →

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.