X
Home > Blog > STEM for ESP32 > HOW to Make LEDs Like Flowing Water with ESP32

HOW to Make LEDs Like Flowing Water with ESP32

By r December 18th, 2024 154 views

1. Overview

In this project, we light up LEDs with time difference, so they will not turn on at the same time. As the name implies, the LEDs turns on and off one by one.

2. Wiring Diagram

Connect this module to socket 9. According to the pin sequence, the red LED is connected to pin io17, the yellow to io18, and the green to io19.

b1

3. Test Code

/*  
  Project name: 3_Flowing_light 
  Function: Control the red, yellow and green LED to show the effect of water flow
  Author: keyestudio    
  Hardware connection:  
    - Connect to port 9 
  Library:  
    - none (no need to import additional library)  
  Cautions:  
    - Ensure that connect to port 9   
    - Before uploading test code, please correctly connect to the development board and port   
*/    
#define RED_LED_PIN 17      //Connect the red LED to port IO17
#define YELLOW_LED_PIN 18   //Connect the yellow LED to port IO18
#define GREEN_LED_PIN 19    //Connect the green LED to port IO19
  
void setup() {  
  // Initialize the digital port to output  
  pinMode(RED_LED_PIN, OUTPUT);  
  pinMode(YELLOW_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);
}  
  
void loop() {  
  // red LED on, yellow and green LED off
  digitalWrite(RED_LED_PIN, HIGH);  
  digitalWrite(YELLOW_LED_PIN, LOW);  
  digitalWrite(GREEN_LED_PIN, LOW);  
  // Delay 300ms  
  delay(300);  
  // yellow LED on, red and green LED off
  digitalWrite(RED_LED_PIN, LOW);  
  digitalWrite(YELLOW_LED_PIN, HIGH);  
  digitalWrite(GREEN_LED_PIN, LOW);   
  // Delay 300ms  
  delay(300);  
  // green LED on, red and yellow LED off
  digitalWrite(RED_LED_PIN, LOW);  
  digitalWrite(YELLOW_LED_PIN, LOW);  
  digitalWrite(GREEN_LED_PIN, HIGH);   
  // Delay 300ms  
  delay(300);  
}

4. Test Result

After uploading the test code, the traffic light module will turn on in this order: red, yellow and green LED.

d3

5. Optimized Code

C is very flexible, we parse the codes into steps to execute them. If we use 10 or more LEDs to make flowing water light, codes will be very long. So an “array” thing is combined with for loop to simplify the codes.

Test Code:

/*  
  Project name: 3_Flowing_light_plus
  Function: Simplify the amount of code, control the red, yellow and green LED to show flowing water effect
  Author: keyestudio    
  Hardware connection:  
    - Connect to port 9 
  Library:  
    - none (no need to import additional library)  
  Cautions:  
    - Ensure connect to port 9   
    - Before uploading test code, please correctly connect to the development board and port
    - The bit number of the array value starts from 0   
*/    

//Put the pin numbers that control the LED into the array, then LED_PIN[0] is IO17 pin, LED_PIN[1] is IO18 pin, and LED_PIN[2] is IO19 pin
int LED_PIN[] = {17,18,19}; 
  
void setup() {  
  /*
    Initialize the digital port to output
    First loop: i = 0 meets the loop condition i < 3, enter loop, LED_PIN[i] is 17
    Second loop: i++ is executed first, so the value of i is 1; i < 3, enter loop, LED_PIN[i] is 18
    Third loop: loop: i++ is executed first, so the value of i is 2; i < 3, enter loop, LED_PIN[i] is 19
    Forth loop: i++ is executed first, so the value of i is 3, which does not meet the loop condition i < 3, exit the for loop.
  */
  for(int i = 0;i < 3;i++){
    pinMode(LED_PIN[i],OUTPUT);
  }
}  
  
void loop() {  
  //flowing water light, The loop pattern is the same as the initialization pin
  for(int i = 0;i < 3;i++){
    digitalWrite(LED_PIN[i],HIGH);
    delay(300);
    digitalWrite(LED_PIN[i],LOW);
  }
}

6. Code Explanation

An array is a collection of elements of the same type: An array contains one or more elements, all of which are of the same type. Yet the number cannot be 0.

Array is divided into one-dimensional and multidimensional array (generally two-dimensional).

Statement: type arr_name [constant value] type: What type of data does the array store arr_name: Array name [constant value]: The number of elements in the array (the size of the array)

HOW to Use PWM Control LED Brightness with ESP32
Previous
HOW to Use PWM Control LED Brightness with ESP32
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us