Skip to main content

Another library to handle sensors connected via I2c bus (SDA, SCL pins) to the Raspberry Pi.

This library implements the following i2c sensors: - Bosch BME280 Environmental sensor (temperature, humidity and pressure) - HTU21D temperature and humidity sensor - BH1750FVI light level sensor

Installation

This library needs the i2c bus to be enabled, and uses the smbus-cffi module to communicate with it, so, before installing with pip, make sure the bus is enabled, and the necessary dependencies are available:

Directions for installing smbus support on Raspberry Pi:

Enable I2c interface with the Raspberry Pi config utility:

# Enable i2c interface
sudo raspi-config

Select Interfacing options->I2C choose <Yes> and hit Enter, then go to Finish and you’ll be prompted to reboot.

Install dependencies for use the smbus-cffi module and reboot:

sudo apt-get install build-essential libi2c-dev i2c-tools python-dev libffi-dev
sudo reboot

Check the i2c address of the sensors

After installing i2c-tools, a new utility is available to scan the addresses of the connected sensors:

/usr/sbin/i2cdetect -y 1

It will output a table like this:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- 23 -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- 77

So you can see three sensors are present at 0x23 (BH1750), 0x40 (HTU21D) and 0x77 (BME280) addresses.

Install with pip

Finally, in your python environment,

pip install i2csense

Usage

This library is intended to be used by other applications, where sensors are instantiated with their configuration parameters, to be read at the desired intervals. However, if you want to use as a simple logger via command line, a simple CLI is also available to test the sensors.

import smbus
from i2csense.bme280 import BME280

bus = smbus.Bus(1)
sensor = BME280(bus)
delta_secs = 5

while True:
    sensor.update()
    if not sensor.sample_ok:
        print("An error has occured")
        break
    print(sensor.current_state_str)
    sleep(delta_secs)

CLI usage

Find sensors:

i2csense
# or specify the i2c bus:
i2csense -b 0

Test sensors:

# Test BME280 sensor with default params:
i2csense -s bme280

# Test BME280 sensor with custom params every 10 secs:
i2csense -d 10 --bus 0 --address 0x77 --sensor bme280 --params osrs_t=4 osrs_p=4 osrs_h=4 mode=2 filter_mode=1

Changelog

  • v0.0.1: First release with 3 sensors: BME280, BH1750, HTU21D.

  • v0.0.2: Minor fixes.

  • v0.0.3: Minor fixes for BH1750, fix README.rst.

TODO:

  • Append more sensors.

  • finish CLI interface with better help and more configuration options.

Although the library only covers three sensors, it would be ideal to continue completing it with more sensors or actuators running in the i2c bus, so I encourage you to contribute with more sensors, or to copy, change, edit, or suggest any changes.

Metadata

Release files for i2csense 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for i2csense 0.0.4
File Size Uploaded
i2csense-0.0.4.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for i2csense 0.0.4
File Interpreter ABI Platform
i2csense-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 26.0 kB

Release files / i2csense-0.0.4.tar.gz

Download URL i2csense-0.0.4.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6f9c0a37d971e5b8a60c54982bd580cff84bf94fedc08c097e603a8e5609c33f
BLAKE2b-256 checksum
How to use checksums
1aa33d5c3466836eb6833b79316654731191b029958fb40ab97e7370a2e9ebdf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / i2csense-0.0.4-py3-none-any.whl

Download URL i2csense-0.0.4-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
19b92e6334ffaf91a86e9ae75ca193a6617da07adb9aaf2f32ccf68b01330980
BLAKE2b-256 checksum
How to use checksums
f608c7a81dc1c773a5b7ef8ddaa5585f8166ed373c6336199ea1e6a1547d1ff0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page