Skip to content

Instantly share code, notes, and snippets.

@BjoernSchilberg
Last active December 15, 2021 22:01
Show Gist options
  • Save BjoernSchilberg/9c3e3e284ecefba49e9a7179714d09f2 to your computer and use it in GitHub Desktop.
Save BjoernSchilberg/9c3e3e284ecefba49e9a7179714d09f2 to your computer and use it in GitHub Desktop.
Tigard

Tigard

Links

BME280 with Circuitpython (I2C on JTAG Header)

python3 -m venv venv
source /tmp/venv/bin/activate
pip3 install Adafruit_Blinka
pip3 install adafruit-circuitpython-bme280
export BLINKA_FT2232H=1

find i2c address

Option1

i2cscan.py ftdi:///2

Output:

   0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
0: .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
1: .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
2: .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
3: .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
4: .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
5: .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
6: .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
7: .  .  .  .  .  .  W  .  .

=> Address is 0x76

Option2

"""CircuitPython I2C Device Address Scan"""
# If you run this and it seems to hang, try manually unlocking
# your I2C bus from the REPL with
#  >>> import board
#  >>> board.I2C().unlock()

import time
import board
import busio

# To use default I2C bus (most boards)
#i2c = board.I2C()

# To create I2C bus on specific pins
i2c = busio.I2C(board.SCL1, board.SDA1)  # QT Py RP2040 STEMMA connector

while not i2c.try_lock():
    pass

try:
    while True:
        print(
            "I2C addresses found:",
            [hex(device_address) for device_address in i2c.scan()],
        )
        time.sleep(2)

finally:  # unlock the i2c bus when ctrl-c'ing out of the loop
    i2c.unlock()

Read sensor values

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio
from adafruit_bme280 import basic as adafruit_bme280

# Create sensor object, using the board's default I2C bus.
#i2c = board.I2C()  # uses board.SCL and board.SDA

# To create I2C bus on specific pins
i2c = busio.I2C(board.SCL1, board.SDA1)

bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c,address=0x76)

# OR create sensor object, using the board's default SPI bus.
# spi = board.SPI()
# bme_cs = digitalio.DigitalInOut(board.D10)
# bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, bme_cs)

# change this to match the location's pressure (hPa) at sea level
# https://www.wetter.de/deutschland/wetter-osnabrueck-18220336.html
bme280.sea_level_pressure = 1029.00

while True:
    print("\nTemperature: %0.1f C" % bme280.temperature)
    print("Humidity: %0.1f %%" % bme280.relative_humidity)
    print("Pressure: %0.1f hPa" % bme280.pressure)
    print("Altitude = %0.2f meters" % bme280.altitude)
    time.sleep(2)

Output:

Temperature: 20.5 C
Humidity: 53.1 %
Pressure: 1021.2 hPa
Altitude = 64.37 meters

Melexis MLX90614 Contact-less Infrared Temperature sensor

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .env
source .env/bin/activate
pip3 install adafruit-circuitpython-mlx90614
export BLINKA_FT2232H=1
python3 find.py

Output:

I2C addresses found: ['0x5a']
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

#  Designed specifically to work with the MLX90614 sensors in the
#  adafruit shop
#  ----> https://www.adafruit.com/product/1747
#  ----> https://www.adafruit.com/product/1748
#
#  These sensors use I2C to communicate, 2 pins are required to
#  interface Adafruit invests time and resources providing this open
#  source code,
#  please support Adafruit and open-source hardware by purchasing
#  products from Adafruit!

import board
import busio
import adafruit_mlx90614

# The MLX90614 only works at the default I2C bus speed of 100kHz.
# A higher speed, such as 400kHz, will not work.
#i2c = board.I2C()
i2c = busio.I2C(board.SCL1, board.SDA1)
mlx = adafruit_mlx90614.MLX90614(i2c,address=0x00)

# temperature results in celsius
while True:
    print("Ambent Temp: ", mlx.ambient_temperature)
    print("Object Temp: ", mlx.object_temperature)
    time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment