Trying to get an active infrared sensor to trigger an event on a GPIO pin. I'm using python and the RPi.GPIO library on an RPi model B.
Some background info, not sure how relevant it is, but I'm a beginner, so not sure. I apologise in advance if my terminology is off.
- I have two LEDs wired to GPIO pins which show various activity. They are both working fine.
- I have another LED wired directly from GPIO 3.3v pin to GPIO ground to show when power is active, this is also working fine.
- The sensor has a master and slave part. I power the sensor from a 12v adapter, running the 12v and ground cables to the master, and then to the slave, along with another cable between the master and slave for sensor detection. I have measured this comms line (labelled T0) to be 4.53v, and when detection event occurs, this drops slightly to 4.51v.
I've tried two different approaches to getting the sensor to trigger an GPIO event.
- Hooking into the T0 comms line. I did this by placing a few resistors in series to get under 3.3v as advised, and wiring the ground (from the 12v adapter) to a GPIO ground. My python code was looking for a GPIO.FALLING event.
- Hooking into an LED on the master board which was lighting up on detection. This rose to 4.7v when detected and stayed at 0v when not. I desoldered the LED from the PCB and replaced with wire going to GPIO, i.e. the + side of the LED went to the GPIO pin, and the - side of the led when to a GPIO ground. Again I placed some resistors to get it under 3.3v. My python code was looking for a GPIO.RISING event.
In both cases the GPIO event doesn't trigger. I've tried setting GPIO.IN with neither pull_up_down specified, and both GPIO.PUD_UP and GPIO.PUD_DOWN set.
I feel the second approach should be easier to work with, which is what I've focused on, but I just can't get the detection to register on the GPIO. I'm guessing I've gotten my resistor values wrong or perhaps wired the ground wrong?
One possible clue - if I try to use a multimeter to read the output after the resistors, an GPIO event is triggered, but it doesn't trigger if I read it before the resistors.
Any help appreciated, thanks!
Update
A friend recommended the second option was the easiest and suggested I ensure the sensor and pi have a common ground, as well as try something more simple first, specifically, write some python code to poll the GPIO pin and using a GPIO power port to trigger an event. Here's the code:
import RPi.GPIO as GPIO
import time
sensor = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensor, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
current = GPIO.input(sensor)
previous = current
def printState(current):
print 'GPIO pin %s is %s' % (sensor, 'HIGH' if current else 'LOW')
printState(current)
while True:
current = GPIO.input(sensor)
if current != previous:
printState(current)
previous = current
time.sleep(0.1)
GPIO.cleanup()
And this works perfectly! I use a wire connected to 3.3v GPIO pin and touch the sensor pin (GPIO pin 7) and the state changes from initial value of LOW to HIGH and stays HIGH as long as I keep them connected. That confirms port setup is working and my problem something to do with either the grounding, the resistor value or (unlikely) the RISING event.