
Potentiometer is connected to GPIO 4 as in 2nd example.
#ARDUINO PWM MOTOR CODE CODE#
With the same code uploaded, we can control the speed of a motor.

Inside loop() function we use the ledcWrite() function, so that according to pot variation LED brightness varies. LED connected to GPIO15 is faded according to pot variation. One end of pot is connected to +3.3v, the other end to GND. In the second example let us add a potentiometer ( 10k value) to GPIO4. Once code is uploaded, you can see the inbuilt blue LED fading. Now upload the code.Press & Hold the Boot button for a smooth upload. Inside the loop we vary the duty cycle from 0 to 255 to increase brightness of LED gradually & when it reaches vaLue 255, we decrease it thus fading down the LED. LedcSetup(0,5000,8) // channel 0, pwm freq 5 khz, Resolution 8 bits Note that we use the channel that is generating the signal, and not the GPIOĪlthough the duty cycle is specified as a percentage, this function receives a value between 0 and another value different from 100.We can pass a value between 0 (wave always with a value of GND) and 1023 (wave always with a value of VCC for 8 bit resolution) for the duty cycle. This function accepts 2 arguments a channel number & an analog value (duty cycle) We’ll use 8-bit resolution, which means you can control the LED brightness using a value from 0 to 255.įinally, to control the LED brightness using PWM, you use the following function: Here we use 8 bits, so the maximum analog value is 255 ( 0 to 255 = 256) Here the first parameter is the channel number (channel 0 is used here)Ģ nd parameter is the PWM Frequency ,here it is 5000 ( 5kHz) which is idle to fade an LEDģ rd parameter is resolution in bits. Next, PWM characteristics are then defined using function There are 16 PWM channels from 0 to 15 in ESP32 & one of them is attached to a GPIO pin. The channel that generates the signal is the ledChannel, that we assign to channel 0. In this example, we’ll get the signal in the led_Pin GPIO, that corresponds to GPIO 2 ( inbuilt LED). GPIO OUTPUT capable pins can only be attached. Inside SETUP first thing is, we need to attach a channel ( channels available from 0 to 15) to a GPIO pin where we want the PWM signal to be generated.Īny GPIO pin can be used, but certain pins which are only INPUTS like GPIO 14,15,16 & 19 CANNOT BE ATTACHED.

GPIO2 pin is assigned to a byte variable led_pin as 2.Ģ more integer variables used to store brightness & fadevalue. In this example let us fade the inbuilt LED onboard. Instead a new Function called ledcWrite() is introduced.Īs the analogWrite() function is not yet available for the ESP32, we will need to go to lower level functions.We will also have more control and flexibility in the PWM functionality, which is good.

In Arduino or ESP8266 Nodemcu we used the analogWrite() function to achieve PWM.īut Analog Write function is not yet implemented in ESP32 Arduino version. In this post we shall see how PWM is implemented on ESP32 & used to fade an LED or control speed of a motor.
