Raspberry Pi OS

Prerequisites

To get maximum performance from your hardware, please make sure you'll meet the following requirements.

Operating System

We recommend using the latest version of Raspberry Pi OS. Download the latest version from raspberrypi.org/downloads/raspberry-pi-os address.

Python Library

Turta_RelayController library is responsible for controlling onboard relays.
To install the library from the (PyPI) Python Package Index, enter the following command to the console.
1
pip install turta-relayhat
Copied!
Add the following statement to the top of your Python code.
1
from turta_relayhat import Turta_RelayHAT
Copied!
Then, create an instance of the Turta_RelayHAT class.
1
rc = Turta_RelayHAT.RelayHAT()
Copied!
Now you're ready to access the library by calling the rc instance.

Initialization

The library automatically initializes the required components when its instance is being created, so there is no need to do a manual initialization.

Basic Members

Set Relay

Turns the desired relay on or off.
1
set_relay(ch, st)
Copied!
  • Parameters
    • Int ch: Relay channel. 1 to 4.
    • Bool st: Relay state. True or False.
  • Returns
    • None

Read Relay State

Reads the desired relay state.
1
read_relay_state(ch)
Copied!
  • Parameters
    • Int ch: Relay channel. 1 to 4.
  • Returns
    • Bool: Relay state. True or False.

Samples

You can copy the example code from https://github.com/Turta-io/RelayHAT/tree/master/Samples/Raspbian/Python address, and then copy it to the Raspberry Pi. There is one example of this sensor.

Relay Controller Sample App

This application demonstrates relay functions. It turns relays on and off in a loop.
To run the example:
  1. 1.
    Copy the library and sample code to a folder on the Raspberry Pi.
  2. 2.
    Open terminal, and go to the folder you copied the codes.
  3. 3.
    Run the sample with the following command:
1
python RelayControllerSampleApp.py
Copied!
Sample Code
RelayControllerSampleApp.py
1
import time
2
from turta_relayhat import Turta_RelayHAT
3
#Install Relay HAT library with "pip install turta-relayhat"
4
5
#Initialize
6
rc = Turta_RelayHAT.RelayHAT()
7
8
try:
9
while 1:
10
#Toggle relay 1
11
rc.set_relay(1, not rc.read_relay_state(1))
12
print("Relay 1 state: " + ("On" if rc.read_relay_state(1) else "Off"))
13
time.sleep(2.0)
14
15
#Toggle relay 2
16
rc.set_relay(2, not rc.read_relay_state(2))
17
print("Relay 2 state: " + ("On" if rc.read_relay_state(2) else "Off"))
18
time.sleep(2.0)
19
20
#Toggle relay 3
21
rc.set_relay(3, not rc.read_relay_state(3))
22
print("Relay 3 state: " + ("On" if rc.read_relay_state(3) else "Off"))
23
time.sleep(2.0)
24
25
#Toggle relay 4
26
rc.set_relay(4, not rc.read_relay_state(4))
27
print("Relay 4 state: " + ("On" if rc.read_relay_state(4) else "Off"))
28
time.sleep(2.0)
29
30
except KeyboardInterrupt:
31
print('Bye.')
Copied!
Result
When you run the sample, the relays turn on one after another and then turn off in the same order. The application also writes the relay states to the terminal. The example output should be like this:
1
Relay 1 state: On
2
Relay 2 state: On
3
Relay 3 state: On
4
Relay 4 state: On
5
Relay 1 state: Off
6
Relay 2 state: Off
7
Relay 3 state: Off
8
Relay 4 state: Off
Copied!
To stop the Python script, just press CTRL + C.
Last modified 1yr ago