NodeMCU PWM with ESPlorer IDE

Introduction

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 cycles are shown below.

PWM Duty Cycle

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

NodeMCU PWM Functions

Let’s see PWM functions that are used to set PWM on NodeMCU Dev Kit. Note that PWM functionality is available only for pins shown in the above figure for NodeMCU Dev Kit.

pwm.close()

This function used to Quit/close PWM mode for the specified GPIO pin.

Syntax: pwm.close(pin)

Parameters:

pin: pin no. 1 to 12 (except 9, 10, 11)

Returns: null

 

pwm.getclock()

This function is used to get PWM clock (frequency) running on a specified pin.

Syntax: pwm.getclock(pin)

Parameters:

pin: pin no. 1 to 12 (except 9, 10, 11)

Returns: It returns the PWM frequency of pin.

 

pwm.getduty()

This function is used to get the PWM duty cycle on the specified pin.

Syntax: pwm.getduty(pin)

Parameters:

pin: pin no. 1 to 12 (except 9, 10, 11)

Returns: It returns the duty cycle of the pin. Maximum is 1023

 

pwm.setclock()

This function is used to set the PWM frequency of the specified pin.

Note that the PWM frequency set for any pin will apply to all the other PWM pins (if those pins are being used as PWM) as well. Only one PWM frequency is allowed for all the PWM pins.

Syntax: pwm.setclock(pin, clock)

Parameters:

pin: pin no. 1 to 12 (except 9, 10, 11)

clock: PWM frequency in between 1~1000.

Returns: null

 

pwm.setduty()

This function is used to set the duty cycle for a specified pin.

Syntax: pwm.setduty(pin, duty)

Parameters:

pin: pin no. 1 to 12 (except 9, 10, 11)

duty: PWM duty cycle in between 0~1023 (10bit).

Returns: null

 

pwm.setup()

This function is used to set PWM mode to specified pin with frequency and duty cycle.

Syntax: pwm.setup(pin, clock, duty)

Parameters:

pin: pin no. 1 to 12 (except 9, 10, 11)

duty: PWM duty cycle in between 0~1023 (10bit).

clock: PWM frequency in between 1~1000.

Returns: null

 

pwm.start()

This function is used to start PWM on a specified pin.

Syntax: pwm.start(pin)

Parameters:

pin: pin no. 1 to 12 (except 9, 10, 11)

Returns: null

 

pwm.stop()

This function is used to stop the output PWM waveform of a specified pin.

Syntax: pwm.stop(pin)

Parameters:

pin: pin no. 1 to 12 (except 9, 10, 11)

Returns: null

Example

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

LED brightness control through PWM

LED brightness control through PWM

Lua Script for PWM

LEDpin = 6          -- Declare LED pin no.
LEDbrightness = 0   -- Set initial LED brightness
PWMfrequency = 1000 -- Set PWM frequency
PWMDutyCycle = 512  -- Set PWM duty cycle in between 0-1023

pwm.setup(LEDpin, PWMfrequency, PWMDutyCycle)-- Setup PWM
pwm.start(LEDpin)   -- Start PWM on LED pin

while(1)
do
    LEDbrightness = adc.read(0) -- Read pot using ADC for LED brightness
    if LEDbrightness > 1023 then-- Limit LED brightness to max of duty cycle
        LEDbrightness = 1023
    end
    pwm.setduty(LEDpin, LEDbrightness)-- set PWM duty cycle to LED brightness
    print('LED Brightness:',math.floor(100*LEDbrightness/1023))-- print LED brightness
    tmr.delay(100000)   -- timer delay of 100000 us
end

 


Components Used

NodeMCU
NodeMCUNodeMCU
1
ESP12F
ESP12E
1

Downloads

ESP8266 Datasheet Download
Lua Script for NodeMCU PWM Download
Ad