Relay LED Control Sample
This sample demonstrates turning relay indicator LEDs on and off.
- 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 turning relay indicator LEDs on and off.
#Install Relay HAT 2 library with "pip3 install turta-relayhat2"
from time import sleep
from turta_relayhat2 import Turta_Relay
relay = Turta_Relay.RelayController(leds_state = True)
#Toggle relays 1 to 5
for x in range(1, 6):
print("Toggling relay", x)
#Turn LEDs off
print("Setting LEDs off")
#Turn LEDs on
print("Setting LEDs on")
#Turn off all relays
print("Turn off all relays")
When you run the sample, the relays invert their states sequentially. Then, it turns off all indicator LEDs, while relays are on. Then, the sample code turns indicator LEDs on and turns off all the relays. The example output should be like this:
Toggling relay 1
Toggling relay 2
Toggling relay 3
Toggling relay 4
Toggling relay 5
Setting LEDs off
Setting LEDs on
Turn off all relays
To stop the Python script, just press CTRL + C.