NodeMCU PWM with Arduino IDE

NodeMCU based ESP8266 has a PWM feature on its GPIO pins. PWM is generally used to control the speed of the DC motors or servo motors.

118.8k views2 min readUpdated Aug 8, 2023
NodeMCU PWM with Arduino IDE

Introduction to PWM

Pulse Width Modulation (PWM) is a technique by which the width of a pulse is varied while keeping the frequency of the wave constant.

LED PWM

 

PWM Generation

A period of a pulse consists of an ON cycle (VCC) and an OFF cycle (GND). The fraction for which the signal is ON over a period is known as a duty cycle.

 

\mathbf{Duty Cycle (percentage) = \frac{Ton}{Total Period} \ast 100}

 

E.g. A pulse with a period of 10ms will remain ON (high) for 2ms.Therefore, the duty cycle will be

D = 2ms / 10ms = 20%

Through the PWM technique, we can control the power delivered to the load by using the ON-OFF signal. The PWM signals can be used to control the speed of DC motors and to change the intensity of the LED. Moreover, it can also be used to generate sine signals. Pulse Width Modulated signals with different duty cycle are shown below.

PWM duty cycle waveform
PWM Duty cycle

NodeMCU based ESP8266 has the functionality of PWM interfaces via software programming. It is achieved with the timer interruption method. The PWM frequency range for ESP8266 is adjustable up to 1KHz.

 

NodeMCU PWM Pins

NodeMCU PWM Pins
NodeMCU PWM Pins

 

Arduino function for NodeMCU PWM

analogWrite(pin, dutycycle): Enables software PWM on the specified pin. duty cycle is in the range from 0 to PWMRANGE, i.e. 1023 by default.

 

analogWrite(pin, 0): Disables PWM on the specified pin.

 

analogWriteRange(new_range): This function is used to change the PWM range (duty cycle).

 

analogWriteFreq(new_frequency): PWM frequency is 1kHz by default. Call this function to change it with new frequency.PWM frequency is in the range of 1 – 1000Khz.

 

Example

Let’s write an Arduino sketch to set PWM on 6thpin of NodeMCU and vary its duty cycle with the potentiometer connected to the ADC pin of NodeMCU. Here we connect LED on PWM pin to visualize the effect (Brightness of LED) of PWM variation.

Interfacing Diagram

NodeMCU PWM LED Brightness Control
LED Brightness Control using NodeMCU PWM

 

NodeMCU PWM Code using Arduino IDE

uint8_t LEDpin = D6;

/* By default PWM frequency is 1000Hz and we are using same 
   for this application hence no need to set */

void setup(){
  Serial.begin(9600);
  analogWrite(LEDpin, 512);  /* set initial 50% duty cycle */
}

void loop(){
  uint16_t dutycycle =  analogRead(A0); /* read continuous POT and set PWM duty cycle according */
  if(dutycycle > 1023) dutycycle = 1023;/* limit dutycycle to 1023 if POT read cross it */
  Serial.print("Duty Cycle: ");  Serial.println(dutycycle);
  analogWrite(LEDpin, dutycycle);
  delay(100);
}

Components Used

Powered byMouser Electronics

Downloads

Comments7

Join the discussion — share a question or tip.

  • JE
    JenaNisha

    Hi i use esp-07 and Free-RTOS-SDK I changed cpu_freq to 160MHz now i want to enable the pwm with frequency 40000Hz my code is uint32_t duties [1]={500}; const uint32_t pin_num [1]={12}; pwm_init(1000, duties , 1, pin_num ); pwm_start(); vTaskDelay(1000); pwm_set_period (25); //us 40Khz but Does not work "pwm_set_period " There is no change in frequency //from 1KHZ

  • FA
    faisaltanoli7272

    can i use RSV pin for PWM ? as you have marked him as PWM pin? kindly reply

    • LO
      lokeshc

      yes, you can.

    • YU
      yuganthachathu

      How should i define rsv pin if i want to use it as a pwm pin? Pin number? If possible please give me an example .

  • JH
    jhmluna· edited

    Hello. Can You help me with one doubt? The ESP8266 datasheet says that it has only 4 PWM pins, but your post says that it has 9. How can this possible? The link for datasheet is: https://www.espressif.com/sites/default/files/documentation/0a-esp8266ex_datasheet_en.pdf

    • AU
      authorized

      I think this post functions saying about software pwm not hardware pwm

  • SO
    sompongindustrial

    THANK YOU VERY MUCH FOR HELPING ME.....