Elektor_Mag_-_January-February_2021

([email protected]) #1
lektor January & February 2021 11

a degree Celsius. This means a temperature of 21.35°C is represented
as 2135. The sensor is initialised so that, internally, it performs a moving
average of two consecutive frames, thus yielding a significant amount
of noise reduction. The registers are read out in a single pass that
guarantees all the values belong to the same frame. The calculations for
the pixel temperature and the on-chip thermistor temperature (which
we do not use here) differ (see the datasheet [3] for more details). For
improved accuracy in the calculation, intermediate values are repre-
sented in units of 1/10,000 of a degree, with a subsequent division
by 100 to produce final results represented in hundredths of a degree.

Calculating the colour value
The colour display is implemented using the HSL colour model [10]
(Figure 7) and the M5StickC bundle includes a suitable TFT driver
library. Of the three parameters hue (H), saturation (S) and luminance (L)
we only vary the H parameter, or angle, in the colour wheel. The S

configured for each pixel. Thus the chip can autonomously monitor a
particular point in its field of view and only wake up the ESP32 when
its temperature exceeds a threshold, thereby saving power. A similar
monitoring feature could also be implemented in the smartphone app,
taking into account all the pixels in the image.


A matter of software
To create the firmware it is possible to use either Espressif ’s proprietary
development environment or the Arduino ecosystem. The latter is very
easy to use as there is a highly-effective global community support
network. The original Arduino IDE is a good way to get started, but
it is not really up to the task of building more advanced applications.
Instead, the author recommends the free Visual Studio Code editor
from Microsoft that supports Arduino via an extension [6]. M5Stick-C
(esp32) can be found under the Board Manager.


The firmware for MTheCam was developed by the author using C++.
Besides the Arduino source code MTheCam_LT.ino and a couple of
.h and .cpp files (Mxxx.cpp/h) we also use the highly-recommended
ArduinoJson library for JSON handling (version 6 of this library is
required; version 5 will not work). The hardware of the platform are
comfortably supported by the M5StickC library [8]. This must be
installed using the library manager (F1 – Arduino: Library Manager).


While the ArduinoJson library documentation is a pleasure to read, this
cannot be said of the descriptions of the M5StickC library. To use it
successfully requires the source code to be tediously and painstakingly
examined and is rather too much like work and not enough like play!


We will now look at various snippets of code covering the various
functions in their logical sequence.


Sensor read
Readings are made available in byte-wide registers in the sensor ten
times per second. The software continuously reads these registers
over the I^2 C bus using the Wire library. The pixel data has a resolution
of 12 bits and so two bytes are used for each. Thus the code to read
the registers in MTC_readReg() appears as follows.


#define BUF_LENRX 128
int reg = 0x80;
byte _rxBuf[BUF_LENRX];


...
Wire.beginTransmission(devAddr); // chip address: see
datasheet
Wire.write(reg); // 0x80 -> read 128bytes of 64
pixels @ 12bits
Wire.endTransmission();
if (Wire.readTransmission(devAddr, rxBuf, BUF_
LENRX) == I2C_ERROR_OK) { success = true; }
Wire.endTransmission();


Under some circumstances, Wire.readTransmission() can return
an error code that can come in useful when tracking down bugs in
the read process.


The code excerpt shown in Listing 1 is responsible for reading the
sensor and calculating temperature values. With the help of a little
bit-twiddling, two entries in the rxBuf[] array are converted into a
temperature represented as an integer in units of one-hundredth of


Figure 5: The M5StickC as a bright orange smartwatch. (Source: m5stack.
com)

Figure 6: Right-angled or straight — there are two ways the sensor can be
mounted on the M5StickC.
Free download pdf