I have been using the Arduino for a few years now and recently curiosity got the better of me with the excitement around the Raspberry Pi. I purchased my first Raspberry Pi and since my interest is more in line with interacting with the physical than just having a small computer, I needed a second project. Of course, the first project was to blink an LED!
For my second project, I decided to expand on the first. I still use output to blink an LED, but now I also use input to decide when to blink the LED. The way this project works is by sensing motion using a PIR, Passive Infra-Red, sensor.
Using the Adafruit Raspberry Pi Cobbler, here’s how I connected the Raspberry Pi GPIO pins to the breadboard and wired up the PIR sensor and LED
Here’s the code!
import RPi.GPIO as GPIO import time PIR = 23 LED = 24 pirState = False # we start, assuming no motion detected pirVal = False # we start, assuming no motion detected GPIO.setmode(GPIO.BCM) GPIO.setup(PIR, GPIO.IN) GPIO.setup(LED, GPIO.OUT) while True: pirVal = GPIO.input(PIR) # read input value if (pirVal == True): # check if the input is HIGH GPIO.output(LED, True) # turn LED ON if (pirState == False): # we have _just_ turned on pirState = True else: GPIO.output(LED, False) # turn LED OFF if (pirState == True): # we have _just_ turned off time.sleep(2) pirState = False;
Parts List / BOM (Bill of Materials) – Excluding Raspberry PI
|1/2 Size breadboard||Adafruit||$5.00|
|Pi Cobbler Breakout Kit for Raspberry Pi||Adafruit||$7.95|
|PIR (motion) sensor||Adafruit||$10.00|
|220 ohm resistor||RadioShack||$1.19|
* = Prices are the best information from the time this tutorial was written. Prices do not include