Lab 2: Sensors & SPIFFS

In today's lab assignment we will bring up the sensors on the Sparrow ESP32 board and build a logger using the filesystem library and the board's internal Flash.

Bringing up the sensors

We'll learn how to get data from all the sensors on the board.

Light Sensor

The board has an on-board LTR308 light sensor. It's connected to the ESP32 processor via a I2C bus. You will need to install a driver library to get light intensity readings. Go to Sketch > Include Library > Manage Libraries and search for LTR308 and then click on the Install button.

The library comes pre-loaded with an example, you can load it from File > Examples > LTR308 and it should print light intensity reading in the Serial Terminal.

Use the Serial Plotter to graph different illumination levels.

BME680 Sensor

Next, you'll be bringing up the BME680 sensor, which is a highly integrated temperature, pressure, humidity and gas sensor. Again, you will need to install a sensor library that enables you to interface the sensor on I2C and get readings.

Run the I2CDemo to get sensor readings and plot them in the Serial Plotter.

ESP32 Flash Memory

The ESP32 chip from Espressif has an external memory (Flash) with a capacity of 4MB. While its main purpose is to save programs and program data, it can also be accessed by the user. The external memory is connected to the processor via the SPI (Serial Peripheral Interface) bus, and the processor can address a maximum of 16MB of external memory.

To differentiate between program and data sections, the external memory is divided into partitions. The partition table is at offset 0x8000 from the starting address of the external memory, and may look like this:

  • app (type 0) - application partition (programs are stored here)
  • data (type 1) - data partition

The size of each partition can be changed, but applications such as the Arduino IDE have this limit hardcoded. The offset of each partition can be defined in a header, and the compiler arranges the code in such a way that access to the program or data partition can be differentiated.

What's a Filesystem

A file system is similar to a database so that multiple files can be organized hierarchically and data in one file does not corrupt (overwrite) data in another file.

Since the file system consumes additional space on the external memory (eg filename), embedded systems with little space use as simple file systems as possible.


The SPIFFS (SPI Flash File System) file system was started by Peter Andersson to address the peculiarities of Flash memories (NOR Flash), which are particularly affected by repeated writes to the same address.

The main repository is available on GitHub (SPIFFS), but the Arduino IDE already has precompiled ESP32 support.

The file system API includes functions like SPIFFS_open() or SPIFFS_write(), but the Arduino IDE provides a file system abstraction API through which we can call functions like and fs.write(). The “SPIFFS_test” program in the Arduino IDE is one such example.


ESP32 personal agenda

Implement a personal address book that will write and read information on the SPIFFS file system. The input data will be entered by the user through a smartphone that will connect to the ESP32 via Bluetooth.

Starting from the “SPIFFS_test” and “SerialToSerialBT” example in the Arduino IDE, we can use the smartphone to send data and commands via Bluetooth to the ESP32. In the main function “loop()” we want to read the message from the phone once, and then be able to write or read to the external memory.

ESP32 datalogger

Using the same libraries and apps as the previous exercise, implement a sensor data logger. The data logger periodically reads all sensor values (e.g. once every 30 seconds) and writes them in a file, together with a timestamp. For the timestamp, you could use a library such as uptime. Use the bluetooth terminal to dump all sensor data.


iothings/laboratoare/2022/lab2.txt · Last modified: 2022/10/13 17:00 by dan.tudose
CC Attribution-Share Alike 3.0 Unported Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0