NodeMCU ESP8266 PWM: LED brightness with Potentiometer

Here NodeMCU/ESP8266 PWM(Pulse Width Modulation) is illustrated by controlling the brightness of LED using a Potentiometer. PWM signals are used to drive motors and LEDs and NodeMCU/ESP8266 have ability to make WiFi connectivity and hence they are used in IoT platform as end devices. So NodeMCU/ESP8266 can be used in IoT(Internet of Things) application where PWM is required. The NodeMCU has 8 pins D0 to D8 which can output PWM signal. Also NodeMCU has one analog input pin A0 which can acquire analog signal. Here we will use pin D5 to output PWM signal with varying duty cycle controlled using a potentiometer which is connected to analog pin A0.

See previous tutorial on NodeMCu/ESP8266 PWM: NodeMCU ESP8266 PWM: LED brightness fading where the LED brightness is fade bright and dim using for loop.

Circuit diagram

In the following circuit diagram below, the LED is connected to PWM pin D5 and the 10KOhm potentiometer is connected to the analog pin A0.

NodeMCU ESP8266 LED brightness control using potentiometer circuit diagram

The following shows the proteus simulation of LED brightness control using potentiometer using NodeMCU ESP8266.

The following video shows how the NodeMCU ESP8266 LED brightness control using potentiometer implemented on breadboard works.

Program code

The following is the program code to control the LED brightness with PWM signal from NodeMCU ESP8266 using potentiometer.


const int LED = D5;

void setup() {
  pinMode(LED, OUTPUT);

void loop(){
  int dc;
  dc = analogRead(A0);
  dc = map(dc, 0, 1023, 0, 255);
  analogWrite(LED, dc);
  delay(100);     // wait for 100ms

In the above program, we read the analog signal from the analog input pin A0 of the NodeMCU. Then the value read which ranges from 0 to 1023 is mapped to 0 to 255. After this we have used the analogWrite(LED,dc) to send duty cycle which can range from 0 to 255 to the LED pin. The LED pin is the PWM pin D5.

See other tutorial on NodeMCU/ESP8266:

- NodeMCU LED Blink in Proteus and on Breadboard

- WiFi controlled DC motor with ESP12E

- ESP8266 PWM Example with Javascript and AJAX

Post a Comment

Previous Post Next Post