Tuesday, June 23, 2015

Blinking your first LED

In this tutorial we will blink an LED using Python.

Time: 10 minutes
Difficulty: Easy

You will need:

  • A Raspberry Pi.
  • A way to access its terminal.
  • A breadboard.
  • A 220 ohm resistor.
  • An LED
  • An Internet connection on your Pi.
  • 2 Jumper Wires
Steps:
  1. If you don't have the GPIO library, you'll need to install it by typing sudo apt-get install python-dev python-rpi.gpio. If you get an error message, try typing sudo apt-get update --fix-missing and try again
  2. Wire up your pi exactly as shown. Be sure to do the wiring with the Pi unplugged.
  3. Type sudo nano /home/pi/Desktop/RPiLED.py
  4. Copy and paste the following code into the nano editor. 
  5. import RPi.GPIO as GPIO  import time    
    def blink(pin):  
            GPIO.output(pin,GPIO.HIGH)  
            time.sleep(1)  
            GPIO.output(pin,GPIO.LOW)  
            time.sleep(1)  
            return  
    GPIO.setmode(GPIO.BOARD)    
    GPIO.setup(11, GPIO.OUT)    
    for i in range(0,50):  
            blink(11)  
    GPIO.cleanup()
  6. Exit the nano editor by pressing CTRL+X, Y, and Enter.
  7. Right now, running RPiLED.py will cause the LED to blink 50 times. You can change how many times the LED blinks by changing the highlighted text above.
  8. Try running your script by typing sudo python RPiLED.py in the terminal.
  9. If it does not work, check your wiring. If you don't see a problem, try using a different LED.
The code for RPiLED came from www.rpiblog.com.


No comments:

Post a Comment