Relay

This documentation includes installation guidelines and sample code for your hardware.

Python Library

Turta_Relay library is responsible for controlling onboard relays.

Initialization

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.

Advanced Initialization

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.

Stacking Relay HATs

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

Basic Members

Write (Control) Relay

Controls the relay.

write(ch, st)
  • Parameters

    • Byte ch: Relay channel. (1 to 5.)

    • Bool st: Relay state. (True or False.)

  • Returns

    • None

Write Once to Relays

Controls all the relays in one command.

write_once(st)
  • Parameters

    • Byte st: Relay states. (0b00000 to 0b11111.)

  • Returns

    • None

Write (Control) All Relays

Turns on or off all the relays.

write_all(st)
  • Parameters

    • Bool st: Relay states. (True or False.)

  • Returns

    • None

Toggle Relay

Inverts the relay's state.

toggle(ch)
  • Parameters

    • Byte ch: Relay channel. (1 to 5.)

  • Returns

    • None

Toggle All

Inverts all the relay states.

toggle_all()
  • Parameters

    • None

  • Returns

    • None

Read Relay

Reads the relay state.

read(ch)
  • Parameters

    • Byte ch: Relay channel. (1 to 5.)

  • Returns

    • Bool: Relay state. (True or False.)

Read All Relay States

Reads all the relay states.

read_all()
  • Parameters

    • None

  • Returns

    • Byte: Relay states. (0b00000 to 0b11111.)

Advanced Members

Set Indicator LEDs

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

Finalizer

When exiting from the program, the library sets all the relays off if preserve state setting is not enabled.