GPIO and Python (3/9) - Blinking LED
In this project you’ll learn how to make a LED blink!
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 blink led script
touch 3_blink.py
3. Create a file for our blink led forever script
touch 3_blink_forever.py
4 . Edit the 3_blink.py script using nano 3_blink.py add the following code:
#!/usr/bin/python from time import sleep # import the time function from the sleep library import RPi.GPIO as GPIO # import our GPIO library GPIO.setmode(GPIO.BCM) # set the board numbering system to BCM # setup our output pins GPIO.setup(17,GPIO.OUT) GPIO.setup(27,GPIO.OUT) # Turn LEDs on print “lights on” GPIO.output(17,GPIO.HIGH) GPIO.output(27,GPIO.HIGH) sleep(1) # sleep for 1 second # Turn LEDs off print “lights off” GPIO.output(17,GPIO.LOW) GPIO.output(27,GPIO.LOW) sleep(1) # Turn LEDs on print “lights on” GPIO.output(17,GPIO.HIGH) GPIO.output(27,GPIO.HIGH) sleep(1) # Turn LEDs off print “lights off” GPIO.output(17,GPIO.LOW) GPIO.output(27,GPIO.LOW) GPIO.cleanup() # the clean-up function will reset all the configurations made in this script. This will stop the warnings we got from the tutorial 2.
5. Edit the 3_blink_forever.py script using nano 3_blink_forever.py add the following code:
#!/usr/bin/python # import libraries from time import sleep import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # set pin numbering system to bcm # setup our output pins GPIO.setup(17,GPIO.OUT) GPIO.setup(27,GPIO.OUT) # create an infinite loop while True: # turn leds on print “lights on” GPIO.output(17,GPIO.HIGH) GPIO.output(27,GPIO.HIGH) sleep(1) # sleep 1 second # turn leds off print “lights off” GPIO.output(17,GPIO.LOW) GPIO.output(27,GPIO.LOW) sleep(1) # sleep 1 second
6. Execute your 3_blink.py script
sudo python 3_blink.py
7. Execute your 3_blink_forever.py script
sudo python 3_blink_forever.py
8. To stop your 3_blink_forever.py script, simply press ctrl+c
1 comment
Nate
needs updating for use of <"> character, as well as new requirement for < () > on print text.
needs updating for use of <"> character, as well as new requirement for < () > on print text.