Saurav Avachat
BT22ECE039
EXPERIMENT NO-4
AIM:
a. Intensity variation of LED using PWM channels. Repeat with multiple LEDs.
b. Intensity variation of LED using Potentiometer.
c. Observe the different PWM waveforms on oscilloscope and perform measurements to study
the effect of duty cycles, frequencies, and resolution.
APPARATUS:
• ESP32 Easy IoT Development Board
• LEDs (multiple)
• Potentiometer (10kΩ)
• Oscilloscope
• Jumper wires (male-to-male)
• USB cable (Type-A to Micro-B)
• Arduino IDE software
THEORY:
Pulse Width Modulation (PWM):
PWM is a digital signal modulation technique where the average voltage or power delivered to a
load is controlled by varying the ON-time of the signal while keeping the total cycle period
(frequency) constant. Instead of delivering a continuous analog voltage, the microcontroller
rapidly switches the signal between HIGH and LOW states, and the effective power is
determined by how long the signal stays HIGH in each cycle. This makes PWM highly efficient
and suitable for controlling devices like LEDs, motors, and heaters.
Duty Cycle:
The duty cycle is a measure of the proportion of ON-time to the total period of the PWM signal,
expressed as a percentage. For instance, a 50% duty cycle means the signal is HIGH for half the
time and LOW for the other half. A higher duty cycle delivers more power, resulting in brighter
LEDs or faster motor speeds, while a lower duty cycle reduces power output. By adjusting duty
cycle dynamically, smooth control over intensity, speed, or power can be achieved.
Saurav Avachat
BT22ECE039
Potentiometer with ESP32:
The ESP32 features a 12-bit Analog-to-Digital Converter (ADC), which reads analog input values
in the range of 0 to 4095. A potentiometer can be connected to provide varying analog voltages
based on its wiper position. These values can be mapped to a PWM duty cycle (commonly 0–
255 for 8-bit resolution) to provide proportional control. For example, rotating the potentiometer
changes the ADC value, which in turn adjusts the PWM duty cycle, allowing smooth dimming of
an LED or gradual speed control of a motor.
Oscilloscope Observation:
An oscilloscope can be used to study the PWM waveform generated by the ESP32. The
horizontal axis shows time, while the vertical axis shows voltage levels (HIGH/LOW). By varying
the duty cycle, one can observe how the width of the HIGH pulse changes, even though the
frequency remains constant. This provides a clear visual link between the duty cycle and the
average voltage delivered to the load. Changes in duty cycle directly affect brightness (for LEDs)
or speed (for motors), and experimenting with different frequencies reveals how resolution and
responsiveness are influenced in real-world applications.
PROCEDURE:
• Part A – LED Intensity Control with PWM: Connect LED to GPIO 5 of ESP32.
• Configure a PWM channel with frequency 5000 Hz, resolution 8-bit (0–255).
• Vary the duty cycle to observe LED brightness change.
• Repeat with multiple LEDs on different GPIO pins.
• Part B – LED Intensity Control with Potentiometer:
• Connect potentiometer middle pin to GPIO 34 (ADC pin), side pins to 3.3V and GND.
• Connect LED to GPIO 5.
• Read analog value (0–4095) from potentiometer.
• Map ADC value to duty cycle (0–255) and apply to LED PWM channel.
• Part C – Oscilloscope Observation: Connect oscilloscope probe to LED GPIO pin.
• Vary duty cycle and frequency in code.
• Observe the waveform shape and measure ON/OFF time.
• Compare effects of different duty cycles (25%, 50%, 75%, 100%).
Saurav Avachat
BT22ECE039
CODE:
Code 1: LED Intensity using PWM (Single LED)
#define LED 5
int freq = 5000;
int ledChannel = 0;
int resolution = 8; // 8-bit (0-255)
void setup() {
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(LED, ledChannel);
void loop() {
for (int duty = 0; duty <= 255; duty++) {
ledcWrite(ledChannel, duty);
delay(10);
for (int duty = 255; duty >= 0; duty--) {
ledcWrite(ledChannel, duty);
delay(10);
Code 2: Multiple LEDs with PWM
int leds[3] = {5, 18, 19};
int channels[3] = {0, 1, 2};
void setup() {
for (int i = 0; i < 3; i++) {
ledcSetup(channels[i], 5000, 8);
ledcAttachPin(leds[i], channels[i]);
}
Saurav Avachat
BT22ECE039
void loop() {
for (int duty = 0; duty <= 255; duty++) {
for (int i = 0; i < 3; i++) {
ledcWrite(channels[i], duty);
delay(10);
Code 3: LED Intensity using Potentiometer
int led=18;
int potpin = 12;
const int freq = 500;
const int resol = 8;
const int channel = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(led,OUTPUT);
ledcAttachChannel(led,freq,resol,channel);
void loop() {
// put your main code here, to run repeatedly:
int val = analogRead(potpin);
Serial.print("Pot value: ");
Serial.println(val);
int x= map(val,0,4095,0,255);
Serial.print("Duty Cycle Value: ");
Serial.println(x);
Saurav Avachat
BT22ECE039
ledcWriteChannel(channel,x);
RESULTS:
LED with different Duty cycle
Saurav Avachat
BT22ECE039
Using the Potmeter
Obsevation with Different duty cycles and resolution,frequency
Saurav Avachat
BT22ECE039
Saurav Avachat
BT22ECE039
1. The LED brightness successfully varied using PWM by changing duty cycle.
2. Multiple LEDs were dimmed and brightened independently using separate PWM
channels.
3. Potentiometer provided smooth control of LED intensity mapped through ADC
input.
4. On oscilloscope, PWM signals showed variation in duty cycle, frequency, and
resolution, confirming theoretical understanding.
CONCLUSION:
This experiment demonstrated the use of PWM on ESP32 to control LED brightness.
Duty cycle variation controlled intensity effectively, and potentiometer-based ADC input
enabled analog dimming. Oscilloscope observation validated the changes in waveform
with different duty cycles and frequencies, Resolution. This highlights the importance of
PWM in real-world applications like LED dimming, motor speed control, and signal
modulation.