Start by running the code in the chapter07/optocoupler_test.py file, and observe the LED blink. Following is the part of the code responsible for the blinking:
# ... truncated ...
pi.write(GPIO_PIN, pigpio.LOW) # On. # (1)
print("On")
sleep(2)
pi.write(GPIO_PIN, pigpio.HIGH) # Off. # (2)
print("Off")
sleep(2)
# ... truncated ...
Here's what's happening:
- At line (1), GPIO 21 is low and the internal LED on the input side is on. The phototransistor on the output side detects this light and is activated, allowing current to flow between the output side's collector (pin 4) and emitter (pin 3), and hence our red LED illuminates.
- The input side of the PC817 circuit is wired as active low—that's why at line (1), GPIO 21 is made low to turn the circuit on, and at line (2), GPIO 21 is set to high to turn the circuit off. Alternative wiring would be active high. If you want to experiment...