Today I Learned

hashrocket A Hashrocket project

Handy Commands for Raspberry Pi Sensors

I recently got an Enviro hat for my Raspberry Pi Zero W. And in tinkering with it, I found there's a ruby driver for interacting with the BME280 sensor.

You can use it like this -

require "i2c/device/bme280"
require "i2c/driver/i2c-dev"

I2CDevice::Bme280.new(driver: I2CDevice::Driver::I2CDev.new("/dev/i2c-1")).read_raw

In debugging, I learned a few useful commands to check the i2c port. Basically, I just needed to figure out what i2c port to use for the ruby driver.

Check if the i2c port is enabled (raspberry pi os)

sudo raspi-config nonint get_i2c # 0 if enabled, 1 if disabled

List all the i2c interfaces by lsing the dev node.

ls /dev/i2c*

Check if the i2c board interface is in the list of kernel modules

lsmod | grep i2c
See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.