On this ESP32 tutorial we will check how to interact with a digital shake sensor, using the ESP32 and the Arduino core. The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.
On this tutorial we will check how to interact with a digital shake sensor, using the ESP32 and the Arduino core.
We will be using this Digital Shake sensor from DFRobot. This is a very simple device that allows to detect a shaking movement in a specific direction, outputting a digital signal  that can be processed with a microcontroller, such as the ESP32.
The device can be powered with a voltage between 3.3 V and 5 V. In our case, since we will be using an ESP32, we will use the 3.3 V level. When the device is powered, an onboard red LED should be turned on.
Note that the sensor also contains a blue onboard LED, which will turn on when shaking it in the correct direction. This helps confirming if the device is working well independently of the software that is running on the microcontroller.
You can read more about the sensor on its wiki.
In this tutorial we will be using interrupts and a binary semaphore to detect the shaking movement and to print a message indicating the detection. By using interrupts, we avoid constantly polling the sensor, leaving the ESP32 CPU free for other useful computation.
So, our Arduino main loop will stay blocked in a semaphore until the sensor is shaken (this event will be signaled by an interrupt). During the time it will be blocked, other tasks can execute useful computation. In our case, we are simply interacting with the sensor, which means there’s nothing else to do. Nonetheless, in a real application scenario, there could be other tasks doing useful computation, which is why we are avoiding polling and using interrupts and semaphores for a more efficient implementation than can be used in other applications.
You can read more about external interrupts on the ESP32 here.
The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.