Moving on to the setup function, we will start by opening a serial connection, so we can later output the measurements obtained from the DHT22. To finalize the global variable declarations, we will need a semaphore, which will be used to synchronize the main loop and the Interrupt Service Routine. This will be used later to configure the timer. In order to be able to configure the timer, we will need to declare a pointer to a variable of type hw_timer_t. We will do it by calling some methods of this object, as we will see below. Then, we will need an object of class DHTesp, which we will use to get the measurements from the sensor. The first thing we are going to do is including the DHTesp.h library, so we can interact with the DHT22 using a high level API rather than having to worry about the lower level details of the communication protocol between the ESP32 and this sensor. The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board. To facilitate the interaction with the mentioned sensor, I’m using a DFRobot DHT22 module which already has all the electronics needed and exposes a wiring terminal that facilitates the connections to the ESP32. Nonetheless, for simplicity and to focus on the synchronization, we will only fetch temperature measurements. On a final introductory note, please take in consideration that the DHT22 allows to both get temperature and humidity measurements. Nonetheless, in a real application use case where multiple tasks may be executing concurrently, freeing the CPU from a task when it is not doing anything is of extreme importance. Naturally, this blocking architecture doesn’t add much to our simple example where our application doesn’t do anything else than interacting with the DHT22. We could have used a dedicated FreeRTOS task but since for this example we will only fetch and print measurements, we will keep the code simpler by using the main loop. So, we will use the semaphores to keep the main loop “blocked” while it is waiting for the next interrupt, leaving the CPU available for the scheduler to execute other tasks. So, between measurements, it doesn’t make sense to leave the main loop active, wasting precious CPU time. We will use an interval of 10 seconds between each measurement, which is a lot of time when we think about machine instructions and clock frequencies. So, the interrupts will only be responsible for signaling the Arduino main loop when its time to get a new measurement. Note however that interrupt handling functions should run as fast as possible, so we should not communicate with the DHT22 inside those functions. Nonetheless, instead of relying on polling or Arduino delays, we will use the timer interrupts to implement the periodicity of the measurements. In terms of implementation, our code will periodically read measurements from the DHT22 sensor. As shown in that tutorial, we will take advantage of the FreeRTOS semaphores to achieve such synchronization. So later, in the coding section, we are going to be configuring the timer interrupt by setting the counter value at which the interrupt should be triggered.įor a tutorial that introduces the synchronization between a task and an Interrupt service Routine, please check here. One important thing to remember about the ESP32 timers is that they are implemented with 64 bit counters and 16 bit prescalers. It explains in detail the timer concepts, which will be important to understand the code below. Please check this tutorial which explains how to install it and also how to wire the ESP32 to the DHT22.įor an introduction on the ESP32 timers, please check this previous post. In order to interact with the DHT22 from the ESP32, we will need an auxiliary library. In this tutorial we will check how to obtain temperature measurements from a DHT22 sensor using the ESP32, the Arduino core and timer interrupts.
0 Comments
Leave a Reply. |