Led Blinking using Raspberry Pi T cobbler -You must set up your Raspberry Pi in this tutorial to power small bulbs called light-emitting diodes (LEDs). Using Python, you can blink the LED. You will need to learn a little bit about how to create electrical circuits on breadboards to do this. Do not panic if you have never heard of a breadboard! To make it easier to create electrical circuits, it is a small board with lots of holes in it. You will also use short wires to connect those holes (called jumper wires). You will even learn to install resistors to keep your LED from burning out. For a list of parts and what they look like, see below collect the pieces, and let’s get started!
The Amazon Purchase links are given below:
Raspberry Pi 3: https://amzn.to/2F4QqqE
Raspberry Pi T Cobbler Board: https://amzn.to/36eCOoL
jumper wires(male-to-male): https://amzn.to/2SrnBwo
330-ohm resistors: https://amzn.to/2Qj1Igg
The common Amazon Purchase links are given below:
Arduino Uno: https://amzn.to/39aq6ZT
Mega 2560: https://amzn.to/2SszMsI
lm7805 Voltage Regulator: https://amzn.to/2ERYoTJ
female DC power jack socket: https://amzn.to/377Pg9M
470uf capacitors: https://amzn.to/2MrN3xR
5×7 cm Vero board: https://amzn.to/37b7KWO
female headers: https://amzn.to/350w6RE
connection wires: https://amzn.to/2MvOJXd
Super Starter kit for Beginners: https://amzn.to/398dliF
Jumper Wires: https://amzn.to/2SrnBwo
12v Adaptor: https://amzn.to/2MuOlZk
PCB plate: https://amzn.to/2MuwNMB
Variable Supply: https://amzn.to/39d0KdP
Digital Multimeter: https://amzn.to/34WbVoa
Vero Board / stripboard: https://amzn.to/39nL9Zg
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2PVwexF
Solder wire: https://amzn.to/2QlOvTS
Wire Stripper: https://amzn.to/353tYJa
PCB small portable drill machine: https://amzn.to/2MvQqnx
Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Setting up your Raspberry Pi for physical computing:
Your Raspberry Pi is unique compared to most computers because of its input and output pins, called GPIO pins. Let’s learn how to work with those pins.
GPIO stands for general-purpose input and output. These are the pins on your Raspberry Pi that allow it to sense and control things around it.
The Raspberry Pi 3 Model B and Raspberry Pi 1 Model B+ have 40 pins located on the edge of the board, arranged in 2 rows of 20 pins each (see below). Most of the pins on a Pi are used for input and output, so they’re often referred to as the Pi’s GPIO pins.
WARNING This project is written for Raspberry Pi 2 Model B. Earlier models of the Raspberry Pi have only 26 pins. To complete this project with a Raspberry Pi 3 Model B, you may select different pins to light up your LEDs
Because all the pins look identical, you need a key or diagram to tell you what each one does. The above image shows the pins labeled. Wow, that’s a lot of pins! Some pins are for power and are labeled either 3V3 or 5V. These produce 3.3 volts or 5 volts, respectively. There are also 8 ground pins and 26 GPIO pins1—26 pins, just like there are 26 letters in the alphabet. The GPIO pins support sending out electrical signals (output) or listening for electrical signals from sensors (input). In your body, your brain can send signals to your hand to smack yourself on the forehead (try it!)— this is just like the output from a Pi. Signals are sent out of your Pi to make something happen in the world. The opposite of output is input. When someone pokes you, your body can detect that poke using nerves in your body. An electrical signal (input) is sent to your brain so you know you’ve been poked. This is like the way your Pi can be used to detect input or actions in the world. Let’s get ready to connect some wires! But wait: connecting an LED directly to the GPIO pins on the board of your Raspberry Pi isn’t feasible, because the pins are so close together. What can you do? You need more space to build circuits.
Raspberry Pi T Cobbler GPIO pins to a breadboard
To give you a room, you’ll move the GPIO pins over to a breadboard. This is called breaking them out. To do this, you need a ribbon cable, Raspberry Pi T Cobbler board, and solderless breadboard (see below figure ). Breadboards make it simple to prototype circuits. Like a park might provide large, open fields that make it easy to play sports, think of a breadboard as a nice, open electrical playing field where you can play with electrical parts. The breadboard allows you to plug wires and components into small holes. You can build and rebuild circuits on a breadboard with little effort.
Find your t cobbler board, and insert it into the top of the breadboard. Line up the pins before you push it down hard (see above figure). Your particular t cobbler board may look a little different, but they all act the same. With the t cobbler board in place, it’ll be easier to build circuits with your GPIO pins. Connect one end of the ribbon cable to the Raspberry Pi’s GPIO pins; line it up carefully before you push it down. Then connect the other end of the cable to the breakout board on your breadboard (see below figure). A Raspberry Pi T Cobbler board has a notch in it so the ribbon cable will only fit one way.
WARNING Ribbon cables usually have a stripe that marks the first wire. White or grey ribbon cables often use a red stripe. Black ribbon cables often have a white stripe. These mark the first wire on the cable. Make sure this first wire is connected toward the edge of your Pi’s board and away from the USB ports.
Building the LED circuit
Your first project is to light up a yellow LED. You’ll control the LED using
GPIO pin 21 (GPIO21). You need these parts:
- Raspberry Pi, ribbon cable, and t cobbler board connected to your breadboard
- 1 yellow LED (5 mm)
- 1 330ohm resistor
- 2 jumper wire (male-to-male)
You’ll build the LED circuit on your breadboard and then program it to light up. The above Figure shows the circuit diagram. To light the LED, you’ll have electricity (+3.3 V) flow from your Pi’s GPIO pin 21 through the LED, through the resistor, and then to ground (0 V). the below Figure shows the LED circuit built on the breadboard. Note that there are many different ways to create this circuit—this is just one way. Let’s walk through the steps to build the circuit.
Steps for Led connecting:
Step 1. Connect the jumper from GPIO pin 21
Raspberry Pi GPIO pins can output 3.3 V. You could pick any pin, but this project uses GPIO pin 21. Connect a short piece of wire from GPIO21 on your breadboard to an empty row on the breadboard. Use row 25. Firmly push the wire into the hole. The metal tip of the wire should go down into the hole, not sit on top. The t cobbler board pins are connected to rows on the breadboard. We’ll refer to the holes on the breadboard.
Step 2. Add the yellow LED
It’s time to connect the yellow LED. LEDs only let electricity flow through them one way, so its important to put them in the right way. LEDs have two wires or legs. The longer leg is called the anode and connects to the positive
side of the circuit (see above figure). The shorter leg, called the cathode, connects to the negative or ground side of the circuit. With the yellow LED, connect the longer leg to +ive and the shorter leg to -ive. You may need to bend the legs and push them a bit to get them into the holes.
Step 3. Connect a resistor
Grab your 330-ohm resistor. You can identify a resistor by its color-coded bands. A 330-ohm resistor has colored bands of orange, orange, and black (see below figure ). They are followed by a fourth band that
indicates the tolerance or quality of the resistor. Common colors for the fourth band are gold (±5% tolerance) and silver (±10% tolerance). The resistor prevents too much electric current from passing through your LED and burning it out. Insert one end of the 330-ohm resistor with led pin and the other end into the negative (-) power bus (or ground). Electricity will flow either way through a resistor, so which way you connect it doesn’t matter. Remember that the negative power bus or ground rail is running vertically along the right side of the breadboard.
Most boards have a blue stripe next to it. That’s it! You have a completed LED circuit built on your breadboard. Now it’s time to program it!
Software: blink LED program
Open IDLE by choosing Python 3 under Menu > Programming. This opens IDLE to the Python 3i am using thonny python idle In the Python idle, let’s check to see if your Pi has the GPIO libraries you need already installed:
>> import RPi.GPIO as GPIO
If you don’t see an error, you’re ready to go. If you see an error saying there is no module named RPi.GPIO, please refer to the sidebar “Updating your Pi.” You’re going to write a program that blinks an LED. It’ll send a voltage
(+3.3 V) out of a GPIO pin to light the LED, then turn it off, and repeat that over and over. Begin by creating the following new program in IDLE. In the Python idle, start a new program by pressing Ctrl-N.
Open thonny python idle
selecting File > New Window.
import RPi.GPIO as GPIO import time # Variable for the GPIO pin number LED_pin_yellow = 21 # Tell the Pi we are using the breakout board pin numbering GPIO.setmode(GPIO.BCM) # Set up the GPIO pin for output GPIO.setup(LED_pin_yellow, GPIO.OUT) # Loop to blink our led while True: GPIO.output(LED_pin_yellow, GPIO.HIGH) time.sleep(1) GPIO.output(LED_pin_yellow, GPIO.LOW) print("Off") time.sleep(1)
Save the program as blinking.py in your home folder. The program can’t be run the same ways you’ve run programs before using IDLE.
Running the program
To run your program, select Run > Run Module (or press F5) from the text editor of the IDLE. With older versions of Raspbian, programs using GPIO pins must be run from the Raspbian command prompt as the superuser (or root). If you run the program at the Python Shell in IDLE, you’ll get an error:
RuntimeError: No access to /dev/mem. Try running as root!
In this case, you use the sudo command to do this. To run the blinking.py program, open LXTerminal and enter the following command:
pi@raspberrypi ~ $ sudo python3 blinking.py
Behold the blinking LED! Try making the light blink faster by adjusting the value in the sleep function. Use a smaller number of seconds, such as 0.5 or 0.1.6 To stop the program, press Ctrl-C.
blink LED: how it works
Let’s take a closer look at how the blinking.py code works.
The import commands load the libraries or toolboxes you want to use in your program:
import RPi.GPIO as GPIO
These commands load the Python libraries for controlling the Pi’s GPIO pins. They also load the time library so you can use the sleep function to control the rate of blinking. Once the libraries are loaded, you can set up your GPIO pins.
SETTING UP A GPIO PIN FOR OUTPUT
To set up a GPIO pin, you first need to tell Python on your Pi that you’ll be referring to pins by the standard breakout numbering scheme. These are the numbers printed on the breakout board. You use the setmode function:
BCM stands for Broadcom—the maker of the computer chip that the Pi uses. Next, you tell your Raspberry Pi that you’ll be using LED_pin_yellow (GP21) for output, meaning you’re planning to send some electricity out of it:
LED_pin_yellow = 21
GPIO.OUT prepares GP21 to send out +3.3 V of electricity.
LOOPING AND BLINKING
Finally, you create an infinite while loop and turn the LED on (set GPIO.HIGH) and off (set GPIO.LOW). You also add a delay using the sleep method found in Python’s time library. Notice how the sleep function takes a parameter that is the number of seconds to sleep or pause. In this case, you use 1 second:
The print commands display messages to the screen. Although they aren’t necessary to blink the LED, they can help debug your program. If you do use them, the screen could quickly fill with messages. Set a longer delay time to prevent this. If you see the messages on the screen but your LED isn’t lighting up, then you probably have an error in
your circuit and not in your program. Check your wiring, try turning around the LED, or try a different LED in case that one is defective.