This example demonstrates how to read temperature data from the BMP280 temperature sensor board. We will use I2C protocol to connect a sensor board to our Nerves target device (e.g., Raspbery Pi).
The explanation below assumes a Raspberry Pi Zero W as a target device but other Nerves targets should work as well.
The Bosch BMP280 sensor itself is so tiny that it might be difficult to handle but some electronics components manufactureres mount it on a breakout board for our convenience, which is what we look for. Do Google search by "BMP280 breakout board" and you will find something like Adafruit BMP280 Sensor board.
You can alternatively use BME280 or BME680 since the elixir-sensors/bmp280 library supports them.
Roughly speaking, there are typically two ways to hook up a sensor to our Nerves target.
If you already have a soldering iron and enjoy using it, this is the go-to option. Even if you have not already, soldering can be fun. This option only requires a pin header and 4 jumper wires
I2C protocol uses 4 pins. It is confusing but different products may name the pins differently.
Target (I2C controller) | Sensor (I2C peripheral) | Description |
---|---|---|
3.3V | Vin | power |
GND | GND | common ground for power and logic |
I2C SDA (GPIO 2) | SDA | I2C data |
I2C SCL (GPIO 3) | SCL | I2C clock |
Taking advantage of Qwiic Connect System or similar system, we can remove the need for the soldering. One tradeoff is that we need to get an extra board like Qwiic HAT for Raspberry Pi and special wires.
Please see the main Nerves installation docs if you haven't used Nerves before.
cd hello_bmp280
Specify the Nerves target hardware.
export MIX_TARGET=rpi0
Specify the WiFi name and password in order to hard-code WiFi credentials in the firmware.
export WIFI_SSID=your_wifi_name
export WIFI_PSK=your_wifi_password
If you do not have WiFi available, you can alternatively connect the target board to your host machine (e.g., your laptop) using a USB cable.
Install dependencies
mix deps.get
build firmware
mix firmware
burn firmware to an SD card
mix firmware.burn
Insert the SD card into your target board and power up
Wait for the target board to finish booting
SSH into the target board
ssh nerves.local
Once you shell into your target board, you can play around with the elixir-sensors/bmp280 library.
- Official docs: https://hexdocs.pm/nerves/getting-started.html
- Official website: https://nerves-project.org/
- Forum: https://elixirforum.com/c/nerves-forum
- Discussion Slack elixir-lang #nerves (Invite)
- Source: https://github.com/nerves-project/nerves