Let me start by saying I am not an expert nor an engineer.
I received my adafruit start pack and proceeded through the tutorials with the parts I had available.
I made this system that would turn on the green led when there was ample light, blue for the transition between light and dark, and red for when there is minimal or no light. The reason for doing this? I had no other sensors to connect at the time.
So, let’s begin.
I assume you already have the Pi Cobbler inserted in the breadboard.
Warning: Don’t have your Raspberry Pi connected to the breadboard or don’t have power to your Raspberry Pi. We really don’t want to fry something while setting things up.
- Connect the GND pin (ground) to the blue power rails (-ve).
- Connect the 3V3 pin (3.3 Volts) to the red power rails (+ve).
- Use jumper wires to relocate pins #23, #24, and #25 to individual and easily accessible rows on the breadboard.
- Add resistors (any values from 330 ohm up to 1000 ohm should be fine) to each of the jumper wire rows, connecting to the other corresponding side.
- Connect your LEDs where the short leg (-ve) connects to the blue rail and the long leg (+ve) connects to the same row as an inserted resistor. (On my board, the Green LED is on #23 and row 24, Blue LED is #24 and row 27, and Red LED is #25 and row 30).
- Connect a jumper wire from pin #22 to an available and accessible row.
- Connect the 1uF capacitor’s -ve leg to the blue rail and the other to the row with the newly attached jumper wire for pin #22.
- Connect one side of the photocell to pin #22 and the other to the red power rail (3V3).
The overhead shot of my setup:
Now to program the Raspberry Pi. The OS I used was adafruit’s occidentalis.
Getting the necessary packages: (If you already have them you may skip this part)
$ sudo apt-get install python-dev $ sudo easy_install rpi.gpio
Once those packages are installed we can write the program.
$ sudo vi ambient_warning.py #!/usr/bin/env python import RPi.GPIO as GPIO, time, os # The levels required changing based on the type of light I was in. # In sunlight, the photocell readings ranged from 15 to 105. In # artificial light the readings were much higher. # Artificial light #LEVEL1 = 160 #LEVEL2 = 300 # Sunlight LEVEL1 = 30 LEVEL2 = 60 DEBUG = 1 GPIO.setmode(GPIO.BCM) GREEN_LED = 23 BLUE_LED = 24 RED_LED = 25 GPIO.setup(GREEN_LED, GPIO.OUT) GPIO.setup(BLUE_LED, GPIO.OUT) GPIO.setup(RED_LED, GPIO.OUT) def RCtime (RCpin): reading = 0 GPIO.setup(RCpin, GPIO.OUT) GPIO.output(RCpin, GPIO.LOW) time.sleep(0.1) GPIO.setup(RCpin, GPIO.IN) while (GPIO.input(RCpin) == GPIO.LOW): reading += 1 return reading def ResetWarningLights (): GPIO.output(GREEN_LED, False) GPIO.output(BLUE_LED, False) GPIO.output(RED_LED, False) def SetWarningLight (level) : ResetWarningLights() if (level <= LEVEL1) : GPIO.output(GREEN_LED, True) elif (level <= LEVEL2): GPIO.output(BLUE_LED, True) else: GPIO.output(RED_LED, True) ResetWarningLights() while True: level = RCtime(22) # print level SetWarningLight(level)
Save your file, double check your board layout, connect the raspberry pi to the breadboard, and run your program.
$ sudo ./ambient_warning.py
If everything is correct you should have an LED on based on the amount of ambient light.