Jump to content
  • entries
  • comments
  • views

2. Driving an LED




Now let's set up a simple circuit with a resistor and a red LED. The resistor is between the 3.3V rail and the anode (long leg) of the LED. The cathode of the LED is connected to GPIO24 (pin 18).



The value of the resistor isn't critical but should be in the range of 300R-1K. I used 470R because I had plenty to hand. This is what it looks like on the breadboard.



Now to control the LED from the RPi. GPIO must be run as root so after the prompt (pi@raspberrypi ~$) type sudo python.

pi@raspberrypi ~$ sudo python


python will start and display something like this. The >>> is the prompt.

Python 2.7.3 (default, Jan 26 2013, 11:20:46)[GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>

First import the GPIO package.

>>> import RPi.GPIO as GPIO

Then set up pin 18 (GPIO24) as output.

>>> GPIO.setup(18, GPIO.OUT)

By default the GPIO pins are HIGH (3.3V). Bu setting them LOW, it drops to 0V and allows power to flow. To do this, set the pin to "False". (This is not the only way to do this. More on this later.)

>>> GPIO.output(18, False)

This will turn the LED on. To it off, set the pin as HIGH by making the setting "True".

>>> GPIO.output(18, True)

You can repeat these GPIO.output settings until you get bored. :)


Exit the python shell by hitting CTRL-D.


Controlling the LEDs by typing python commands every time will get tedious, so instead we'll write a little program to automate it. Open a new file called ledtest1.py with your favourite text editor. I use nano (long time users of Linux may recognise this as a cut down version of pico) so the command is:

pi@raspberrypi ~$ nano ledtest1.py


Add the following. The items after the hash (#) are comments which are not processed and can be left out if desired. However it is good programming practice to ad comments so you remember what each command or block of commands is intended to do.

import RPi.GPIO as GPIO    # Imports the GPIO packageimport time                # Imports a timer packageGPIO.setup(18, GPIO.OUT)   # Set up header pin 18 (GPIO24) as outputwhile 1:                   # Starts a loop  GPIO.output(18, False)   # Turns the LED on  time.sleep(1)            # Waits for 1 second  GPIO.output(18, True)    # Turns the LED off  time.sleep(1)            # Waits for 1 second


Save the file and close the editor.


Again, GPIO must be run as root so from the Linux command line type:

pi@raspberrypi ~$ sudo python ledtest1.py

The LED should now flash on and off, changing about every 1 second. It may not be exactly 1 second as the RPi is doing other things in the background which may affect the timer.


Quit the program by hitting CTRL-C.



  • Like 1


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...