ESP32 - Potentiometer fade LED

In a previous tutorial, We have learned how a potentiometer triggers a LED. In this tutorial, We are going to learn how to change the brightness of LED according to the potentiometer's output value

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Potentiometer
1×LED
1×220 ohm resistor
1×Breadboard
5×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About LED and Potentiometer

If you do not have knowledge of LED and potentiometer (pinout, how they work, how to connect them to ESP32, how to program for them...), learn about them in the following tutorials:

Wiring Diagram

ESP32 Rotary Potentiometer LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

How To Program

  • Reads the input on analog pin A0 (value between 0 and 1023)
int analogValue = analogRead(A0);
  • Scales it to brightness (value between 0 and 255)
int brightness = map(analogValue, 0, 1023, 0, 255);
  • Sets the brightness LED that connects to pin 3
analogWrite(LED_PIN, brightness);

ESP32 Code

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-potentiometer-fade-led */ const int POTENTIOMETER_PIN = A0; // ESP32 pin connected to Potentiometer pin const int LED_PIN = 21; // ESP32 pin GIOP21 connected to LED's pin // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // declare LED pin to be an output: pinMode(LED_PIN, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // reads the input on analog pin A0 (value between 0 and 1023) int analogValue = analogRead(POTENTIOMETER_PIN); // scales it to brightness (value between 0 and 255) int brightness = map(analogValue, 0, 1023, 0, 255); // sets the brightness LED that connects to pin 3 analogWrite(LED_PIN, brightness); // print out the value Serial.print("Analog: "); Serial.print(analogValue); Serial.print(", Brightness: "); Serial.println(brightness); delay(100); }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Open Serial Monitor on Arduino IDE
  • How to open serial monitor on Arduino IDE
  • Rotate the potentiometer
  • See the LED
  • See the result on Serial Monitor. It looks like the below:
  • COM6
    Send
    Analog: 6, Brightness: 1 Analog: 34, Brightness: 8 Analog: 89, Brightness: 22 Analog: 149, Brightness: 37 Analog: 214, Brightness: 53 Analog: 297, Brightness: 74 Analog: 365, Brightness: 90 Analog: 431, Brightness: 107 Analog: 510, Brightness: 127 Analog: 589, Brightness: 146 Analog: 695, Brightness: 173 Analog: 790, Brightness: 196 Analog: 970, Brightness: 241 Analog: 996, Brightness: 248 Analog: 1018, Brightness: 253 Analog: 1023, Brightness: 255
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

LED on Commercial Products

Small LEDs usually are used to indicate the status of devices. For examples:

Big LEDs usually are used for lighting. They can be combined into groups. For examples:

Potentiometer on Commercial Products

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere