Learning electronics through Elixir and the Nerves IoT framework is so much fun. I enjoy keeping my study notes whenever I learn something particularly about Elixir/Nerves-related things.
Today, I want to play with temperature and humidity sensors.
There are tons of temperature and humidity sensors available out there, and of course they come in different forms and specifications.
Some temperature humidity sensors
- DHT11
- DHT12
- DHT22/AM2302
- AM2302
- AM2322
- LM35DZ
- SI7021/SHT21/HTU21
- AHT20
- SHT31
- BME280
- BME680
- HTU21D
- HDC1080
- more
Types of temperature humidity sensors
Probably specialists would classify sensors in different ways, but to me as a casual electronics hobbyist, I would categorize them by how they look and how to connect them to my Raspberry Pi. For hobby projects, many popular sensors come in as a breakout board while starter kits for beginners often contain DHT11. Some sensors support serial interface such as SPI or I2C; others not. Some allow us to take advantage of Qwiic Connect System or STEMMA QT for easy wiring.
Calibration
Technically, sensors cannot be accurate without calibration on a regular basis. Different sensors can produce different results in the same environment. With that said, sensor readings will be good enough for our hobby projects with the factory setup of the sensor.
Elixir Libraries
Just like for other components, many libraries are written in C, C++ or Python; however Alchemists ported those libraries to Elixir over time. There are many more that need to be ported, which is the place we can contribute in the open source Elixir community. I wrote a few myself.
For DHT11, DHT22, and AM2302
I used the jjcarstens/dht library for my DHT11 sensor. It works like a charm.
iex> gpio_pin = 21
21
iex> DHT.read(gpio_pin, :dht11)
{:ok, %{humidity: 17.0, temperature: 28.0}}
This Adafruit post explains about DHT11, DHT22 and AM2302 Sensors.
For AHT20
My understanding is that AHT20 is the "modern" version of DHT series sensors. The sensor itself is so tiny and many manufactures mount it on a breakout board.
Since I could not find any Elixir library for this sensor, I studied the data sheet and implemented a little library mnishiguchi/AHT20.
The sensor has the I2C interface and its default address is 0x38
.
iex> {:ok, aht20} = AHT20.start_link(bus_name: "i2c-1", bus_address: 0x38)
{:ok, #PID<0.1407.0>}
iex> AHT20.read_data(aht20)
{:ok,
%AHT20.Measurement{
raw_humidity: 158_119,
raw_temperature: 410_343,
relative_humidity: 15.079402923583984,
temperature_c: 28.26671600341797,
temperature_f: 82.88008880615234
}}
Here are some resources:
For Bosche BMP/BME series
These sensors seem to be upscale sensors for toy projects. Unlike other inexpensive sensors, the Bosche sensors have comprehensive data sheets and driver libraries (written in C) themselves. Depending on the model, Bosche BMP/BME series are capable of reading multiple values.
temperature | pressure | humidity | gas | |
---|---|---|---|---|
BMP280 | ✔ | ✔ | ||
BME280 | ✔ | ✔ | ✔ | |
BME680 | ✔ | ✔ | ✔ | ✔ |
There are two Elixir libraries for Bosche BMP/BME series:
- fhunleth/bmp280 - 100% Elixir
- lucaong/elixir_bme680 - Elixir interacting with C code
Here are some resources:
Conclusion
There are many sensors out there. Some popular sensor libraries are ported to Elxiir already. Although some products do not have Elxir library, we can just write one for other alchemists. That is open source community.
I'll add more info as I learn more things.
Top comments (0)