ESP8266 PWM Example

This ESP8266 PWM example explains how to use the Pulse Width Modulation (PWM)  with the ESP8266.

analogWrite(PIN,VALUE);
ESP8266 can generate PWM on all IO pins. The ESP8266 analogWrite is different than the Arduino Uno. ESP8266 uses 10-bit resolution for PWM generation PWM value varries from 0 to 1023. Arduino Uses 8-Bit Resolution i.e.PWM range is 0-254.

So, in order to use PWM, we can call the analogWrite function simillar to the function also available with Arduino boards.

analogWrite, Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the PWM signal on most pins is approximately 1 KHz.

You do not need to call pinMode() to set the pin as an output before calling analogWrite().

The analogWrite function has nothing to do with the analog pins or the analogRead function.

Before we start actual programming lets have a look at What is PWM?

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (3.3 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of “on time” is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 3.3V controlling the brightness of the LED.

In the graphic below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino’s PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 – 1023, such that analogWrite(1023) requests a 100% duty cycle (always on), and analogWrite(512) is a 50% duty cycle (on half the time) for example.

ESP8266 PWM

LED Fading Program using ESP8266 PWM Function

/*
 * Copyright (c) 2015, circuits4you.com
 * All rights reserved.
/* Generates PWM on Internal LED Pin GPIO 2 of ESP8266*/

#include <ESP8266WiFi.h>
#define LED 2

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

//=======================================================================
//                    Power on setup
//=======================================================================
void setup() {
  Serial.begin(115200);
  pinMode(LED,OUTPUT);
}

//=======================================================================
//                    Main Program Loop
//=======================================================================
void loop() {
  //PWM Value varries from 0 to 1023  
  Serial.println("10 % PWM");
  analogWrite(LED,102);
  delay(2000);

  Serial.println("20 % PWM");
  analogWrite(LED,205);
  delay(2000);

  Serial.println("40 % PWM");
  analogWrite(LED,410);
  delay(2000);

  Serial.println("70 % PWM");
  analogWrite(LED,714);
  delay(2000);

  Serial.println("100 % PWM");
  analogWrite(LED,1024);
  delay(2000);

  //Continuous Fading
  Serial.println("Fadding Started");
  while(1)
  {
    // set the brightness of pin 9:
    analogWrite(LED, brightness);
  
    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;
  
    // reverse the direction of the fading at the ends of the fade:
    if (brightness <= 0 || brightness >= 1023) {
      fadeAmount = -fadeAmount;
    }
    // wait for 30 milliseconds to see the dimming effect
    delay(10);
  }
}
//=======================================================================

Upload program in your ESP and open serial monitor.

Seeing Its Result is really Fun With this technique

Once you get this example running, grab your Node MCU ESP8266 and shake it back and forth in space. What you are doing here is essentially mapping time across the space. To our eyes, the movement blurs each LED blink into a line. As the LED fades in and out, those little lines will grow and shrink in length. Now you are seeing the pulse width.

ESP8266 PWM Output

Leave a Reply