PC Loop Sample

This sample demonstrates reading input states of photocoupler.

To run the sample:

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

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

  3. Run the sample with the following command:

python3 Photocoupler_Loop.py

Sample Code

#!/usr/bin/env python3

#This sample demonstrates reading input states of photocoupler.
#Install IoT HAT 3 library with "pip3 install turta-iothat3"

from time import sleep
from turta_iothat3 import Turta_Photocoupler

photocoupler = Turta_Photocoupler.PhotocouplerInputs()

    while True:
        #Read photocoupler input states
        pc1 = photocoupler.read(1)
        pc2 = photocoupler.read(2)

        #Print the readings
        print("Photocoupler 1..: " + ("High." if pc1 else "Low."))
        print("Photocoupler 2..: " + ("High." if pc2 else "Low."))


#Exit on CTRL+C
except KeyboardInterrupt:


When you run the sample, it prints photocoupler input states continuously. The example output should be like this:

Photocoupler 1..: Low.
Photocoupler 2..: Low.
Photocoupler 1..: High.
Photocoupler 2..: Low.

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

Last updated