The Feather Sense is full of sensors including proximity, color, temperature, pressure, altitude, magnetometer, accelerometer, gyro, humidity and sound. CircuitPython makes it super simple to get data from these sensors to use however you like. This CircuitPython demo sets up each of the sensors and prints the data from them to the serial console.

Remember, you'll need to have CircuitPython installed and the appropriate libraries loaded before continuing.

Feather Sense Sensor Demo

Save the following example as code.py on your CIRCUITPY drive.

"""Sensor demo for Adafruit Feather Sense. Prints data from each of the sensors."""
import time
import array
import math
import board
import audiobusio
import adafruit_apds9960.apds9960
import adafruit_bmp280
import adafruit_lis3mdl
import adafruit_lsm6ds.lsm6ds33
import adafruit_sht31d

i2c = board.I2C()

apds9960 = adafruit_apds9960.apds9960.APDS9960(i2c)
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
lis3mdl = adafruit_lis3mdl.LIS3MDL(i2c)
lsm6ds33 = adafruit_lsm6ds.lsm6ds33.LSM6DS33(i2c)
sht31d = adafruit_sht31d.SHT31D(i2c)
microphone = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
                              sample_rate=16000, bit_depth=16)

def normalized_rms(values):
    minbuf = int(sum(values) / len(values))
    return int(math.sqrt(sum(float(sample - minbuf) *
                             (sample - minbuf) for sample in values) / len(values)))

apds9960.enable_proximity = True
apds9960.enable_color = True

# Set this to sea level pressure in hectoPascals at your location for accurate altitude reading.
bmp280.sea_level_pressure = 1013.25

while True:
    samples = array.array('H', [0] * 160)
    microphone.record(samples, len(samples))

    print("\nFeather Sense Sensor Demo")
    print("---------------------------------------------")
    print("Proximity:", apds9960.proximity)
    print("Red: {}, Green: {}, Blue: {}, Clear: {}".format(*apds9960.color_data))
    print("Temperature: {:.1f} C".format(bmp280.temperature))
    print("Barometric pressure:", bmp280.pressure)
    print("Altitude: {:.1f} m".format(bmp280.altitude))
    print("Magnetic: {:.3f} {:.3f} {:.3f} uTesla".format(*lis3mdl.magnetic))
    print("Acceleration: {:.2f} {:.2f} {:.2f} m/s^2".format(*lsm6ds33.acceleration))
    print("Gyro: {:.2f} {:.2f} {:.2f} dps".format(*lsm6ds33.gyro))
    print("Humidity: {:.1f} %".format(sht31d.relative_humidity))
    print("Sound level:", normalized_rms(samples))
    time.sleep(0.3)

Set the sea_level_pressure to match the sea level pressure at your location in hectoPascals for the most accurate altitude reading. Update the following line:

Download: file
bmp280.sea_level_pressure = 1013.25

Let's take a quick look at the code.

First the code imports all the libraries needed, creates the I2C object, and instantiates all of the sensors.

Next is a helper function for getting sound level.

Then the code enables proximity and color sensing on the APDS9960.

Next you set the sea level pressure to match your location if you want a more accurate altitude reading.

Inside the loop, we first gather raw data from the sound sensor.

Then, we print a list of all of the sensor data to the serial console, formatted for appropriate precision and followed by appropriate units where applicable.

Finally, we have a time.sleep() so the serial console is readable - without the sleep it moves too quickly to read!

That's all there is to reading sensor data on the Feather Sense using CircuitPython! You can easily adapt this example to print only the sensor data you're interested in, or use the sensor data for other purposes. Give it a try!

This guide was first published on Mar 11, 2020. It was last updated on Mar 11, 2020.

This page (CircuitPython Sense Demo) was last updated on Nov 06, 2020.