Relay Toggle Sample

This sample demonstrates toggling the relays.

To run the sample:

  1. Copy the sample code to a folder on the Raspberry Pi.

  2. Open terminal, and then go to the folder you copied the sample.

  3. Run the sample with the following command:

python3 Relay_Toggle.py

Sample Code

Relay_Toggle.py
#!/usr/bin/env python3
#This sample demonstrates toggling the relays.
#Install Relay HAT 2 library with "pip3 install turta-relayhat2"
from time import sleep
from turta_relayhat2 import Turta_Relay
#Initialize
relay = Turta_Relay.RelayController()
try:
while 1:
#Toggle relay 1
relay.toggle(1)
print("Toggle relay 1: " + ("On" if relay.read(1) else "Off"))
sleep(2.0)
#Toggle relay 2
relay.toggle(2)
print("Toggle relay 2: " + ("On" if relay.read(2) else "Off"))
sleep(2.0)
#Toggle relay 3
relay.toggle(3)
print("Toggle relay 3: " + ("On" if relay.read(3) else "Off"))
sleep(2.0)
#Toggle all relays
relay.toggle_all()
print("Toggle all relays: " + str(bin(relay.read_all())))
sleep(2.0)
#Toggle relay 4
relay.toggle(4)
print("Toggle relay 4: " + ("On" if relay.read(4) else "Off"))
sleep(2.0)
#Toggle relay 5
relay.toggle(5)
print("Toggle relay 5: " + ("On" if relay.read(5) else "Off"))
sleep(2.0)
except KeyboardInterrupt:
print('Bye.')

Result

When you run the sample, the relays invert their states sequentially. The application also writes the relay states to the terminal. The example output should be like this:

Toggle relay 1: On
Toggle relay 2: On
Toggle relay 3: On
Toggle all relays: 0b11000
Toggle relay 4: Off
Toggle relay 5: Off
Toggle relay 1: Off
Toggle relay 2: Off
Toggle relay 3: Off
Toggle all relays: 0b00111
Toggle relay 4: On
Toggle relay 5: On

To stop the Python script, just press CTRL + C.