[Elixir Nerves] Temperature Humidity Sensors
![[Elixir Nerves] Temperature Humidity Sensors](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1612785846563%2FW2Bd70TX1.jpeg&w=3840&q=75)
Search for a command to run...
![[Elixir Nerves] Temperature Humidity Sensors](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1612785846563%2FW2Bd70TX1.jpeg&w=3840&q=75)
I wrote a script called phx-docker-compose-new that can be used instead of mix phx.new to generate a new Phoenix application. Building the development environment for a Phoenix application using Docker Compose is convenient because you can set up not...
この記事について Raspberry Pi、TensorFlow、Pythonのいずれにも詳しくない筆者が、物体検出をやって楽しんだ成果の記録です。 TensorFlow公式の物体検出のサンプルプログラムを実行します。 動作環境 ボード Raspberry Pi 4 Model B OS Raspberry Pi OS (32-bit または 64-bit) デスクトップ環境 カメラ Raspberry Pi カメラモジュール v2 Python Python ...
One day I was trying to upgrade one of my Phoenix 1.5 apps to Phoenix 1.6. In that app, I customize Bootstrap styles using SCSS. I want to talk about what I found and stumbled on when setting up SCSS. TIL All explained in Phoenix 1.6 Asset Managemen...
This is written in Japanese. I might convert it to English later, maybe. はじめに Elixirのテストでモックを用意するときに利用するElixirパッケージとして、moxが人気です。Elixir作者のJosé Valimさんが作ったからということもありますが、ただモックを用意するだけではなくElixirアプリの構成をより良くするためのアイデアにまで言及されているので、教科書のようなものと思っています。 一言でいうと「その場...
TIL Access supports keyword lists (Keyword) and maps (Map) out of the box, but not structs. There is a reason why Elixir does not implement Access behaviour for structs. Perfer data.key for accessing predefined atom keys of map or struct. Access be...
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.
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.
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.
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.
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.
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:
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) 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:
Here are some resources:
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.
