Raspberry Pi Blinking LED

Making an LED blink is simple once you know how. To most people reading this who are new to coding an LED blinking doesn’t seem very interesting…it’s just a light after all.

Well yes that’s true, but the magic is when the jr. Developer writes then runs the Python script that tell the LED to turn on and off. I was 29 when I experienced the tremendous sense of accomplishment and realizing this was the beginning of robotics.

For an adult diagnosed with ADHD and Bipolar 1 stumbled into coding by mistake. But my always racing mind that I was slowly losing a grip on seemed to stop, slow down, and calm itself when I would write scripts. Coding truly is amazing!

 

Back to the project…

Materials

  1. Raspberry Pi with a Noobs flashed SD card and Raspian Desktop installed.
2. Keyboard & Mouse3. A bread Board4. A single 100ohm resistor5. An LED6. Two Jumper Wires

 

Also, if you’re wondering why to even bother with the resistor, I was too. From what little I understand, once the LED lights up it offers no resistance at all, so you’ve basically got a short-circuit. Things may work for awhile, but you could drastically shorten the lives of both the LED and the Pi. From the aforementioned tutorial:

You must ALWAYS use resistors to connect LEDs up to the GPIO pins of the Raspberry Pi. The Raspberry Pi can only supply a small current (about 60mA). The LEDs will want to draw more, and if allowed to they will burn out the Raspberry Pi. Therefore putting the resistors in the circuit will ensure that only this small current will flow and the Pi will not be damaged. Resistors are a way of limiting the amount of electricity going through a circuit; specifically, they limit the amount of ‘current’ that is allowed to flow.

Ultimately, here’s how I laid it out. The resistor I used was only 220 Ω (the tutorial recommends 330 Ω), but it worked okay.

breadboard single led circuit

Python Script

After you create the circuit, you still need to signal the GPIO pin, in order to turn on the LED.

My favorite part…writing the script.

Our script needs to do the following:

  • Initialize the GPIO ports
  • Turn the LED on and off in 1 second intervals 

To initialize the GPIO ports on the Raspberry Pi we need to first import the Python library, the initialize the library and setup pin 8 as an output pin.

 

import RPi.GPIO as GPIO    # Import Raspberry Pi GPIO library
from time import sleep     # Import the sleep function from the time module

GPIO.setwarnings(False)    # Ignore warning for now
GPIO.setmode(GPIO.BOARD)   # Use physical pin numbering
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW)   # Set pin 8 to be an output pin and set initial value to low (off)

 

Now we’re going to add the line of code that tells the LED light to turn on and off in 1 second intervals. We will set up the line of code to loop the program until we manually turn it off.

while True: # Run forever
    GPIO.output(8, GPIO.HIGH) # Turn on
    sleep(1)                  # Sleep for 1 second
    GPIO.output(8, GPIO.LOW)  # Turn off
    sleep(1)                  # Sleep for 1 second



Combining the two sections of code should look something like this:

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from time import sleep # Import the sleep function from the time module

GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin and set initial value to low (off)

while True: # Run forever
 GPIO.output(8, GPIO.HIGH) # Turn on
 sleep(1) # Sleep for 1 second
 GPIO.output(8, GPIO.LOW) # Turn off
 sleep(1) # Sleep for 1 second




With our program complete save the file “your name.py”

$ python yourname.py

With the program running you should see something like an LED light blinking on and off.

Console Output

Here’s the output that prints to the console while the LED blinks. That last word, “scrot”, is the command for taking a full-screen capture with the Scrot app in Raspbian.

blinking led output

It’s Aliiiiive!

And finally, here’s the LED blinking on and off. Success!

Parts used in this tutorial:

https://www.amazon.com/gp/product/B01LPLPBS8/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B01LPLPBS8&linkCode=as2&tag=sudogrizzlyge-20&linkId=22af89e999ecc262d9e068f5e89b86a6

Be sure to browse through our online store. Our mission is to encourage kids to pursue and seek more knowledge in programming.

https://sudogrizzlygents.com

And always stays safe and take advantage of IP Vanish services at 50% off!

https://www.ipvanish.com/?a_bid=0fe9ca17&a_aid=napp

Follow us on Facebook:

https://facebook.com/sudogrizzlygentsllc/


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.