In order to get into some hardware related projects with the RASPI, I figured it would be a good idea to figure out how to do some very basic stuff with the GPIO.
The first project I think anyone does is make the LEDs light up. There are plenty of tutorials out there with bits of information scattered about, but I'll try to use my experiences to supply all the information (and references to information) here.
First of all, it is a good idea to know what each pin does on the Raspberry Pi GPIO and get your Pi all ready and set up to work with Python.
Here is a great tutorial via adafruit that gives you a quick and dirty overview the GPIO, steps to take and libraries to install in order to program it via Python.
Once you have that all set up, in order to make an LED light up programmatically, you need to have it wired properly between a resistor that is connected to one of the numbered GPIO pins (4, 8, 7, 17, 22, 23, 24 or 25), and the ground pin (GND).
I wired my LEDs using the adafruit Pi Cobbler and a breadboard, but you could attach wires directly to the GPIO if so inclined.
Here is the example I followed for connecting LEDs to the Pi's GPIO.
And here is mine wired up to three LEDs:
Here is a handy resistor decoder I use now and then.
In the above picture, the black wire is contacted between the GND pin and the negative blue column on the bread board.
Here is a diagram (note the totally fake cobbler)
For the red LED, the resistor is connected to GPIO pin 23 and an unused row on the breadboard (row 18..chosen arbitrarily). The positive end of the LED (long wire) is also in row 18 and the negative end (short wire) is connected the the blue negative column where the ground wire is connected.
I did the same for the other two LEDs but with their respective GPIO ports. Green was further away from the GPIO pin, so I used the yellow and green jumper wires to make a similar circuit.
Once everything was connected, I wrote a simple Python program to cycle through the LEDs. Somehow it worked!
Here is the code, and then some 'splainin':
GREEN_LED = 24
24 is the GPIO pin. I don't like to hard code values and plus it makes code more readable/maintainable when you do things this way.
GPIO.setup(GREEN_LED, GPIO.OUT)
Initialize the GPIO pin used for the green LED for output
GPIO.output(GREEN_LED, True)
Turn the LED on
GPIO.output(GREEN_LED, False)
Turn the LED off
GPIO.cleanup()
Uninitialize all the GPIO pins
And there you have it!
Here is a little Python code that pings an IP address. Once an IP address pings back, an LED is turned on. In this example, when one of three iPhones is detected on my home network, an associated LED will light up. The light will turn back off if the IP address becomes unreachable.
The first project I think anyone does is make the LEDs light up. There are plenty of tutorials out there with bits of information scattered about, but I'll try to use my experiences to supply all the information (and references to information) here.
First of all, it is a good idea to know what each pin does on the Raspberry Pi GPIO and get your Pi all ready and set up to work with Python.
Here is a great tutorial via adafruit that gives you a quick and dirty overview the GPIO, steps to take and libraries to install in order to program it via Python.
Once you have that all set up, in order to make an LED light up programmatically, you need to have it wired properly between a resistor that is connected to one of the numbered GPIO pins (4, 8, 7, 17, 22, 23, 24 or 25), and the ground pin (GND).
I wired my LEDs using the adafruit Pi Cobbler and a breadboard, but you could attach wires directly to the GPIO if so inclined.
Here is the example I followed for connecting LEDs to the Pi's GPIO.
And here is mine wired up to three LEDs:
In this example I have my LEDs wired up to GPIO ports:
- GREEN_LED = 24
- RED_LED = 23
- BLUE_LED = 18
Here is a handy resistor decoder I use now and then.
In the above picture, the black wire is contacted between the GND pin and the negative blue column on the bread board.
Here is a diagram (note the totally fake cobbler)
For the red LED, the resistor is connected to GPIO pin 23 and an unused row on the breadboard (row 18..chosen arbitrarily). The positive end of the LED (long wire) is also in row 18 and the negative end (short wire) is connected the the blue negative column where the ground wire is connected.
I did the same for the other two LEDs but with their respective GPIO ports. Green was further away from the GPIO pin, so I used the yellow and green jumper wires to make a similar circuit.
Once everything was connected, I wrote a simple Python program to cycle through the LEDs. Somehow it worked!
Here is the code, and then some 'splainin':
#!/usr/bin/env python
import RPi.GPIO as GPIO, time, os
SLEEP_TIME = 1
GPIO.setmode(GPIO.BCM)
GREEN_LED = 24
RED_LED = 23
BLUE_LED = 18
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(RED_LED, GPIO.OUT)
GPIO.setup(BLUE_LED, GPIO.OUT)
try:
while True:
print "Red Light"
GPIO.output(GREEN_LED, False)
GPIO.output(RED_LED, True)
GPIO.output(BLUE_LED, False)
time.sleep(SLEEP_TIME)
print "Blue Light"
GPIO.output(RED_LED, False)
GPIO.output(BLUE_LED, True)
GPIO.output(GREEN_LED, False)
time.sleep(SLEEP_TIME)
print "Green Light"
GPIO.output(BLUE_LED, False)
GPIO.output(GREEN_LED, True)
GPIO.output(RED_LED, False)
time.sleep(SLEEP_TIME)
except KeyboardInterrupt:
GPIO.cleanup()
Break it on down:GREEN_LED = 24
24 is the GPIO pin. I don't like to hard code values and plus it makes code more readable/maintainable when you do things this way.
GPIO.setup(GREEN_LED, GPIO.OUT)
Initialize the GPIO pin used for the green LED for output
GPIO.output(GREEN_LED, True)
Turn the LED on
GPIO.output(GREEN_LED, False)
Turn the LED off
GPIO.cleanup()
Uninitialize all the GPIO pins
And there you have it!
Here is a little Python code that pings an IP address. Once an IP address pings back, an LED is turned on. In this example, when one of three iPhones is detected on my home network, an associated LED will light up. The light will turn back off if the IP address becomes unreachable.
#!/usr/bin/env python # This program will detect specific IP address on the network and # Enable an LED if it is detected import RPi.GPIO as GPIO, time, os from subprocess import call SLEEP_TIME = 1 host1 = "192.168.1.128" # my iPhone host2 = "192.168.1.108" # wife's iphone host3 = "192.168.1.186" # an iPad GPIO.setmode(GPIO.BCM) GREEN_LED = 24 RED_LED = 23 BLUE_LED = 18 GPIO.setup(GREEN_LED, GPIO.OUT) GPIO.setup(RED_LED, GPIO.OUT) GPIO.setup(BLUE_LED, GPIO.OUT) def detect_IP(ip_add, color_led): ping_command = "ping -c 1 %s > /dev/null 2>&1" % ip_add if call(ping_command, shell=True) > 0: GPIO.output(color_led, False) else: GPIO.output(color_led, True) try: while True: detect_IP(host1, GREEN_LED) detect_IP(host2, RED_LED) detect_IP(host3, BLUE_LED) time.sleep(SLEEP_TIME) except KeyboardInterrupt: GPIO.cleanup()
I know this is an old post, but I just happened to stumble across it. I'm new to the Pi scene. I followed your examples, and they work great.
ReplyDeleteWhat I'm trying to do is set the Pi up with 2 reed sensors for 2 doors. If the doors are closed, the LEDs turn on. If they are open, the LEDs are off.
What would be the best way to go about programming? In your example, you are using a yellow and green wire to extend the connections. I would imagine the two wires on each reed sensor would replace those wires.
This is just a fabulous blog as I was looking for right information on Wholesale Store for Cell Phone Accessories and found your blog really awesome. It is true that IPhone7 plus Light up Case is a nice way to have a cut on the cost. There are a lot of companies that are selling Cell Phone Accessories Wholesale USA, but finding one reliable is really hard. I found your website via Google while searching for a Cell Accessories Wholesale, your website came up, it looks great. Thanks for your article and keep publishing more informative content!!
ReplyDelete