I am trying to help some 8th graders build a robot. They learned about the Raspberry Pi and Python with GPIOZERO in an earlier class.
They have wheels spinning using the Motor class from GPIOZERO and now want to measure speed and distance. They have optical encoders on the inner shafts of the geared motors and infrared optical sensors for each motor. The sensor have Vcc, ground, and output. With Vcc connected to 3v3, the LED on the sensor lights when there is no interruption of the infrared beam and is out if the beam is interrupted. I conjecture that output is about 3.3 volts when the light is on and probably floating when it's off.
There ought to be a way to read this with GPIOZERO, but I've been unable to figure it out. Any help would be much appreciated.
Edit: Let me try to make this a little less vague. The kids have no-name (and no documentation) sensors that look like this:
If I connect Vcc to 3v3, GND to GND and OUT to GPIO 27, the following code works, i.e. tells whether the gap in the sensor is open or occluded.
import RPi.GPIO as GPIO
from time import sleep
pin = 27
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
if GPIO.input(pin) == GPIO.HIGH:
My goal is to get the same result using the GPIOZERO library.
(I should probably put a current-limiting resistor between "OUT" and the GPIO pin, and I'll get the kids to do that.)