Small LED really big
Small LED really big
Now that we have discussed everything you need to get started it is time we dive in and create something beautiful - a blinking led. To get moving, start your Pi and gather the necessary components from your kit including, a breadboard, some jumper cables (you can choose any of the colors), a 220Ω resistor, and an led (we would suggest the beautiful red led). Once gathered, make all the necessary connections using the image and table below as a guide. Please pay attention to the polarity of the LED. You will want to make sure that the shorter leg of the LED is connected via the resistor to ground (GND), otherwise the circuit won’t work. You’ll learn why this is important later.
|Connections Pi||Connection LED|
|GPIO18||long leg LED|
|GND (Ground)||short leg LED + 220Ω Resistor|
We create this new file for our led program. We will first show you the program and then explain it step for step afterwards.
import RPi.GPIO as gpio import time gpio.setmode(gpio.BCM) gpio.setup(18, gpio.OUT) gpio.output(18, gpio.HIGH) time.sleep(2) gpio.output(18, gpio.LOW) time.sleep(2) gpio.output(18, gpio.HIGH) time.sleep(2) gpio.output(18, gpio.LOW) gpio.cleanup()
You can close the program with
Ctrl+ X and save the written program with confirming it. Use the command
sudo python3 led.py to execute the program in the terminal.
Step for step
We have already learned a lot about functions. You know how they look like and how they operate. In first two lines of code we are including what is called a library to the program. You can imagine a library like a real library for books except without stories about young wizards and rather a list of functions which we have at our disposal for use in our program.
import RPi.GPIO as gpio import time
The library in the first line includes a lot of functions with which we can use to easily control the GPIO Pins.The
as part makes it possible to give the library a new short nickname, or alias as some may call it, for all the references in the program. Instead of typing
RPi.GPIO.function() we now only need to say
gpio.function() and can save time and brain energy. And yes, it is necessary to reference both the library and the function if we use a library. The library in the second line has a lot of functions for time which we can use for timing commands or printing the current date. We will use both libraries a lot in the following programs.
You will see the first function in line 3. This function is very important and lets Python know which gpio layout we are talking about. There are a few different options but we are choosing our favorite called
BCM. It is the same as on the GPIO Pin Helper.
In line 4 we set up the specific function for the pin since every pin is capable of being used as an input or an output. Because we don’t want to measure data, but rather control the led, the pin type is output.
In line 5 we are “turning on” the circuit for the pin by setting it to
HIGH. The LED should now be turned on because there is a closed electronic circuit from the pin, to the led, to the resistor and then to ground. Because we want to see the blinking led, we need to wait a bit before turning the LED off. For this we can use the time library. In line 6 we are create a two second delay with the
time.sleep(2) function. The parameter between the two parentheses is the amount of seconds the program should be halted.
To turn off the circuit, we set the pin to LOW in line 7. After another break we are turning the LED on again. In line 12 we clean up all the of settings and everything else on the GPIO pins. This is the last piece of the program.
You may have questioned, “Why do we need the resistor”? The resistor limits the currents that pass into the following circuit. If the current is too high, the LED or the Raspberry Pi could be damaged. This is why it is also referred to as a protective resistor. You can imagine this whole thing as a water circle. In this case the LED is like a water wheel. For the water wheel to move (and the LED to shine) you need to meet the following requirements:
a) an adequate amount of water b) the water must be strong enough c) the water must be flowing to somewhere.
If the water flow is too strong, the wheel will be damaged. To prevent this we use the protective resistor, as it limits the amount of water which can flow. When you use a resistor, you do not need to check the direction with which you are putting it into the circuit.
Doch wofür brauchen wir jetzt diesen Widerstand? Dieser begrenzt die Menge an Strom und verhindert, dass die LED oder der Raspberry Pi beschädigt werden und wird auch Vorwiderstand genannt. Du kannst dir das Ganze auch wie ein Wasserkreislauf vorstellen. Die LED wäre in diesem Fall ein Wasserrad. Damit sich dieses zu drehen beginnt muss es a) einen Wasserfluss geben und b) muss dieser stark genug sein. Wenn das Wasser aber zu stark fließt, würde das Rad kaputt gehen und deswegen dient der Widerstand als eine Verengung und begrenzt den Wasserfluss. Es ist dabei egal, in welche Richtung du den Widerstand einsteckst.