Day 5 - Section 3 - Multiple LEDs example
Site: | ΕΛ/ΛΑΚ Moodle |
Course: | Robotics - 3D Printing - Internet of Things |
Book: | Day 5 - Section 3 - Multiple LEDs example |
Printed by: | Guest user |
Date: | Tuesday, 14 January 2025, 4:08 AM |
Description
After the completion of this section the students will be able to:
- create a circuit with multiple LEDs onboard
- program the NodeMCU to make the LEDs turn ON and OFF, one by another
- change the blinking speed to faster or lower
- add more LEDs to the circuit
Light the LEDs up, one by another...
Now, let's try to create a program with four LEDs that they will be turned on and after a while they will be turned off in a row.First of all, we connect the LEDs, the cables and the NodeMCU on the breadboard, according to the following picture (you can use any cable color you like, not only the ones that are displayed below):
As we notice, we use pins D0, D1, D2, and D3, or pins 16, 5, 4 and 0 if we talk in Arduino IDE language!
Then, we copy the following code to the Arduino IDE and upload our program:
void setup() {
pinMode(16, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(0, OUTPUT);
}
void loop() {
digitalWrite(16, HIGH);
delay(200);
digitalWrite(5, HIGH);
delay(200);
digitalWrite(4, HIGH);
delay(200);
digitalWrite(0, HIGH);
delay(200);
digitalWrite(16, LOW);
delay(300);
digitalWrite(5, LOW);
delay(300);
digitalWrite(4, LOW);
delay(300);
digitalWrite(0, LOW);
delay(300);
}
pinMode(16, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(0, OUTPUT);
}
void loop() {
digitalWrite(16, HIGH);
delay(200);
digitalWrite(5, HIGH);
delay(200);
digitalWrite(4, HIGH);
delay(200);
digitalWrite(0, HIGH);
delay(200);
digitalWrite(16, LOW);
delay(300);
digitalWrite(5, LOW);
delay(300);
digitalWrite(4, LOW);
delay(300);
digitalWrite(0, LOW);
delay(300);
}
Are we happy by the result?
Now think about, how can we add more LEDs and what should we do if we want to make them light on and off faster? What is the maximum amount of LEDs that we can handle?
Experiment, play, enjoy!