Programmable LEDs (Basic)

Having setup an LED to be permanently lit via the 3.3V GPIO pin, the next step is to get the LED to light on demand, via a GPIO control pin. There are several pins to choose from, I used pin 17. It’s a similar circuit to the previous example, except we move the anode wire from the 3.3V pin to pin 17.

Once you’ve got the circuit wired up, we need to switch over to the software in order to get the LED to light up. There are many different languages and libraries you can use, but as this is the basic installment, we’re going to use some pre-made libraries – WiringPi and RPi.GPIO. WiringPi is for shell access and RPi.GPIO is for Python access.

To control the LED by the command line, type the following into the shell:

gpio -g mode 17 out
gpio -g write 17 1

This will set the pin to output mode and then turn the LED on, and to turn it off again you can type the following:

gpio -g write 17 0

To control the LED by Python, you need to write a little script:

import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.HIGH)
time.sleep(5)
GPIO.output(17, GPIO.LOW)

This will turn the LED on for 5 seconds, then turn it off again before exiting the script. Like I say, all very basic stuff, but we can expand on this from here.

Advertisements
This entry was posted in Circuits, GPIO, LEDs, Raspberry Pi. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s