ESP8266 - Potentiometer fade LED

In a previous tutorial, we discovered how a potentiometer can be used to trigger a LED. This tutorial instructs you how to use ESP8266 to adjust the brightness of the LED based on the output value of the potentiometer.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Potentiometer
1×Potentiometer Kit
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) Screw Terminal Expansion Board for ESP8266

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
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.

Overview of LED and Potentiometer

If you are unfamiliar with LED and potentiometer (including pinout, functioning, programming, etc.), the following tutorials can help you out:

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Rotary Potentiometer LED

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program

  • Gets the value from the analog pin A0, which is between 0 and 1023.
int analog_value = analogRead(A0);
  • Adjusts the brightness to a value between 0 and 255.
int brightness = map(analog_value, 0, 1023, 0, 255);
  • Sets the brightness of the LED connected to pin 3.
analogWrite(LED_PIN, brightness);

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-potentiometer-fade-led */ const int POTENTIOMETER_PIN = A0; // The ESP8266 pin connected to Potentiometer pin const int LED_PIN = D8; // The ESP8266 pin connected to LED's pin // The setup function runs once on reset or power-up void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // declare LED pin to be an output: pinMode(LED_PIN, OUTPUT); } // The loop function repeats indefinitely. void loop() { // reads the input on analog pin A0 (value between 0 and 1023) int analog_value = analogRead(POTENTIOMETER_PIN); // scales it to brightness (value between 0 and 255) int brightness = map(analog_value, 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(analog_value); Serial.print(", Brightness: "); Serial.println(brightness); delay(100); }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the IDE to transfer the code to the ESP8266.
  • Open the Serial Monitor.
  • Turn the potentiometer.
  • Check out the LED.
  • Check out the result on the Serial Monitor.
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  

※ NOTE THAT:

This tutorial uses the analogRead() function to get data from an ADC (Analog-to-Digital Converter) that's connected to a sensor or another part. The ESP8266's ADC works well for projects where you don't need very precise readings. But remember, the ESP8266's ADC isn't very accurate for detailed measurements. If your project needs to be very precise, you might want to use a separate ADC like the ADS1115 with the ESP8266, or use Arduino like the Arduino Uno R4 WiFi, which has a more reliable ADC.

Video Tutorial

※ 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!