Trybotics Logo

ESP8266 Using PWM With Potentiometer

DESCRIPTION

For those who are not accustomed to electronics, PWM means power control. And in this assembly, we show how to use it to control the light intensity of a LED, similar to a dimmer on a lamp, with options to darken and brighten.

This mechanism also allows you, for example, to connect a driver to a motor. This is just one of many possibilities.

- Note: PWM stands for Pulse-Width Modulation.

Description:

The assembly consists of a potentiometer, which is a variable resistor, monitored by the ESP. In this scheme, I am using the same source code that I would use with an Arduino. Due to its many advantages, we use Arduino IDE with ESP in other projects as well.

In the assembly, the ESP is connected to USB only for power supply. We also have the cursor pin, which is the pin of the center potentiometer, connected in the AD port, and the positive and negative.

As the voltage varies, it is possible to read a different value in the AD. Therefore, by turning the potentiometer, it is possible to increase or decrease the brightness of the LED.

Description:

The electrical scheme is very simple: using the ESP8266 in the configuration of the NodeMCU, we will power the USB. So here, the potentiometer has to be connected from one end to the negative, and the other from the positive. The medium, which is the cursor, stays on in ADC 0, because this ESP only has a port that reads analog values.

Description:

Description:

Setup

In the Setup function, we define the behavior of the pins that we use, in this case the LED and POTENTIOMETER.

void setup()
{ Serial.begin(115200) // Instrução para colocar o gpio que iremos utilizar como entrada, // podemos fazer a leitura nesse pino pinMode(A0, INPUT); // A0 é uma constante que indica o pino que ligamos nosso potenciômetro // Instrução para colocar o gpio que iremos utilizar como saída, // podemos alterar seu valor livremente para HIGH ou LOW pinMode(LED_BUILTIN, OUTPUT); // LED_BUILTIN é uma constante que indica o LED do ESP8266 }

Loop

In this function, the logic is to read the POT value and assign this value (which is the intensity of the brightness) in the LED.

void loop()
{ // faz a leitura do pino A0 (no nosso caso, o potenciômetro, retorna um valor entre 0 e 1023) int potencia = analogRead(A0); Serial.println(potencia); // como o LED no ESP8266 trabalha de maneira contrária, ou seja, quanto maior o valor atribuído, menor a intensidade. Faremos o cálculo para aumentarmos o brilho conforme girarmos o potenciômetro em sentido horário. potencia = 1023 - potencia; // atribui o valor lido do potenciômetro para configurar a intensidade do brilho do LED analogWrite(LED_BUILTIN, potencia); }


YOU MIGHT ALSO LIKE