IO Port

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

Python Library

IOPort library is responsible for reading analog inputs and controlling GPIO pins on the IO Ports.

To install the library from the (PyPI) Python Package Index, enter the following command to the console.

pip install turta-iothat

Add the following statement to the top of your Python code.

from turta_iothat import Turta_IOPort

Then, create an instance of the Turta_IOPort class.

io = Turta_IOPort.IOPort(d1In, d2In, d3In, d4In)

Now you're ready to access the library by calling the io 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 Digital IO

Sets the digital output state.

set_digital(ch, st)
  • Parameters

    • Int ch: IO channel. 1 to 4.

    • Bool st: Output state.

  • Returns

    • None

Read Digital IO

Reads the digital input.

bool read_digital(ch)
  • Parameters

    • Int ch: IO channel. 1 to 4.

  • Returns

    • Bool: Input state.

Read Analog Input

Reads the analog input.

There is a known issue on reading analog inputs with Python. As a result, the analog readouts may not be consistent. This is a software related issue and we'll publish the updated software when the problem is resolved.

float read_analog(ch)
  • Parameters

    • Int ch: Analog input channel. 1 to 4.

  • Returns

    • Float: Analog input value. 0.0 to 1.0.

Samples

You can copy the example code from https://github.com/Turta-io/IoTHAT/tree/master/Samples/Raspbian/Python address, and then copy it to the Raspberry Pi. There are two examples of this library.

IO Port Digital Sample App

This application demonstrates digital read and write functions.

To run the example:

  1. Copy the library and sample code to a folder on the Raspberry Pi.

  2. Open terminal, and go to the folder you copied the codes.

  3. Run the sample with the following command:

python IOPortDigitalSampleApp.py

Sample Code

IOPortDigitalSampleApp.py
import time
from turta_iothat import Turta_IOPort
#Install IoT HAT library with "pip install turta-iothat"

#Initialize
#IO Ports 1 and 2 are set to be inputs
#IO Ports 3 and 4 are set to be outputs
io = Turta_IOPort.IOPort(True, True, False, False)

try:
    while 1:
        #Toggle IO pin 3
        io.set_digital(3, not io.read_digital(3))

        #Print IO pin 3's state
        print(io.read_digital(3))

        #Delay 5 seconds
        time.sleep(5.0)

#Exit on CTRL+C
except KeyboardInterrupt:
    print('Bye.')

Result

When you run the sample, it toggles digital IO pin 3 every 5 seconds. The application also writes the IO state to the terminal. The example output should be like this:

True
False

To stop the Python script, just press CTRL + C.

IO Port Analog Sample App

This application demonstrates analog read function.

To run the example:

  1. Copy the library and sample code to a folder on the Raspberry Pi.

  2. Open terminal, and go to the folder you copied the codes.

  3. Run the sample with the following command:

python IOPortAnalogSampleApp.py

Sample Code

IOPortAnalogSampleApp.py
import time
from turta_iothat import Turta_IOPort
#Install IoT HAT library with "pip install turta-iothat"

#Initialize
io = Turta_IOPort.IOPort(True, True, True, True)

try:
    while 1:
        #Read analog input 1
        print(io.read_analog(1))
        
        #Delay 5 seconds
        time.sleep(5.0)

#Exit on CTRL+C
except KeyboardInterrupt:
    print('Bye.')

Result

When you run the sample, it reads the analog input channel 1 and writes the analog value to the terminal. The example output should be like this:

0.23456789

To stop the Python script, just press CTRL + C.

Last updated