Relay
This documentation includes installation guidelines and sample code for your hardware.
Turta_Relay library is responsible for controlling onboard relays.
Add the following statement to the top of your Python code.
from turta_relayhat2 import Turta_Relay
Then, create an instance of the RelayController class.
relay = Turta_Relay.RelayController()
Now you're ready to access the library by calling its instance.
There are three advanced initialization parameters.
relay = Turta_Relay.RelayController(addr = 0x20, leds_state = True, preserve_state = False)
Parameters
Byte addr: Device address. 0x20 to 0x27. Default is 0x20. Bool leds_state: True to enable, False to disable indicator LEDs. Default is True. Bool preserve_state: Keeps relay states as they are, even if you reboot the Raspberry Pi. Default is False.
If you're stacking Relay HATs, create an instance for each board.
relay_1 = Turta_Relay.RelayController() # Default 0x20 address
relay_2 = Turta_Relay.RelayController(0x21) # Other board
...
relay_8 = Turta_Relay.RelayController(0x27) # Last board
Controls the relay.
write(ch, st)
Parameters
Byte ch: Relay channel. (1 to 5.) Bool st: Relay state. (True or False.) Returns
None
Controls all the relays in one command.
write_once(st)
Parameters
Byte st: Relay states. (0b00000 to 0b11111.) Returns
None
Turns on or off all the relays.
write_all(st)
Parameters
Bool st: Relay states. (True or False.) Returns
None
Inverts the relay's state.
toggle(ch)
Parameters
Byte ch: Relay channel. (1 to 5.) Returns
None
Inverts all the relay states.
toggle_all()
Parameters
None Returns
None
Reads the relay state.
read(ch)
Parameters
Byte ch: Relay channel. (1 to 5.) Returns
Bool: Relay state. (True or False.)
Reads all the relay states.
read_all()
Parameters
None Returns
Byte: Relay states. (0b00000 to 0b11111.)
Enables or disables the indicator LEDs. Useful if you're stacking a few Relay HATs.
set_leds(st)
Parameters
Bool st: Indicator LEDs state. (True or False.) Returns
None
When exiting from the program, the library sets all the relays off if preserve state setting is not enabled.
Last modified 3yr ago