# [Elixir Nerves] Temperature Humidity Sensors

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](https://www.google.com/search?q=dht11)
- [DHT12](https://www.google.com/search?q=dht12)
- [DHT22/AM2302](https://www.google.com/search?q=dht22)
- [AM2302](https://www.google.com/search?q=AM2302)
- [AM2322](https://www.google.com/search?q=AM2322)
- [LM35DZ](https://www.google.com/search?q=LM35DZ)
- [SI7021/SHT21/HTU21](https://www.google.com/search?q=SI7021)
- [AHT20](https://www.google.com/search?q=AHT20)
- [SHT31](https://www.google.com/search?q=SHT31)
- [BME280](https://www.google.com/search?q=BME280)
- [BME680](https://www.google.com/search?q=BME680)
- [HTU21D](https://www.google.com/search?q=HTU21D)
- [HDC1080](https://www.google.com/search?q=HDC1080)
- more

## Types of temperature humidity sensors

Probably specialists would [classify sensors in different ways](https://www.digikey.com/en/blog/types-of-temperature-sensors), 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](https://www.adafruit.com/product/386). Some sensors support serial interface such as SPI or I2C; others not. Some allow us to take advantage of [Qwiic Connect System](https://www.sparkfun.com/qwiic) or [STEMMA QT](https://learn.adafruit.com/introducing-adafruit-stemma-qt) for easy wiring.

[![](https://cdn-shop.adafruit.com/970x728/386-00.jpg)](https://www.adafruit.com/product/386)

[![](https://cdn-shop.adafruit.com/970x728/2652-05.jpg)](https://www.adafruit.com/product/2652)

## Calibration

Technically, sensors cannot be accurate without [calibration](https://en.wikipedia.org/wiki/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](https://github.com/jjcarstens/dht) library for my DHT11 sensor. It works like a charm.

```elixir
iex> gpio_pin = 21
21

iex> DHT.read(gpio_pin, :dht11)
{:ok, %{humidity: 17.0, temperature: 28.0}}
```

[This Adafruit post](https://learn.adafruit.com/dht) 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](https://github.com/mnishiguchi/AHT20).

The sensor has the I2C interface and its default address is `0x38`.

```elixir
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:

- [AHT20 data sheet](https://cdn.sparkfun.com/assets/d/2/b/e/d/AHT20.pdf).

### For Bosch BMP/BME series

These sensors seem to be upscale sensors for toy projects. Unlike other inexpensive sensors, the Bosch sensors have comprehensive data sheets and [driver libraries (written in C)](https://github.com/BoschSensortec) themselves. Depending on the model, Bosch 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](https://github.com/fhunleth/bmp280) - 100% Elixir
- [lucaong/elixir_bme680](https://github.com/lucaong/elixir_bme680) - Elixir interacting with C code

Here are some resources:

- [BMP280 data sheet](https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp280-ds001.pdf)
- [BME280 data sheet](https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf)
- [BME680 data sheet](https://cdn-shop.adafruit.com/product-files/3660/BME680.pdf)
- [Adafruit BME680 pinouts](https://learn.adafruit.com/adafruit-bme680-humidity-temperature-barometic-pressure-voc-gas/pinouts)

## Conclusion

There are many sensors out there. Some popular sensor libraries are ported to Elixir already. Although some products do not have Elixir library, we can just write one for other alchemists. That is open source community.

I'll add more info as I learn more things.

![](https://user-images.githubusercontent.com/7563926/105795801-74bae480-5f5b-11eb-8d45-0ffd14db26bb.jpg)

