Skip to content

Commit

Permalink
updating example code for the new feather sense
Browse files Browse the repository at this point in the history
updating CP and Arduino examples to check for the LSM6DS33 or the LSM6DS3TR-C
  • Loading branch information
BlitzCityDIY committed Jan 24, 2024
1 parent e2f476f commit fe39638
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
49 changes: 29 additions & 20 deletions Adafruit_Feather_Sense/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@
import math
import board
import audiobusio
import adafruit_apds9960.apds9960
import adafruit_bmp280
import adafruit_lis3mdl
import adafruit_lsm6ds.lsm6ds33
import adafruit_sht31d
from adafruit_apds9960.apds9960 import APDS9960
from adafruit_bmp280 import Adafruit_BMP280_I2C
from adafruit_lis3mdl import LIS3MDL
from adafruit_sht31d import SHT31D

i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller

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)
# check for LSM6DS33 or LSM6DS3TR-C
try:
from adafruit_lsm6ds.lsm6ds33 import LSM6DS33 as LSM6DS
lsm6ds = LSM6DS(i2c)
except RuntimeError:
from adafruit_lsm6ds.lsm6ds3 import LSM6DS3 as LSM6DS
lsm6ds = LSM6DS(i2c)

apds9960 = APDS9960(i2c)
bmp280 = Adafruit_BMP280_I2C(i2c)
lis3mdl = LIS3MDL(i2c)
sht31d = SHT31D(i2c)
microphone = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
sample_rate=16000, bit_depth=16)

Expand All @@ -42,14 +48,17 @@ def normalized_rms(values):

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))
print(f"Proximity: {apds9960.proximity}")
print(f"Red: {apds9960.color_data[0]}, Green: {apds9960.color_data[1]}, " +
f"Blue: {apds9960.color_data[2]}, Clear: {apds9960.color_data[3]}")
print(f"Temperature: {bmp280.temperature:.1f} C")
print(f"Barometric pressure: {bmp280.pressure}")
print(f"Altitude: {bmp280.altitude:.1f} m")
print(f"Magnetic: {lis3mdl.magnetic[0]:.3f} {lis3mdl.magnetic[1]:.3f} " +
f"{lis3mdl.magnetic[2]:.3f} uTesla")
print(f"Acceleration: {lsm6ds.acceleration[0]:.2f} " +
f"{lsm6ds.acceleration[1]:.2f} {lsm6ds.acceleration[2]:.2f} m/s^2")
print(f"Gyro: {lsm6ds.gyro[0]:.2f} {lsm6ds.gyro[1]:.2f} {lsm6ds.gyro[2]:.2f} dps")
print(f"Humidity: {sht31d.relative_humidity:.1f} %")
print(f"Sound level: {normalized_rms(samples)}")
time.sleep(0.3)
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
#include <Adafruit_BMP280.h>
#include <Adafruit_LIS3MDL.h>
#include <Adafruit_LSM6DS33.h>
#include <Adafruit_LSM6DS3TRC.h>
#include <Adafruit_SHT31.h>
#include <Adafruit_Sensor.h>
#include <PDM.h>

Adafruit_APDS9960 apds9960; // proximity, light, color, gesture
Adafruit_BMP280 bmp280; // temperautre, barometric pressure
Adafruit_LIS3MDL lis3mdl; // magnetometer
Adafruit_LSM6DS33 lsm6ds33; // accelerometer, gyroscope
Adafruit_LSM6DS3TRC lsm6ds3trc; // accelerometer, gyroscope
Adafruit_LSM6DS33 lsm6ds33;
Adafruit_SHT31 sht30; // humidity

uint8_t proximity;
Expand All @@ -24,11 +26,15 @@ float accel_x, accel_y, accel_z;
float gyro_x, gyro_y, gyro_z;
float humidity;
int32_t mic;
long int accel_array[6];
long int check_array[6]={0.00, 0.00, 0.00, 0.00, 0.00, 0.00};

extern PDMClass PDM;
short sampleBuffer[256]; // buffer to read samples into, each sample is 16-bits
volatile int samplesRead; // number of samples read

bool new_rev = true;

void setup(void) {
Serial.begin(115200);
// while (!Serial) delay(10);
Expand All @@ -41,6 +47,30 @@ void setup(void) {
bmp280.begin();
lis3mdl.begin_I2C();
lsm6ds33.begin_I2C();
// check for readings from LSM6DS33
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t temp;
lsm6ds33.getEvent(&accel, &gyro, &temp);
accel_array[0] = accel.acceleration.x;
accel_array[1] = accel.acceleration.y;
accel_array[2] = accel.acceleration.z;
accel_array[3] = gyro.gyro.x;
accel_array[4] = gyro.gyro.y;
accel_array[5] = gyro.gyro.z;
// if all readings are empty, then new rev
for (int i =0; i < 5; i++) {
if (accel_array[i] != check_array[i]) {
new_rev = false;
}
else {
new_rev = true;
}
}
// and we need to instantiate the LSM6DS3TRC
if (new_rev = true) {
lsm6ds3trc.begin_I2C();
}
sht30.begin();
PDM.onReceive(onPDMdata);
PDM.begin(1, 16000);
Expand All @@ -65,7 +95,12 @@ void loop(void) {
sensors_event_t accel;
sensors_event_t gyro;
sensors_event_t temp;
lsm6ds33.getEvent(&accel, &gyro, &temp);
if (new_rev = true) {
lsm6ds3trc.getEvent(&accel, &gyro, &temp);
}
else {
lsm6ds33.getEvent(&accel, &gyro, &temp);
}
accel_x = accel.acceleration.x;
accel_y = accel.acceleration.y;
accel_z = accel.acceleration.z;
Expand All @@ -77,7 +112,7 @@ void loop(void) {

samplesRead = 0;
mic = getPDMwave(4000);

Serial.println("\nFeather Sense Sensor Demo");
Serial.println("---------------------------------------------");
Serial.print("Proximity: ");
Expand Down Expand Up @@ -157,4 +192,4 @@ void onPDMdata() {

// 16-bit, 2 bytes per sample
samplesRead = bytesAvailable / 2;
}
}

0 comments on commit fe39638

Please sign in to comment.