Relay Toggle Sample
This sample demonstrates toggling the relays.
- 2.Copy the sample code to a folder on the Raspberry Pi.
- 3.Open terminal, and then go to the folder you copied the sample.
- 4.Run the sample with the following command:
#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
relay = Turta_Relay.RelayController()
#Toggle relays 1 to 5
for x in range(1, 6):
print("Toggling relay", x)
#Toggle all relays
print("Toggling all relays")
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:
Toggling relay 1
Toggling relay 2
Toggling relay 3
Toggling relay 4
Toggling relay 5
Toggling all relays
To stop the Python script, just press CTRL + C.