Relay Preserve State Sample
This sample demonstrates state preserve functionality of the Relay HAT.
- 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 state preserve functionality of the Relay HAT.
#Install Relay HAT 2 library with "pip3 install turta-relayhat2"
from time import sleep
from turta_relayhat2 import Turta_Relay
relay = Turta_Relay.RelayController(preserve_state = True)
#Read and increment the current relay state
state = relay.read_all() + 1
if (state == 32):
state = 0
#Set the new relay state
print("Relay state is set")
When you run the sample, the relay states are changed every two seconds. The example output should be like this:
Relay state is set
To stop the Python script, just press CTRL + C.