But the most important function will be the one that allows us to specify the duty cycle value. We will write the code to control the duty cycle values of the signal in our Arduino main loop. The full setup function is shown bellow, along with the constants definition. To attach the pin to the PWM channel, we call the ledcAttachPin function, passing as arguments the number of the GPIO pin and the PWM channel previously defined. As we’ve seen in this previous tutorial, we can use the LED_BUILTIN constant to get the number of the pin connected to the LED on our FireBeetle ESP32 board. In our case, we want it to be generated on the pin that is connected to the on board LED. So, we will need to attach channel 0 (which was the one we defined) to the digital pin where we want the PWM signal to be generated. Note however that the channel is not the pin where we will control the LED. To do so, we call the ledcSetup function, which receives as input the 3 previously mentioned parameters, in the same order. First, we need to setup the channel, frequency and resolution we specified. Now, in the setup function, we will configure the LED PWM functionalities. We will use channel 0 and a resolution of 8 bits. Also from the header file, we can see that we can choose a channel from 0 to 15 and a resolution between 1 and 16 bits, which is coherent with the values mentioned on the introductory section. We are algo going to specify the LED PWM channel and the resolution of the PWM duty cycle, in bits. Note however that the maximum values for the frequency are not yet very clear and in the header file of the functionality we are going to use is stated that the maximum frequency depends on the resolution chosen. The first one will be the frequency of the PWM signal generated to control the LED. In the first portion of the code, we will specify some global configuration constants. The accuracy of the duty cycle can be configured until 16 bits of resolution. In terms of hardware, the LED PWM of the ESP32 is composed of 16 independent channels, with configurable duty cycles and wave periods. Nevertheless, we will also have more control and flexibility in the PWM functionality, which is good. Thus, we will need to go to lower level functions in this tutorial. Note that at the time of writing, the commonly used analogWrite Arduino function was not yet available for the ESP32 Arduino environment support. Since the board as a built in LED, no external hardware will be needed. I will be using DFRobot’s FireBeetle ESP32 board to perform the tests. The objective of this post is to explain how to fade a LED with the ESP32, using the LED PWM functionalities of the microcontroller. The objective of this post is to explain how to fade a LED with the ESP32, using the LED PWM functionalities.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |