Controlling components on a separate power supply from the Raspberry Pi. This project uses a PIR sensor, an IR range sensor, a solenoid, a reception bell and some bright LEDs to form a proximity alarm system.
The first video is the main event. The IR range sensor is set to ring the bell when it detects an object within 40 cm. In the second video you can see the PIR which switches on the bright LEDs when it detects motion nearby. The idea is that as you approach the lights come on and if you get really close the bell rings.
Below is a breadboard diagram and photos. There are a few things to note:
- I'm using a split rail breadboard which means I can keep the 3.3v from the Raspberry Pi separated from the 5v from the breadboard power supply. You'll notice that the ground is common which I have achieved by bridging over the split on the ground rail.
- I've used a rectifier diode on the fet for the solenoid to avoid reverse spike problems.
- I adapted this Arduino PIR guide for the Raspberry Pi
The mcp3008.py unit is for talking to the analog to digital chip with the same name. I have included routines in here now for reading the raw 10-bit number, a voltage relative to 3.3V and the distance in cm from the range sensor. Read these articles for more info: Raspberry Pi hardware SPI analog inputs using the MCP3008 , Raspberry Pi distance measuring sensor with LCD output.
Bell.py is a very simple program to read the status from the sensors and activate the bell or LEDs accordingly. The main loop sleeps for 0.1 seconds each iteration so we're checking the sensor status about 10 times a second which is fast enough. Once an alarm has been activated it will not be activated again for about 3 seconds. This has two benefits. Firstly it stops the bell constantly ringing when you're within 40cm. Secondly the PIR sensor takes a couple of seconds to settle after each motion detection. During this period the sensor fluctuates between low and high so if you just have the LEDs triggered directly from the True/False state of the GPIO pin then you'll have flickering lights. An important difference in the code is that we switch the LEDs on when motion is detected and leave them on for the 3 second window. For the solenoid though, we set it high to hit the bell but then on the next iteration 0.1 seconds later we set it low again to retract the pin. This gives a nice "flick" motion onto the bell pusher and it avoids everything getting hot!
Just before you look at the code I wanted to promote an interesting project that a colleague is working on: The PiXi board.
The PiXi-200 is designed to expand the I/O capabilities of the Raspberry Pi and provide a low-cost means of introducing the user to the world of digital electronics and FPGA technology while at the same time give the 'Pi enthusiast' a little more to play with or even provide a basis for some product development.
I'm looking forward to getting my hands on this but the project needs some support to get off the ground. So please head on over to Astro Designs and register your interest.
Here's the code: