ESP32 S3 UNO - Potentiometer fade LED

This ESP32 S3 Uno potentiometer fade LED tutorial shows you how to dim an LED by turning a knob. You will wire a potentiometer and an LED to the ESP32 S3 Uno form board, read the knob position with Arduino code, and use it to set the LED brightness with PWM. When you turn the knob, the LED fades smoothly from off to full brightness.

In this tutorial, you will:

  1. Wire a potentiometer and an LED to the ESP32 S3 Uno
  2. Read the potentiometer value with analogRead()
  3. Convert the 0-4095 reading to a brightness level with map()
  4. Control LED brightness with analogWrite() (PWM)
ESP32 S3 Uno potentiometer fade LED

Hardware Preparation

1×ESP32 S3 Uno-form Board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Potentiometer
1×Alternatively, 10k Ohm Trimmer Potentiometer
1×Alternatively, Potentiometer Kit
1×Alternatively, Potentiometer Module with Knob
1×LED Kit
1×LED (red)
1×LED Module
1×Alternatively, Button and LED Kit
1×220Ω Resistor
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.

Overview of LED and Potentiometer

This project joins two simple parts: a potentiometer that gives an input, and an LED that shows an output. If you have not used them before, it helps to learn each one on its own first.

The tutorials below explain the pinout, how each part works, and how to program it:

ESP32 S3 Uno Pinout

The image below shows the pinout diagram of the ESP32 S3 Uno form board. Use it to find the Uno header pins (D3, A0, 3.3V…) and their GPIO numbers when you wire the circuit.

ESP32 S3 Uno pinout diagram

Wiring Diagram

The potentiometer goes to an analog pin, and the LED goes to a PWM pin through a 220 ohm resistor. Follow the diagram and the table below.

The wiring diagram between ESP32 S3 Uno Rotary Potentiometer LED

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Uno Pin
Potentiometer VCC 3.3V
Potentiometer GND GND
Potentiometer Output A0 (GPIO2)
LED Anode (+) via 220 ohm resistor D3 (GPIO17)
LED Cathode (-) GND

WARNING

Power the potentiometer from the 3.3V pin, not 5V. The ESP32 S3 Uno pins are not 5V tolerant, so a 5V signal on A0 can damage the board.

How To Program

The sketch does three small jobs in a loop. It reads the knob, scales the number, and sends it to the LED.

Read the potentiometer

Read the voltage on analog pin A0 (GPIO2). The ESP32 S3 Uno has a 12-bit ADC, so the value goes from 0 to 4095.

int adc_value = analogRead(POTENTIOMETER_PIN);

Convert to brightness

PWM brightness uses a range from 0 to 255, so scale the reading with map().

int brightness = map(adc_value, 0, 4095, 0, 255);

Set the LED brightness

Write the brightness to the LED on pin D3 (GPIO17) with a PWM signal.

analogWrite(LED_PIN, brightness);

ESP32 S3 Uno Code

The sketch below reads the potentiometer every 100 ms, turns the reading into a brightness level, and sets the LED with PWM. It also prints both values to the Serial Monitor, so you can see what happens when you turn the knob.

/* * This ESP32 S3 Uno code was developed by newbiely.com * * This ESP32 S3 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3-uno/esp32-s3-uno-potentiometer-fade-led */ #define LED_PIN 17 // The ESP32 S3 Uno pin GPIO17 (D3) connected to the LED #define POTENTIOMETER_PIN 2 // The ESP32 S3 Uno pin GPIO2 (A0) connected to the potentiometer void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); // declare LED pin to be an output: pinMode(LED_PIN, OUTPUT); } void loop() { // reads the input on analog pin GPIO2 (A0) (value between 0 and 4095) int adc_value = analogRead(POTENTIOMETER_PIN); // scales it to brightness (value between 0 and 255) int brightness = map(adc_value, 0, 4095, 0, 255); // sets the brightness of the LED that connects to pin GPIO17 (D3) analogWrite(LED_PIN, brightness); // print out the value Serial.print("Analog: "); Serial.print(adc_value); Serial.print(", Brightness: "); Serial.println(brightness); delay(100); }

In setup(), the code calls analogSetAttenuation(ADC_11db). By default, the ESP32 S3 Uno ADC can only measure a small voltage. With 11 dB attenuation, the analog pin reads the full range from 0V up to about 3.3V. Without this line, the reading hits 4095 too early and the LED reaches full brightness before the knob reaches the end.

Detailed Instructions

Follow these steps one by one:

  1. New to the ESP32 S3 Uno? Follow ESP32 S3 Uno - Getting Started first.
  2. Wire it up as shown in the diagram.
  3. Connect the board to your computer with a USB Type-C cable.
  4. Open Arduino IDE, choose the ESP32S3 Dev Module board and the correct COM port.
  5. Copy the code above and paste it into Arduino IDE.
  6. Upload the code by clicking the Upload button.
  7. Open the Serial Monitor and set it to 9600 baud.
  8. Turn the potentiometer slowly from one end to the other.
  9. Watch the LED get brighter or dimmer, and check the values on the Serial Monitor:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
Analog: 0, Brightness: 0 Analog: 140, Brightness: 8 Analog: 360, Brightness: 22 Analog: 600, Brightness: 37 Analog: 860, Brightness: 53 Analog: 1190, Brightness: 74 Analog: 1460, Brightness: 90 Analog: 1725, Brightness: 107 Analog: 2045, Brightness: 127 Analog: 2360, Brightness: 146 Analog: 2785, Brightness: 173 Analog: 3165, Brightness: 197 Analog: 3880, Brightness: 241 Analog: 3990, Brightness: 248 Analog: 4075, Brightness: 253 Analog: 4095, Brightness: 255
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2
  1. Tip: If the LED changes brightness only on one part of the knob turn, check that the analogSetAttenuation(ADC_11db) line is in setup() and that the potentiometer is powered from 3.3V.

Video Tutorial

Watch the video below to see this ESP32 S3 Uno project step by step.

FAQ

Why does the ESP32 S3 Uno read 0 to 4095 instead of 0 to 1023?

The ESP32 S3 Uno has a 12-bit ADC, so analogRead() returns a number from 0 to 4095. Boards like the UNO R4 use 0 to 1023 by default. If you copy code from those boards, change 1023 to 4095 in the map() call, or the LED will be at full brightness for most of the knob turn.

Why does the code call analogSetAttenuation(ADC_11db)?

It sets the ESP32 S3 Uno ADC input range to about 0V to 3.3V, so the full turn of the potentiometer maps to 0 to 4095. Without it, the range is smaller and the reading reaches 4095 too early. The UNO R4 does not need this line, because it is only for ESP32 boards. Keep the potentiometer output at 3.3V or less.

Can I power the potentiometer from 5V on the ESP32 S3 Uno?

No. With 5V on VCC, the potentiometer output can reach 5V, and the ESP32 S3 Uno pins are not 5V tolerant. This can damage the analog pin. Use the 3.3V pin instead.

Which ESP32 S3 Uno pins can dim an LED?

Almost every Uno header pin supports PWM, so analogWrite() works on D2 to D13. This tutorial uses D3, which is GPIO17. In code, always write the GPIO number, like 17, not D3.

Why does the LED flicker or jump at some knob positions?

The ESP32 S3 Uno ADC is a bit noisy, so the reading can move by a few steps even when the knob does not move. You can smooth it by averaging several readings, or by adding a small 0.1 µF capacitor between the potentiometer output and GND.

What if my project needs more pins than the Uno headers give?

The ESP32 S3 Uno has two extra rows of holes with more GPIOs. GPIO15 and GPIO16 support I/O, PWM and analog, so they are a good choice for an extra potentiometer. GPIO45 and GPIO35 to GPIO42 support I/O and PWM, so they work well for extra LEDs. GPIO47 and GPIO48 are output only with PWM. Solder pin headers to these holes and write the GPIO number in code. Be careful with two things. First, GPIO47 and GPIO48 may run at 1.8V instead of 3.3V on some modules (especially ones with "V" in the name, like R8V or R16V), so 3.3V devices can be damaged or act strangely. Second, GPIO0, GPIO3 (D6), GPIO45 and GPIO46 (D9) are boot strapping pins, and a wrong connection can stop the board from booting or uploading code. Use the other extra pins first.

Troubleshooting

Problem Possible Cause Solution
No COM port shows in Arduino IDE USB driver missing or charge-only cable Install the CP210x or CH340 driver and use a USB data cable
Upload fails or times out Board is not in download mode Hold the BOOT button, press and release RESET, then upload again
LED does not light at all LED is reversed or the resistor is not connected Put the long leg (anode) toward D3 (GPIO17) and check the 220 ohm resistor
LED is at full brightness for most of the knob turn Code still maps 0 to 1023 or the attenuation line is missing Use map(adc_value, 0, 4095, 0, 255) and keep analogSetAttenuation(ADC_11db) in setup()
Reading stays at 0 or 4095 Potentiometer output is not on A0 or VCC/GND is loose Connect the middle pin to A0 (GPIO2) and the outer pins to 3.3V and GND
LED brightness jumps or flickers ADC noise or a loose breadboard wire Press the wires in firmly and average several readings in code
Serial Monitor shows strange characters Wrong baud rate Set the Serial Monitor to 9600 baud

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!