Second Arduino Lesson RGB-LED
Hi there!
This is the second basic tutorial on how to program your Arduino microcontroller
As always what it should look like
What you need:
- Microcontroller (Arduino, Elegoo,...)
- 330 Ohms resistor
- a RGB-LED
...and of course some jumper wires
Ok let's start!
Look at the photo to connect your RGB LED the right way.
Left one goes to Red as it is the first letter in RGB 😉.
The second one is a bit longer it should be connected to a 330 Ohm resistor and the resistor to ground.
3rd should then be connected to green and last but not least the fourth to blue.
Use Ports that are capable for analog output such as Ports 9 to 11. They're marked with a ~.
Functions and Commands to learn:
- #define (to name a port e.g.: #define blueLED 3) Note that you must not add a ; after a #define
- int (to clarify a variable type int means Integer and it can store a value between -32,768 and +32,767 e.g.: int delaytime = 1000;)
- pinMode() (to set a pin as an OUTPUT or INPUT e.g.: pinMode(blueLED, OUTPUT);)
- digitalWrite() (to turn a signal on (HIGH) or off (LOW) e.g.: digitalWrite(blueLED, HIGH);)
- delay() (waits for a time in milliseconds e.g.: delay(delaytime); You can also use variables!)
- for() (used to repeat a block of code in {} braces. e.g.: for(int i = 0; i<255; i++){code})
Next: program your microcontroller
Let's say we want to gradually turn on a certain color, then mix it with another, and so on...
Try to look at the code and you'll certainly figuer out how it works. However if you have a question feel free to submit a comment.
#define blueLED 11 //Defines Port 11 as blueLED
#define redLED 9 //Defines Port 9 as redLED
#define greenLED 10 //Defines Port 10 as greenLED
int Blue = 0; //Set color value to 0
int Red = 0; //Set color value to 0
int Green = 0; //Set color value to 0
int delayTime = 5; //Set delayTime to 20 ms
//
void setup() { //Setup only runs once
pinMode(blueLED, OUTPUT); //Defines blueLED as an Output
pinMode(redLED, OUTPUT); //Defines redLED as an Output
pinMode(greenLED, OUTPUT); //Defines greenLED as an Output
digitalWrite(blueLED, LOW);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
}
//
void loop() {// Loop repeats itself the whole time until you shut your controller down
//
for(int i = 0; i<255; i++) //performs the task in the {} brakets till i is bigger than 254; counting variable i is created and set to 0 in the for function
{Blue = 255 - i; //Sets color value
Red = i;//Sets color value
analogWrite(blueLED, Blue);//Outputs a analog signal with a value between 0 and 255 to the Blue LED
analogWrite(redLED, Red);// Same for Red
delay(delayTime);//Delays for delayTime
}
for(int i = 0; i<255; i++)// Same thing as above but with other colors this time
{Red = 255 - i;
Green = i;
analogWrite(greenLED, Green);
analogWrite(redLED, Red);
delay(delayTime);
}
for(int i = 0; i<255; i++)//And the again
{Green = 255 - i;
Blue = i;
analogWrite(blueLED, Blue);
analogWrite(greenLED, Green);
delay(delayTime);
}
}
Very nice post! I give you UP
Congratulations @etschgi1! You have received a personal award!
1 Year on Steemit
Click on the badge to view your Board of Honor.
Do not miss the last post from @steemitboard:
Congratulations @etschgi1! You received a personal award!
You can view your badges on your Steem Board and compare to others on the Steem Ranking
Vote for @Steemitboard as a witness to get one more award and increased upvotes!