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

Photocoupler_Loop.py
#!/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

#Initialize
photocoupler = Turta_Photocoupler.PhotocouplerInputs()

try:
    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."))

        #Wait
        print("-----")
        sleep(0.5)

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

Result

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