ESP32 Ticker: Light Sensor readings

Introduction

In this tutorial we will check how to periodically obtain measurements from an ambient light sensor, using the ESP32 and the Ticker library.

I’ll be using this sensor module from DFRobot. For an introductory tutorial and an electric diagram on how to connect it to the ESP32, please check this previous post. The basics of the Ticker library were also already covered on this post.

The tests shown below were performed on a ESP32-E FireBeetle board, also from DFRobot.

The code

The first thing we will do is including the Ticker.h library. This include will expose to us the Ticker class, which will allow us to setup a callback function to be executed periodically.

#include <Ticker.h>

Then we will define a variable to hold the number of the ESP32 analog pin connected to the sensor. I’ll be using pin 36 of my board (like shown on this previous post electric diagram). Naturally, if you have the sensor connected to another pin of your ESP32, you should define this variable accordinly.

const int sensorPin = 36;

We will also instantiate an object of the previously mentioned Ticker class.

Ticker measurementTicker;

Next we will take care of writing the code of our Arduino setup function. We will start by opening a serial connection, so we can output the obtained sensor measurements and later check them using the Arduino IDE serial monitor.

Serial.begin(115200);

Then we will configure our Ticker object to run a callback function periodically. To do so, we simply need to call the attach_ms method on the Ticker object, passing as first input the interval (in milliseconds) between invocations of the function and as second input the callback function.

For illustration purposes, we will set the function to run every 5 seconds (5000 milliseconds). Our function will be called getMeasurement and we will check its implementation below.

measurementTicker.attach_ms(5000, getMeasurement);

The whole Arduino setup function is shown in the snippet below.

void setup(){
  Serial.begin(115200);
   
  measurementTicker.attach_ms(5000, getMeasurement);
}

Now we will take care of defining our getMeasurement function. It will return void and receive no arguments. Its implementation will simply consist on obtaining an analog measurement from the sensor and then directly print it to the serial port.

void getMeasurement(){
  int analogVal = analogRead(sensorPin);    

  Serial.println(analogVal);
}

The complete code is shown below.

#include <Ticker.h>

const int sensorPin = 36;

Ticker measurementTicker;
 
void getMeasurement(){
  int analogVal = analogRead(sensorPin);    

  Serial.println(analogVal);
}
 
 
void setup(){
  Serial.begin(115200);
   
  measurementTicker.attach_ms(5000, getMeasurement);
}
 
void loop(){}

Testing the code

Before uploading the code to the ESP32, make sure all the wirings between the device and the sensor are correctly done and the system is powered. Then, in case you are not using the same ESP32 pin as I am, change the value of the global variable in the code to reflect your configuration.

After all of the previous steps are done, simply compile and upload the code to your ESP32, using the Arduino IDE. When the procedure finishes, open the IDE serial monitor.

You should start getting measurements from the sensor, with a periodicity of 5 seconds, like illustrated below in figure 1. You can vary the amount of light getting to the sensor to see corresponding variations in the obtained measurements (no light should get the measurements close to zero and having a light pointing at the sensor should get the measurements close to 4095).

Periodic measurements from the ambient light sensor, using the Ticker library.
Figure 1 – Periodic measurements from the ambient light sensor.

Additional notes

In this post we covered a very simplistic use case, using a specific ambient light sensor. Nonetheless, the approach followed here can be used generically for different sensors or even actuators that need to be fired / stopped periodically.

Naturally, different sensors / actuators may need more complex code to interact with, but the objective of this post was just to give a brief overview of a possible program architecture.

We also assumed, for simplicity, that the process of obtaining and processing the measurement (in our case, it was simply printing it to the console) was acceptable to be done entirely in the callback function triggered by the Ticker library. We also assumed a reasonably big interval between measurements (5 seconds).

Nonetheless, it is very important to take in consideration that these callbacks should not do too much work and should not have blocking calls. This means that if, for example, you intend to send the measurements to a remote server via HTTP, you should most likely defer that part to another FreeRTOS task.

Some important considerations about the Ticker library were already mentioned in this section of the introductory tutorial, and I recommend you to check and consider them for your application, specially if you are working with real time constraints.

Suggested ESP32 Readings

Leave a Reply