RPi is working, so what to do now? Some hardware hacking! I want my RPi to detect when somebody came into room and to do something (for example fire an flame-thrower on the intruder). So let’s try it.
Raspberry Pi is equipped with expansion connector – 2×13 pins with standard 1/10″ spacing. Connector is described on RPi wiki.
There is one I2C and one SPI bus on connector – it will be tested later. 5V, 3.3V and GND connection. And an 8 GPIO ports.
It is lower nuber of GPIO than on any Arduino, I know. There is no analog input/output, also. But I don’t like writing code in low-level languages like on Arduino, and I want to use user-friendly and powerfull platform. RPi is much better for me.
GPIO ports on RPi are 3.3 volts and are not 5 volt compatible. If you connect them to 5V signal, your RPi will be dead instantly. So be carefull.
All GPIO ports are available as files under /sys filesystem. But you have to be root for using them – and this is bad. I don’t want running my code under root account, it is a bad behaviour.
There is a solution. People from quick2wire.com released some software solving this trouble. You can „open“ GPIO pin by using
gpio-admin export 22
then you can read from port
or even write to the port
echo out > /sys/devices/virtual/gpio/gpio22/direction
echo 1 > /sys/devices/virtual/gpio/gpio22/value
directly from the shell.
Driving a LED from RPi
It’s easy. I’ve bought a LED with 2 mA driving current – so low so I can connect it directly to RPi. Connected it with a 680 ohm resistor (i fact two serial connected 330 ohm resistor) between GPIO port 24 and a GND. Then I tried:
gpio-admin export 24
echo out > /sys/devices/virtual/gpio/gpio24/direction
echo 1 > /sys/devices/virtual/gpio/gpio24/value
and LED starts shining.
Using a PIR detector with RPi
PIR stands for „passive infrared“. It’s a basic security/surveillance device – it can detect movement in indoor area by detecting changes in infrared radiation.
For a less than 6 USD you can buy a cheap PIR detector from china e-shop with free shipping to EU. Delivery time was about 10 days – better than some czech e-shops…
It is powered by 5 V (available on RPi expansion connector). Output signal is 0 V when nothing happens, and 3.3 V when movement is detected – so it is compatible with RPi GPIO pin with no conversion need.
So I connected it to 5 V, GND and GPIO 21. And wrote some shell script, which:
- waits for a „0“ signal on GPIO 21 – at start, we will wait for „iddle“ status, no movement detected
- executes an infinite loop:
- waits for a „1“ signal on GPIO 21
- display time and a message on console – „VETRELEC“ (means „intruder“ in czech lng)
- sends „1“ to GPIO 24 – so LED is switched ON and shining
- waits for end of a alarm – a „0“ signal on GPIO 21
- display time and a message on console – „Konec poplachu“ (means „end of alarm“)
- sends „0“ to GPIO 24 – so LED is switched off
Started the script and … wait for it … it works. When somebody entered the room, message is printed and LED is switched ON. No movement in room -> LED is black. I’m so happy!
Next time to try: Connect to the Lego Mindstorms NXT (robotics set from Lego) with Raspberry Pi.
- http://elinux.org/Rpi_Low-level_peripherals#General_Purpose_Input.2FOutput_.28GPIO.29 – GPIO port schema
- http://elinux.org/Rpi_Low-level_peripherals#GPIO_Driving_Example_.28Shell_script.29 – how to use GPIO from shell script
- http://quick2wire.com/2012/05/safe-controlled-access-to-gpio-on-the-raspberry-pi/ – tools for working with GPIO without need to be root
- http://www.goodluckbuy.com/dc5v-20v-pyroelectric-infrared-pir-motion-sensor-detector-module-tdc-718.html – PIR detector