tiny machine learning LESSON 3



Regarding the safety aspects, since the projects are based on a very low voltage power supply supplied by the USB port of the PC or by backup batteries or power supplies with a maximum of 9V output, there are no particular risks of an electrical nature. It is however necessary to specify that any short circuits caused during the exercise could cause damage to the PC, to the furnishings and in extreme cases even to burns, for this reason every time a circuit is assembled, or modifications are made on it, it will be necessary to do it in power failure and at the end of the exercise it will be necessary to disconnect the circuit by removing both the USB cable for connection to the PC and any batteries from the appropriate compartments or external power connectors. Furthermore, again for safety reasons, it is strongly recommended to carry out the projects on insulating and heat resistant mats that can be purchased in any electronics store or even on specialized websites.

At the end of the exercises it is advisable to wash your hands, as the electronic components could have processing residues that could cause damage if swallowed or if in contact with eyes, mouth, skin, etc. Although the individual projects have been tested and safe, those who decide to follow what is reported in this document assume full responsibility for what could happen in the execution of the exercises provided for in it. For younger children and / or for their first experiences in the field of electronics, it is advisable to perform the exercises with the help and in the presence of an adult.

Roberto Francavilla

The Humidity, Temperature and Pressure sensors of the Nano BLE Sense

With this new lesson we continue the discovery of the hardware equipment of the Arduino Nano 33 BLE Sense.  And in particular we will focus on two sensors: PRESSURE SENSOR LPS22HB and HUMIDITY AND TEMPERATURE SENSOR HTS221.

For all the projects that follow we only need the Nano BLE Sense card, a PC and the USB connection cable.

It is good to highlight once again that the purpose of  the exercises that will follow is to discover in detail the use of the Libraries and therefore the operation of the sensors on the Arduino Nano 33 BLE Sense board.


[technical information extracted and synthesized from the ST Lite datasheet]

This sensor allows the detection of barometric pressure thanks to a piezoresistive element. The output is digital. The sensor also measures the ambient temperature.

Let’s see how it works.

The sensing element that detects absolute pressure consists of a suspended membrane produced using a dedicated process developed by ST. The deformation of this membrane, subjected to the environmental pressure to be detected, modifies the electrical characteristics of the same membrane that you measure, determines the pressure to which it is subjected.

This sensor is able to measure pressures ranging from 260 to 1260 hPa (hPa = Pascal hectogram – 1 hPa = 1 millibar).

Sensor calibration is carried out directly at the factory.

[technical information extracted and synthesized from the ST Lite datasheet]

The HTS221 is a digital sensor for relative humidity and temperature. The sensing element consists of a polymer dielectric planar capacitor structure capable of detecting changes in relative humidity.

In fact, the humidity present in the air modifies the electrical characteristics of the dielectric present in the capacitor and therefore by measuring these variations it is possible to measure the amount of humidity present in the air. The same applies to temperature.

Some characteristic data of the sensor:

–         Relative humidity range from 0 to 100%

–         Humidity accuracy: ± 3.5%

–         Temperature range from -40°C to 120°C

–         Temperature accuracy: ± 0.5°C in the range from 15°C to 40°C

PAI-004: Measure Humidity and Temperature with HTS221

The sketch to be used for this project is the one shown below, it was obtained directly from the sample sketch in the library (apart from some small changes made by me):

Once the sketch has been loaded, as described above and the port to which the board is connected has been changed, open the Serial Monitor and the readings made will be shown… also try to breathe above to change the humidity and even in a small part the temperature …

Analysis of the sketch of project AI-004 - Measure Humidity and Temperature with HTS221

For this sensor you need to use the Arduino_HTS221.h Library, and you need to call it with the classic syntax:

#include <Arduino_HTS221.h>

As you can see from the sketch, the readings from the sensor are made using two simple functions:

float temperature = HTS.readTemperature();

float humidity = HTS.readHumidity();

The value returned by the function is float.

PAI-005: Measurement of Barometric Pressure and Temperature with LPS22HB

Also for this project we will use, modifying it appropriately, the sketch made available by the example of the library.

The sketch to use for this project is:

Once the sketch is loaded and the port to which the Nano BLE Sense is connected has been changed, open the Serial Monitor and you will be shown the pressure and temperature readings.

The pressure is returned by the sensor directly in kPa (in kilo pascal), to do the conversion the millibars just multiply by 10 the value returned by the reading and in this way you get the hPa (hecto pascal) that we know to be equal to 1 millibar:

Sketch Analysis of Project AI-005 - Pressure and Temperature with LPS22HB

As you can see from the sketch, also in this case the use of the <Arduino_LPS22HB.h> Library  greatly simplifies the sketch.  In fact, once the library is called up with “include”, just use the functions:

float pressure = BARO.readPressure();

float temperature = BARO.readTemperature();

to receive the values of the barometric pressure and temperature readings.  Also in this case the values are returned in float.

PAI-006: Simple Weather Station

By properly modifying both the previous sketches and using the functions that make available to us the two libraries of the two sensors HTS221 and LPS22HB, it is possible to create with our Nano BLE Sense a small and simple meteorological station able to give us those values of pressure, temperature and relative humidity of our environment.

The modified sketch is as follows:

Loaded the sketch and changed the communication port between the PC and the board, passing from the writing to the reading one, you can open the serial monitor that shows us the read values:

Analysis of the sketch of the PROJECT AI-006 – Simple Weather Station

This project is nothing more than the union of the functions found in the two previous projects. As it is possible to observe , the temperature is detected by the two distinct sensors and the result is practically identical to less than a few tenth of a degree.

Also in this case I calculate the hPA to obtain the measurement of the pressure in millibars.

If you found the lesson interesting, make a donation you will help me realize many others.