Let's quickly see how to control the RGB led with your Arduino once again in this section.
I'm going to go quite fast.
The goal is not to go into details about each Arduino functionality, but to reach the next activity
part where you will practice on Arduino functionalities controlled by the Raspberry Pi using serial
communication.
So let's start and let's directly write some basic code for the RGB Led.
So what you can do here already is to connect your Arduino.
So once the circuit is correctly done, you can connect your Arduino back to the Raspberry Pi with the
USB cable and then check that you have the Arduino connected here with the correct board, of course.
And now, well, basically how to control an RGB led is very simple.
If you already know and you probably already know how to control an Led.
Well, an RGB Led is nothing more than three different LEDs when we speak about the code.
So we are going to control each leg of each color separately.
So I'm going to create some define here.
So define RGB red pin with PIN number 11.
So make sure that you use the pins you have selected.
If you have selected different pins, define RGB.
So green pin ten and define RGB blue pin nine.
And if you have some doubt about the order of those pins for you, well, just start with this and then
you will be able to change that after you experiment with some code.
So we have the defines.
Then what we do is we simply do pin mode on each leg.
Okay?
Just like we were controlling three different LEDs.
Okay, so pin mode for the red pin.
PIN mode for the green pin mode for the blue.
Okay.
Everything is correctly set up and then.
Well, I'm not going to do anything in the loop.
I'm just going to show you how to just power on this RGB led.
So if you do digital right here, you can do digital, right?
And you can also do analog, right?
Because we are using pins that are compatible.
I'm going to start with digital.
Right?
Okay.
So you can put, for example.
Hi, and then the digital.
Let's just use the same here.
So for the red and then for the green and then for the blue.
So let's say we want only to power on so low.
Only the green and the blue.
Okay.
To make a combination of green and blue color.
So those are going to be powered on with full intensity and this one with zero intensity.
So let's So board is connected.
Let's upload.
All right.
And you can see we have this color, which is not exactly blue, not exactly green, but in the middle.
So with this, you can do different combinations, okay?
Up to seven different combinations.
And then I'm going to comment this.
And now let's see an example with analog.
Right?
Okay.
So I'm going to use also the pin like this.
And then you need to give a number between 0 and 255.
So here you have more control over the colors.
Okay?
So if you put zero, that's going to be powered off.
If you put 255, it's just like you use digital, right, with high.
And then if you use any of, for example, the 180, this is going to be a percentage of the brightness
basically between 0 and 255.
That's going to be applied to the red color of the RGB.
So let's do this.
I'm going to use the same for the green, but I'm going to put zero here.
I'm going to try to make a purple color.
So I'm going to do blue and let's say 200.
Okay.
So we're going to do a mix of red and blue, which should turn purple.
And let's see that.
So let's run the code.
Upload, uploading and done uploading.
Okay.
And we have a color that is purple.
So that's pretty much it for the RGB.
You first create some defined or const int and then you do pinmode for each pin and then you can use
either digitalwrite or analogwrite.
So this only if you have the functionality of course, and you can create any combination of color and
this you can use in the setup in the loop.
Well, you can use that anywhere you want.