My Account

Wish List (0)

Saved Carts

Compare

Approved Re-Seller
Follow us on Facebook! Follow us on Twitter! Follow us on Instagram!

GPIO & Python (2/9) - LEDs

February 21, 2017

In this project you’ll learn how to turn on and off a LED

Things you will need:

Raspberry Pi + SD Card
Keyboard + Mouse
Monitor + HDMI Cable
Power Supply
Breadboard
1x Red LED
1x Blue LED
2x 330Ω Resistor
3x M/F Jumper Wire

Prerequisites:

Latest version of Rasbian installed on your SD Card
Raspberry Pi setup with a keyboard, mouse and monitor

1. Change the current directory to our gpio_python_code directory:

cd gpio_python_code 

2. Start by creating a file for our led on script

touch 2_on.py

3. Then create a file for our led off script

touch 2_off.py

4. Edit the 2_on.py file using nano 2_on.py and add the following code

#!/usr/bin/python

import RPi.GPIO as GPIO # import a library

GPIO.setmode(GPIO.BCM) # set the pin numbering system to BCM

GPIO.setup(17,GPIO.OUT) # set GPIO17 as an OUTPUT
GPIO.setup(27,GPIO.OUT) # set GPIO27 as am OUTPUT

print "Lights on" # print to the screen so we know what’s going on

GPIO.output(17,GPIO.HIGH) # set GPIO17 high, 3v3 will now be active on that pin
GPIO.output(27,GPIO.HIGH) # set GPIO27 high, 3v3 will now be active on that pin

5. Exit and save the file, CTRL+x, Y, enter

6. Edit the 2_off.py file using nano 2_off.py and add the following code

#!/usr/bin/python

import RPi.GPIO as GPIO # import a library

GPIO.setmode(GPIO.BCM) # set the pin numbering system to BCM

GPIO.setup(17,GPIO.OUT) # set GPIO17 as an OUTPUT
GPIO.setup(27,GPIO.OUT) # set GPIO27 as am OUTPUT

print "Lights off" # print to the screen so we know what’s going on

GPIO.output(17,GPIO.LOW) # set GPIO17 low, 3v3 will now be de-active on that pin
GPIO.output(27,GPIO.LOW) # set GPIO27 low, 3v3 will now be de-active on that pin

7. Exit and save the file, CTRL+x, Y, enter

8. To turn your LEDs on, execute your 2_on.py script:

sudo python 2_on.py

You might see some warnings, for the sake if this tutorial you can ignore these.
They are just letting you know that the GPIO pins used in the script, may have already
been configured.

8. To turn your LEDs off, execute your 2_off.py script:

sudo python 2_off.py

You’ll probably see some warnings, for the sake if this tutorial you can ignore these.
They are just letting you know that the GPIO pins used in the script, may have already
been configured.

Last update: February 22, 2017

Comments

Leave a Comment

Leave a Reply

* Name:
* Comment:
* E-mail: (Not Published)
Website: (Site url with http://)
Verification code: