Relay Toggle Sample

This sample demonstrates toggling the solid state 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 solid state relays.
#Do not toggle dc motors or solenoids frequently.
#Install IoT HAT 3 library with "pip3 install turta-iothat3"
from time import sleep
from turta_iothat3 import Turta_Relay
#Initialize
relay = Turta_Relay.RelayController()
try:
while True:
#Option one: Toggle the relays with 'write' method
#Turn relay 1 on
relay.write(1, True)
#Print relay 1 state
print("Relay 1 State...: " + ("On." if relay.read(1) else "Off."))
#Wait
sleep(2.0)
#Turn relay 1 off
relay.write(1, False)
#Print relay 1 state
print("Relay 1 State...: " + ("On." if relay.read(1) else "Off."))
#Wait
sleep(2.0)
#Option two: Toggle the relays with 'toggle' method
#Each method call inverts the relay's state
#Toggle relay 2
relay.toggle(2)
#Print relay 2 state
print("Relay 2 State...: " + ("On." if relay.read(2) else "Off."))
#Wait
sleep(2.0)
#Exit on CTRL+C
except KeyboardInterrupt:
print('Bye.')

Result

When you run the sample, it switches the relays and prints their states. The example output should be like this:

Relay 1 State...: On.
Relay 1 State...: Off.
Relay 2 State...: On.
Relay 1 State...: On.
Relay 1 State...: Off.
Relay 2 State...: Off.

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