This is an old revision of the document!
It is advised to use timers that periodically execute a function to handle repeating tasks. Hardware timers work parallel to the CPU and consume few CPU resources. ESP32-S3 has 4 hardware timers, but each timer can execute multiple handlers.
The idea of using the timer is to encapsulate a piece of compact code that can be run virtually asynchronously and executed is a precisely-defined time manner. In this scenario, we use a timer to update the LCD screen periodically. We choose a dummy example where Timer 1 is used to increment a value of the byte
type, and Timer 2 reads this value and writes it on the LCD. Naturally, a collision may occur whenever two processes are to access a single memory block (variable). It is critical when both processes (tasks, asynchronous functions) are writing to it. However, in our example, the handler executed by Timer 1 only writes to the variable, while Timer 2 only reads from it. In this scenario, there is no need to use mutexes (semaphores). A scenario with detailed description of when to use mutexes is beyond the scope of this example.
To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first:
A standard LCD handling library is attached to the platformio.ini
, and it is the only library needed. Timers are integrated into the Arduino framework for ESP32.
lib_deps = adafruit/Adafruit LiquidCrystal@^2.0.2
Present on the LCD current value of the byte
variable; update the LCD every 3 seconds using one timer. Use another timer to increase this variable every second.
Check LCD visibility in the camera FOV. You may also disable LED if it interferes with the camera video stream, as described in the scenario EMB9A: Use of RGB LEDs.
We used to separate tasks, but for this case, complete code is provided in chunks, including LCD handling. It presents relations on where particular parts of the code should be located when using timers and how timing relates between components.
Include libraries:
#include <Arduino.h> #include <Adafruit_LiquidCrystal.h>
#define LCD_RS 2 #define LCD_ENABLE 1 #define LCD_D4 39 #define LCD_D5 40 #define LCD_D6 41 #define LCD_D7 42
volatile byte i = 0; hw_timer_t *Timer1 = NULL; hw_timer_t *Timer2 = NULL; const int baseFrequency = 80; MHz const int interval1 = 1000000; Interval in microseconds = 1s const int interval2 = 3000000; Interval in microseconds = 3s static Adafruit_LiquidCrystal lcd(LCD_RS, LCD_ENABLE, LCD_D4, LCD_D5, LCD_D6, LCD_D7); void IRAM_ATTR onTimer1() { i++; } void IRAM_ATTR onTimer2() { lcd.clear(); lcd.setCursor(0,0); lcd.print(i); } void setup(){ Timer1 config
Timer1 = timerBegin(0, baseFrequency, true); timerAttachInterrupt(Timer1, &onTimer1, true); timerAlarmWrite(Timer1, interval1, true); timerAlarmEnable(Timer1); //Timer2 config Timer2 = timerBegin(1, baseFrequency, true); timerAttachInterrupt(Timer2, &onTimer2, true); timerAlarmWrite(Timer2, interval2, true); timerAlarmEnable(Timer2); //start LCD lcd.begin(16,2); lcd.clear(); //start both timers timerAlarmEnable(Timer1); timerAlarmEnable(Timer2);
}
void loop() {
}
…
Describe activities done in Step n.
On the LCD screen, you should see values starting from number 3, then 6, 9, 12 and so on (=3 every 3 seconds). Note, as byte
has a capacity of 256 (0…255), the sequence changes in the following increments once it overflows.
How many timers can I use?: ESP32-S3 has 4 hardware timers. You may trick this limitation by using smart handlers that have, e.g., an internal counter and internally execute every N-th cycle. This helps to simulate more timers in a software way.