Custom Display Manufacturing in China: A Complete Guide to Sourcing Screens
Whether you are developing an IoT device, a medical instrument, or consumer electronics, the display is often the most critical

The ESP32 microcontroller is a powerhouse for IoT and embedded systems projects, renowned for its robust feature set and wireless capabilities. One of its most fundamental and powerful tools for interacting with the analog world is Pulse Width Modulation, or PWM.
While a digital pin can only be ON (5V/3.3V) or OFF (0V), many components like LEDs, servos, and motors require variable voltage or precise timing for control. This is where the ESP32‘s PWM capabilities excel. This tutorial will provide an in-depth exploration of how to harness the ESP32‘s PWM functionality, from basic concepts to advanced implementation.
Pulse Width Modulation is not a true analog signal. Instead, it is a digital technique to simulate an analog result by rapidly switching a digital signal on and off. The key parameters are:
Frequency: The number of complete on/off cycles per second (Hz). A higher frequency means the cycle repeats faster.

Duty Cycle: The percentage of one period where the signal is HIGH (ON). A 0% duty cycle is always OFF, a 100% duty cycle is always ON, and a 50% duty cycle is ON half the time.

Resolution: The precision with which the duty cycle can be set. It is expressed in bits. A higher resolution allows for finer control over the pulse timing.
The average voltage delivered to a load is directly proportional to the duty cycle. By varying the duty cycle, we effectively control the average power. 
Unlike simpler microcontrollers (e.g., Arduino UNO) which may have a limited number of hardware PWM pins, the ESP32 is incredibly flexible. Its key features include:
Dedicated LED PWM Controller: The ESP32 has a dedicated hardware module specifically for generating PWM signals, ensuring consistent and glitch-free operation without CPU intervention.
High Resolution: It supports up to 16-bit resolution, allowing for extremely fine control (65,536 discrete duty cycle levels).
High Frequency: It can generate signals with frequencies up to tens of kHz, which is essential for applications like driving brushless DC motors or switching power supplies efficiently.
Pin Flexibility: Almost any GPIO pin can be configured as a PWM output, providing tremendous design flexibility.

The Arduino IDE analogWrite() function is not natively available for ESP32 in the same way. Instead, we use the ESP32-specific LED Control (LEDC) functions.
The setup process involves two main steps: configuration and output.
ledcSetup())Before outputting a signal, you must set up a PWM channel with your desired frequency and resolution.
cpp
ledcSetup(channel, frequency, resolution_bits);
channel: The LEDC channel (0-15) to configure.
frequency: The desired PWM frequency (e.g., 5000 for 5 kHz).
resolution_bits: The resolution in bits (1-16, e.g., 8 bits for 256 steps).
ledcAttachPin())This function links the configured channel to a specific physical GPIO pin.
PWM_pin: The GPIO pin you want to use as PWM output (e.g., GPIO 16).
channel: The channel number you configured in Step 1.
ledcWrite())This function outputs the PWM signal with the specified duty cycle to the attached pin.
ledcWrite(channel, duty_value);
channel: The channel number.
duty_value: An integer value between 0 and (2^resolution – 1). For 8-bit resolution, this is 0-255.
Let’s put theory into practice by creating a classic fading LED effect.
LED
220Ω Resistor
Breadboard and Jumper Wires
Connect the anode of the LED (long leg) through the 220Ω resistor to GPIO 16. Connect the cathode (short leg) to GND.
// Define the PWM Channel, GPIO Pin, and PWM parameters
const int pwmChannel = 0; // Select PWM channel 0
const int pwmPin = 16; // GPIO 16 for PWM output
const int freq = 5000; // PWM frequency of 5 KHz
const int resolution = 8; // 8-bit resolution (duty cycle 0-255)
void setup() {
// Configure the PWM functionalit
ledcSetup(pwmChannel, freq, resolution);
// Attach the channel to the GPIO pin
ledcAttachPin(pwmPin, pwmChannel);
}
void loop() {
// Increase LED brightness gradually
for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) {
ledcWrite(pwmChannel, dutyCycle);
delay(10);
}
// Decrease LED brightness gradually
for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle–) {
ledcWrite(pwmChannel, dutyCycle);
delay(10);
}
}
This code will smoothly fade the LED on and off by incrementally changing the duty cycle.
The utility of PWM extends far beyond dimming lights:
Servo Motor Control: Servos use PWM pulses of a specific duration (typically 1-2ms) within a 20ms period to determine their angle. The ESP32’s high resolution allows for very precise servo positioning.
DC Motor Speed Control: Coupled with an H-Bridge motor driver IC (like the L298N), PWM is the standard method for controlling the speed and direction of DC motors.
Audio Generation: By generating PWM signals at audio frequencies (20Hz – 20kHz) and filtering the output with a low-pass filter, the ESP32 can synthesize simple tones and sounds.
Power Regulation: PWM is the cornerstone of modern switched-mode power supplies (SMPS) and voltage regulators, allowing for highly efficient power conversion.
Mastering Pulse Width Modulation is a critical skill for anyone working with the ESP32. Its dedicated hardware, high resolution, and pin flexibility make it superior to many other microcontrollers for generating precise digital control signals. By understanding the ledcSetup(), ledcAttachPin(), and ledcWrite() functions, you can unlock a world of possibilities, from creating simple lighting effects to building complex robotic systems. Integrate these techniques into your next ESP32 project to achieve professional-grade control over analog devices.
======================================
Whether you are developing an IoT device, a medical instrument, or consumer electronics, the display is often the most critical
Searching for a PCB assembly factory in China can feel overwhelming. Thousands of factories in Shenzhen alone. Each one claiming to
ESP32s.com – Your Local Partner in China’s Electronics Hub “I walk the floor so you don’t have to. Here is
The world of AI is buzzing. You have likely heard of OpenClaw, the open-source AI agent that has exploded on GitHub,
If you manufacture electronics—whether IoT devices, consumer gadgets, medical instruments, or industrial controls—you already know that China’s Pearl River Delta (PRD) is
If you’re sourcing electronics from China, you’ve likely faced the same challenges: unreliable suppliers, quality inconsistencies, communication gaps, and the
No account yet?
Create an Account