ad space

PWM example using ESP8266 ESP12E

 In this ESP8266 tutorial we show how to use PWM(Pulse Width Modulation) of ESP8266 WiFi chip to control the brightness of a LED via software and using potentiometer. We will use ESP12E module which is a module version of ESP8266.

There are various PWM application such as DC motor control using PWM, signal generation and telecommunication. Using ESP8266 wifi module to control motors can be useful in IoT(Internet of Things) application at home and in industrial IoT.

There are few ESP8266 related PWM concepts and function which are explained as under.

 ESP8266 PWM resolution and analogWriteRange() function

The ESP8266 based WiFi module have 10 bits of resolution which gives range from 0 to 1023. This range can be changed using the analogWriteRange() function. 

ESP8266 PWM frequency and analogWriteFreq() function

The ESP8266 based WiFi module has default PWM frequency set to 1KHz. This can be changed using the analogWriteFreq() function. The range of PWM frequency is from 100 Hz and 1 kHz.

 ESP8266 PWM generation and analogWrite() function

To generate PWM signal using ESP8266 we can use the analogWrite(Pin, DutyCycle) function where the parameters are the Pin number and the Duty Cycle which can range from 0 to 1023 in default resolution.

LED brightness control using PWM with ESP8266 WiFi Module ESP12E

The following picture shows the connection of ESP8266 wifi module with LED connected to GPIO2.


 The following video demonstrates LED brightness control using PWM via program.


 The following shows circuit wiring diagram for LED brightness control using ESP12E wifi module.

circuit diagram LED ESP12E wifi module

In the above schematic diagram, a LED is connected to the GPIO pin 2. 

ESP8266 Program code for LED brightness control via program

The following is the program source code to control the brightness of the LED at pin 2 via program. The program first increases the duty cycle of the PWM signal and hence increases the brightness of the LED light from lowest to maximum duty cycle using the analogWrite() function. Then the program decreases the duty cycle from highest value to lowest and hence the LED brightness decreases. The PWM frequency is set to 500 using analogWriteFreq() function.

const int LEDpwm = 2;

void setup() {
pinMode(LEDpwm, OUTPUT);
Serial.begin(115200);
analogWriteFreq(500);
}

void loop() {
for(int pwmValue=0; pwmValue <= 1023; pwmValue++){
analogWrite(LEDpwm, pwmValue);
delay(1);
}
delay(10);
for(int pwmValue=1023; pwmValue >= 0; pwmValue--){
analogWrite(LEDpwm, pwmValue);
delay(1);
}
}

 

ESP8266 Program code for LED brightness control using Potentiometer

 In this ESP8266 PWM example, a 10KOhm potentiometer is used to control the brightness of a LED which is connected to GPIO pin 13. 

 

ESP12E potentiometer LED

The following video demonstrates LED brightness control using ESP8266 and a Potentiometer.


 The following shows the schematic diagram of connecting potentiometer and LED to ESP12E module.

schematic diagram of potentiometer, LED, ESP12E module

The following is the program code to control the LED brightness with potentiometer connected to ESP8266 module.

const int LEDpwm = 13;

void setup() {
pinMode(LEDpwm, OUTPUT);
Serial.begin(115200);
analogWriteFreq(500);
}

void loop() {
int a = analogRead(A0);
analogWrite(LEDpwm, a);
}


Post a Comment

Previous Post Next Post