Download the sample code from https://github.com/Turta-io/RelayHAT2/blob/master/Samples/Python/Relay_Toggle.py​
Copy the sample code to a folder on the Raspberry Pi.
Open terminal, and then go to the folder you copied the sample.
Run the sample with the following command:
python3 Relay_Toggle.py
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 sleepfrom turta_relayhat2 import Turta_Relay​#Initializerelay = Turta_Relay.RelayController()​try:while 1:#Toggle relays 1 to 5for x in range(1, 6):print("Toggling relay", x)relay.toggle(x)sleep(2.0)​#Toggle all relaysrelay.toggle_all()print("Toggling all relays")sleep(2.0)​except KeyboardInterrupt:print('Bye.')
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 1Toggling relay 2Toggling relay 3Toggling relay 4Toggling relay 5Toggling all relays
To stop the Python script, just press CTRL + C.