X
Home > Blog > STEM for ESP32 > HOW to Use PWM Control LED Brightness with ESP32

HOW to Use PWM Control LED Brightness with ESP32

By r December 17th, 2024 241 views

1. Overview



The LED brightness can be controlled via voltage. There are two ways to control kidsIOT board pin output voltage: a pin output with ADC function, or use PWM(“Pulse Width Modulation”). For kidsIOT board, all GPIO pins support PWM.

2. Principle

What is PWM?

Pulse width modulation is a solution that simulates the change of analog signals through digital ones.

Pulse width is the high level in a complete square wave cycle. So, pulse width modulation is to adjust the high level (of course, in other words, low level is also adjusted because the cycle is fixed).

a7

  • PWM frequency

    the number of times the signal going from high level to low level and back to high level in 1 second (one cycle), that is, how many cycles there are in a second.

    Unit: Hz

    Expression: 50Hz 100Hz

  • PWM cycle

    T=1fCycle=1frequency

    If the frequency is 50Hz, the cycle will be 20ms, i.e., there are 50 PWM cycles in one second.

  • PWM duty cycle

    the ratio of high level time to the whole cycle time.

    • Unit: %(1% ~ 100%)

    • Cycle: The time of a pulse signal. The number of cycles in 1s equals the frequency.

    • Pulse width time: high level time.

a8

The relationship between duty cycle and LED brightness

The longer the high level time is, the greater the duty cycle will be, and the brighter the LED will be.

3. Wiring Diagram

b1

4. Test Code

/*  
  Project name: 2_PWM_LED 
  Function: Control LED brightness, from dark to light and then from light to dark. 
  Author: keyestudio    
  Hardware connection:  
    - Connect RED LED to digital port 17  
  Library:  
    - none (no need to import additional library)  
  Cautions:  
    - Ensure the LED is connected to digital port IO17  
    - 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
  
void setup() {  
   // initialize PWM
  pinMode(RED_LED_PIN,OUTPUT);
}  
  
void loop() {  
  for(int i = 0;i <= 255;i++){
    analogWrite(RED_LED_PIN,i);
    delay(10);
  }
  for(int i = 255;i >= 0;i--){
    analogWrite(RED_LED_PIN,i);
    delay(10);
  }
}


5. Test Result

After uploading the test code, the red LED goes from dark to light and then from light to dark, repeatedly.

d2


6. Code Explanation

6.1 analogWrite()

analogWrite() function outputs analog values (PWM waves). It controls the brightness of the LED or the rotation speed of motor. After calling analogWrite(), the pin will generate a stable rectangular wave with the specified duty cycle until the function (or digitalRead() / digitalWrite()) is called next time on the same pin

Syntax: analogWrite(pin, value)

  • pin: the Arduino pin to write to. Allowed data types:int

  • value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types:int

Please refer to the official website for details: analogWrite() | Arduino Documentation

6.4 for()

for loop is used to execute codes in it for a specified number of times. for is especially useful for scenarios where the number of loops is known.

Here is the syntax of for statement:

for (Initialize the expression; Conditional expression; Iterative expression) {  
      // Loop: Block of test code to be executed repeatedly
}

  • Initialize the expression: Execute before the loop starts, usually to initialize one or more loop control variables.

  • Conditional expression: Check before each iteration of the loop. If the condition is true (non-zero), the loop is executed; If it is false (zero), the loop exits.

  • Iterative expression: Executes at the end of each loop iteration and is usually used to update loop control variables.

a10

① Set the initial value of the loop, execute it only once, and then enter ②.

② To determine whether the loop condition is met. Here i <= 255, if i is less than or equal to 255, enter the loop ③.

③ Repeat the code. Put the code that needs to loop here. This code needs to control the PWM value from 0 to 255, so we just treat the value of i as PWM. Then enter ④.

④ i++ is i plus one over the original value, it equals “i = i +1”. Similarly, i- - is “i = i - 1”. After that, enter ⑤.

⑤ After adding one (or subtracting one) to the value of i, determine whether the value of i is less than or equal to 255. If yes, continue into the loop ③, if not, exit the for loop.

Please refer to the official website for details: for | Arduino Documentation

++ , --,<= ,>= official website operator explanation: Language Reference | Arduino Documentation

a9

HOW to Complete LED BLINK with ESP32
Previous
HOW to Complete LED BLINK 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