In this example we are going to connect to infrared obstacle reflection photoelectric sensor motion detection sensor.
I am not going to deny it, I find this sensor to be awful rubbish. Without reading the data you can hook it up and see it when the 2nd green lights goes off then its sensing something. Problem is that it usually does not sense something when you expect it to, and if you look on the net then you will see others have same stories, and that it gets even worse in sunlight.
Even fiddling with the sensitivity screw on it did not make it give detections at times when I actually wanted them.
Good thing is since this sensor can be tested without reading its data then we know this is just not a good sensor and that our reading from it is not to blame.
But we will still cover the wiring since its simple. This one has straight forwards markings and just simple 0 or 1 to poll.
Disclaimer:
We do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged sensor or damaged Raspberry PI.
Pieces we use are:
- Raspberry PI 2
- Cobbler and Cobbler cable
- IR Infrared Obstacle Reflection Photoelectric Sensor sensor (They go as low as $0.90 on Aliexpress where I got this one from probably even lower if you buy more than one unit – though I am not sure anyone would want many pieces of this one).
- Wiring PI Xojo module from Paul Lefebvre
First thing is connecting the sensor:
- (5V) Red is the 5V input (was marked as VCC on the sensor)– I always use red for the power input.
- (GPIOX) Yellow is the Data pin (was marked as OUT on the sensor)
- (GND) Blue is the ground (was marked as GND on the sensor) – I always use blue for the ground.
Connecting to the Cobbler:
Make sure your Raspberry PI is not powered on when you do the connections.
In my example then I have it connected as follows:
- Red to pin 2 (5V)
- Blue to pin 25 (GND)
- Yellow to GPIO 5 which is pin 29.
The Xojo code:
I have a timer to poll the sensor. we set the timer up and the pin mode in the Window Open event.
Sub Open() Const kInfraRedSensorPin = 5 GPIO.SetupGPIO GPIO.PinMode(kInfraRedSensorPin, GPIO.INPUT) Timer1.Mode = Timer.ModeMultiple Timer1.Enabled = true End Sub
In the Timer.Action event I got the following code:
(If you want it more responsive then you can increase the cycles of the for loop)
Sub Action() Dim i as Integer Const kInfraRedSensorPin = 5 for n as Integer = 1 to 10 i = GPIO.DigitalRead(kInfraRedSensorPin) if i <> 0 then Listbox1.AddRow “Motion detected” return end if App.DoEvents(1) next End Sub
Thats all there is to it!