CircuitPython I2C Sensor

We have drivers for many popular I2C sensors in our driver bundle (and more being written all the time!)

I2C is a 2-wire protocol for communicating with simple sensors and devices and its really easy to use with CircuitPython

Remember that the Gemma & Trinket M0 does not have the required i2c pull-up resistors on SDA or SCL! You must have those on the sensor board (all of ours do) or add them yourself. 10K ohm pullups to 3.3V work well, you cannot use the 'internal' pullups.

Lets try wiring up to a nice Si7021 temperature & humidity sensor:

# I2C sensor demo

import board
import busio
import adafruit_si7021
import time

i2c = busio.I2C(board.SCL, board.SDA)

# lock the I2C device before we try to scan
while not i2c.try_lock():
    pass
print("I2C addresses found:", [hex(i) for i in i2c.scan()])

# unlock I2C now that we're done scanning.
i2c.unlock()

# Create library object on our I2C port
si7021 = adafruit_si7021.SI7021(i2c)


# Use library to read the data!
while True:
    print("Temp: %0.2F *C   Humidity: %0.1F %%" % (si7021.temperature, si7021.relative_humidity))
    time.sleep(1)

We used our Alligator-to-breadboard wires to connect up the Gemma to a Si7021 breakout

  • Vin connects to 3.3V
  • GND connects to GND
  • SDA connects to D0
  • SCL connects to D2

With a Trinket M0, a small breadboard fits both pieces, just wire it up so

  • Vin connects to 3.3V
  • GND connects to GND
  • SDA connects to D0
  • SCL connects to D2

Adafruit Si7021 Temperature & Humidity Sensor Breakout Board

PRODUCT ID: 3251
It's summer and you're sweating and your hair's all frizzy and all you really want to know is why the weatherman said this morning that today's relative humidity would max...
$6.95
IN STOCK

Small Alligator Clip to Male Jumper Wire Bundle - 12 Pieces

PRODUCT ID: 3255
For bread-boarding with unusual non-header-friendly surfaces, these cables will be your best friends! No longer will you have long strands of alligator clips that are grabbing little...
$7.95
IN STOCK

Then check the REPL. If you have not yet used this chip you may get an ImportError: no module named 'adafruit_si7021'

That means you need to install the Adafruit_Si7021 library that gives you the friendly interface we use above.

Check out our page on Installing Libraries to learn how to download the driver bundle and drag the driver you need to the lib folder

You will also need the adafruit_bus_device library folder - that will give you I2C access in a nice manner!

Once you're done you'll see you have the libraries installed:

Finally if you re-run you will be able to see the temperature and humidity data from the sensor:

Last updated on 2017-12-01 at 04.59.09 PM Published on 2017-08-23 at 05.57.02 PM