Jump to content
 
  • entries
    7
  • comments
    6
  • views
    11,210

3. Adding a second LED


DavidB-AU

635 views

Now let's add a green LED adjacent to the red one and connect it to GPIO25 (pin 22).

blogentry-6959-0-27233700-1359209380.png

 

Now create a new file called ledtest2.py and enter the following:

import RPi.GPIO as GPIOimport time# Set up GPIO25 (pin 22) as outputGPIO.setup(22, GPIO.OUT)while 1:  # Turn green LED on  GPIO.output(22, False)  time.sleep(1)  # Turn green LED off  GPIO.output(22, True)  time.sleep(1)

 

Run the program as root.

pi@raspberrypi ~$ sudo python ledtest2.py

 

If all goes well, the green LED will flash on and off. Quit the program by hitting CTRL-C.

 

Now let's get a little more complicated. Create a new file called ledtest3.py and enter the following:

import RPi.GPIO as GPIOimport time# Set up pins 18 (GPIO24) and 22 (GPIO25) as outputGPIO.setup(18, GPIO.OUT)GPIO.setup(22, GPIO.OUT)while 1:  GPIO.output(18, False)   # Turn red LED on  GPIO.output(22, True)    # Turn green LED off  time.sleep(1)  GPIO.output(18, True)    # Turn red LED off  GPIO.output(22, False)   # Turn green LED on  time.sleep(1)

 

Run the program.

pi@raspberrypi ~$ sudo python ledtest3.py

 

The LEDs should now flash between red and green.

 

blogentry-6959-0-92300500-1359211142.png

  • Like 1

0 Comments


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...