NodeMCU ESP8266 PWM: LED brightness fading

The NodeMCU ESP8266 board is designed to connect to sensor, send PWM signal to drive motors or LED and is able to connect to a WiFi network. For this reason NodeMCU is used in building Internet of Things(IoT) application and as IoT platform together with application like NodeRed. Here NodeMCU ESP8266 PWM(Pulse Width Modulation) is illustrated by fading the brightness bright and dim of a LED using the PWM signal from NodeMCU.

Pins and Function related to PWM for NodeMCU/ESP8266

The NodeMCU/ESP8266 has 6 pins which are capable to output PWM signals. These are generally referred to as D0 to D8 but have different GPIO names. By default the PWM signal can have frequency from 0 to 1KHz. We can however change the frequency to different value using the command analogWriteFreq(value) where value is the frequency which we can set upto 4KHz. Similarly we can change the duty cycle of the PWM signal using the command analogWriteRange(value) where value is the duty cycle we want to set. We can also disable to PWM on specific pin by using the command analogWrite(pin,0) where pin is the pin name(D0 to D8). The main command or function used to send PWM signal on a pin is the command analogWrite(pin, dutycycle).

Circuit Diagram

The following is the circuit diagram of the connecting a LED to the PWM pin 5(D5) of NodeMCU/ESP8266 via 220Ohm resistor.

NodeMCU ESP8266 fading LED brightness circuit diagram

The same NodeMCU/ESP8266 LED brightness fading circuit implemented on the breadboard is shown below.

NodeMCU ESP8266 fading LED brightness on breadboard

The following video shows the NodeMCU ESP8266 LED brightness fading dim and bright simulation in Proteus.


The following video shows the NodeMCU ESP8266 PWM LED brightness fading implemented on breadboard.


 Program Code

The following is NodeMCU ESP8266 program code for fading the brightness of the LED from dim to bright and from bright to dim again.


//source:https://www.ee-diary.com

const int LED = D5;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}


void loop(){
  for(int dc = 0; dc <= 255; dc++){
    analogWrite(LED, dc);
    Serial.println(dc);                       
  }
  delay(100);   // wait for 100ms
  for(int dc = 255; dc >= 0; dc--){
    analogWrite(LED, dc);
    Serial.println(dc);
  }
  delay(100);   // wait for 100ms
}

 In the above code, the PWM pin D5 is given an alias name LED. In the setup() function we have declared the LED pin as output and also setup the serial port with baud rate 9600bps. In the loop() function we have used for loop to first increase the brightness of the LED by increasing the PWM duty cycle from 0 to 255. Here the function analog(LED,dc) is used where dc is the duty cycle. 0 means 0% and 255 is 100%. After the dim to highest brightness a delay of 100ms is used. Afterwards, we decrease the duty cycle from 255 to 0. This decrease the brightness from the highest brightness to lowest brightness that is LED is off.

See next tutorial on NodeMCU PWM:NodeMCU ESP8266 PWM: LED brightness with Potentiometer

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