using an rpi to control an rgb led

by:IPON LED     2020-05-10
As rahulkar mentioned in his tutorial, the LED controls seem to be the \"Hello World\" for electronics \".
It\'s easy to set up, you\'ll learn a lot about how electronics work, and in the end, you\'ll get a nice little hardware to show off to your friends --
Really cool!
This tutorial will be naked.
Basic knowledge, you can use this tutorial as the first project in order to have a basic understanding of Raspberry Pi, circuits, and programming.
In particular, I will explain the code behind the program line. by-
This is a line for those who are new to Python.
After this tutorial, you should master how the basic program works and be able to modify our code.
In this tutorial, we will use rgb led.
R, G, and B represent red, green, and blue, respectively, and a single RGB LED actually contains three LEDs in a single bulb.
These are the primary colors of light;
That is, in theory, you can use a combination of these three colors to create light of any color.
A mix of pure red and pure blue produces magenta, blue, and cyan greens, while red and green turn yellow, all three of which turn white.
Different shades can be obtained by mixing different strengths of the three primary colors.
In reality, we will find that it takes a little bit of refinement to hone on a specific tone of the Pi, which may not always work.
Before we start, let\'s briefly introduce how the circuit and LEDs work.
When you buy the LED, you should see a bunch of text on the back of the package that shows the technical specifications.
If you can\'t seem to find any specs, try running Google search on the serial number of the part.
What we care about is the forward voltage (
VF (in volts)
Forward current (
If, in milliamperes). Typical LEDs (
Burn a color)
Each variable has a value.
However, you may notice that LEDs of different colors have different values.
In particular, red appears to reduce the lowest forward voltage, while blue drops the highest forward voltage.
Again, for the three colors, it is very likely that your rgb led will have different values.
You need to keep these values in mind when you buy the material so you can choose the right resistor-
Don\'t worry, we\'ll be there!
You\'ll also notice that the RGB LEDs have four instead of two leads.
As mentioned earlier, rgb led is actually a very small circuit consisting of three different LEDs.
Things can get a little tricky now.
There are two types of RGB LEDs: Co-anode and co-cathode.
On the common anode type, the longest lead wire is connected to the positive pole, allowing the current to flow to all three LEDs inside, the leads 1, 3 and 4 are ground leads in red, green and blue respectively.
On the other hand, common cathode types share ground leads;
The three shorter leads are respectively connected to the forward terminals in the same order.
Which one should you use?
As a beginner, I will stick to the common cathode types.
The normal cathode allows you to better understand how the raspberry handles the output and how the circuit works, and while the normal anode type can still work with the current sinking, it is slightly
Resistors are important because they prevent the LEDs from sending too much current through the Pi and they do so if there is a chance.
This can cause you to blow the LED, blow up the GPIO pin, and/or destroy the Pi.
Always use resistors when connecting the output to the Pi, even if they are lowlevel.
Typically, these LEDs can handle up to 30 mA of the current each, but we keep them below 17 mA.
Why can\'t we be that high?
The Pi can only provide so much current to the GPIO pin, and again, too much stretching can cause serious damage.
How high can we go?
The answer is not super direct, but it can be inferred.
According to eLinux, the GPIO pin is connected to 3.
3v rail, up to 50 mA power supply at a time.
However, this does not mean that there is a regulator for the railway to limit it to 50 mA.
If this is allowed, the circuit connected to the track will continue to draw more than 50 mA of the current until the short circuit.
Keeping all three pins at least below 17 mA will ensure that our Pi is adequately protected even if all three colors burn at the same time.
The resistance you buy will depend on your individual LEDs.
You need three resistors, one for each color.
To calculate the resistance, go to LEDCalculator. net.
This site uses Ohm\'s law, so you don\'t have to do that.
Just enter the voltage provided (3. 3v)
Voltage drop (
Forward voltage for a single color)
, Current rating (
Below 16 mA)
Number of Led (
Treat each color as a single LED in this case).
Do this for each color and change the voltage drop appropriately.
Now you have got the value of the resistor!
Get each of the 1/4 W sizes (
In this case, the wattage measures the heat absorbed by the resistance before failure).
Now we have everything we need.
Let\'s start!
Technically, no welded breadboards are needed, but it makes things faster and easier.
However, if you choose to weld, I will assume that you have at least some basic knowledge of the circuit, and the chart below will be sufficient for you to continue working.
The solder-free breadboard is used in prototyping as it allows you to move components around the circuit easily without having to weld and disassemble.
Believe me, when I say that no pads can save hours even for a simple project.
On the solder-Free plate, the positive and negative poles called buses can be connected to both the positive and negative terminals on the power supply.
We can use jumpers to connect to these buses to form circuits.
Column between two groups of buses marked as A-E and F-
J, is connected by row, but these connections do not go through the separator in the middle.
For example, column A-
The first line E. Make a track, column F-
The J of the first row makes another track, column A-
The E in the second row is a separate track.
Now that we understand this, let\'s talk about the circuit.
For this circuit, we need to connect the co-cathode to the ground pin through a resistor, and each of the three anode is connected to a different programmable PIN.
In this case, we will use the numbers 9, 11, 13, and 15 of the GPIO pins (
There are two different naming conventions for the Pi\'s GPIO pins --
This is the use of the \"Board of Directors\" convention).
Pin 9 is grounded so we will connect our cathode to it and then connect the red, green and blue to pins 11, 13 and 15.
Once it\'s all ready, we can continue testing our circuits --
We don\'t want faulty circuits to make us think our code is wrong.
To do this, apart from the ground connection, disconnect the circuit from the Pi and turn on the Pi.
After the operating system is started, first remove the red line from the breadboard and connect it to pin 1.
This pin is not programmable and is always set to 3.
3 v, so the red LED should light up when the red line is connected.
Do this in all three colors.
Before we start, it is important to note that indentation is important in the Python language.
Other languages use punctuation marks such as brackets to tell the order in which things run;
Python uses indentation.
Therefore, you may not be able to simply copy and paste the code in this page into the program, as the format may not be fully retained.
Feel free to follow and write your own code or download my sample code here.
If you are new to coding, then it is important that there is no way to write a program.
People have their own style, but it depends mainly on how you think about solving problems.
I will provide the sample code and explain the line of it-for-
But please know that this is not the only way to achieve exactly the same result.
We imported two. in libraries -
Functions, variables, etc.
This has been defined and we can call for it.
This is a Raspbian library that allows us to control the GPIO pins using Python.
We defined some variables.
Which RGB pin will we connect.
This step is not necessary, but it will make it easier for you to change the pins if you need.
Here we define a function.
A loop that continuously requests and responds to user input by calling various functions.
In this case, the user will be able to turn the color on and off.
This program is written like this, so it only accepts a few pre-
The input specified.
Otherwise, it will do nothing and display a message for this.
Again, we don\'t ask for any variables to use this function, so the brackets remain empty.
\"Though the real\" is basically saying \"do it when the program is running\", so it will keep running until we exit.
We define a new variable \"cmd\" and set it to equal the input of the user.
It also keeps prompting users to select an option.
Now, the \"if\" statement is run only if the user inputs exactly match the text inside the quotation marks.
Please note that here we use two \"equal\" symbols.
In Python, actually in most languages, a single \"equal\" is used to change the value of a variable, while double is used to compare whether the variable values match each other.
\"Elif\" represents \"else, if \".
This illustrates the previous \"if\" statement and adds new parameters that allow us to easily create the \"else, if\" statement chain.
Using our last \"else\" statement, if the user tries to enter any command other than the one specified above, it tells the user that this is an invalid command.
The last thing the function needs is to \"return\" so the program knows when to loop.
This is the last line of our code.
When the program is running, this line will call the main function we just defined and set the loop. And that\'s it!
It\'s time to run the program.
Navigate to the top menu bar and select run-
> Run the module, or press f5.
You will be asked to save the work before IDLE runs the program.
Continue, type \"red on\" after the prompt, and then click enter.
The red light is coming on!
Note that at least all colors should be closed before exiting the program.
However, I would suggest you run the GPIO. cleanup()
Run before exiting.
This is the built-in feature that comes with RPi.
GPIO library, and reset any pins used in the program.
You can modify this code to include commands that run the GPIO. cleanup()
, You can also interrupt the program by pressing ctrl c, and then type the GPIO. cleanup()
Enter user input directly.
Please feel free to ask any questions!
I \'d be happy to help.
Custom message
Chat Online 编辑模式下无法使用
Chat Online inputting...