Skip to content

Commit 3c7b707

Browse files
authored
Merge pull request #2718 from adafruit/feather_sense_update
updating example code for the new feather sense
2 parents b171407 + bc7dffd commit 3c7b707

File tree

2 files changed

+66
-24
lines changed

2 files changed

+66
-24
lines changed

Adafruit_Feather_Sense/code.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@
88
import math
99
import board
1010
import audiobusio
11-
import adafruit_apds9960.apds9960
12-
import adafruit_bmp280
13-
import adafruit_lis3mdl
14-
import adafruit_lsm6ds.lsm6ds33
15-
import adafruit_sht31d
11+
from adafruit_apds9960.apds9960 import APDS9960
12+
from adafruit_bmp280 import Adafruit_BMP280_I2C
13+
from adafruit_lis3mdl import LIS3MDL
14+
from adafruit_sht31d import SHT31D
1615

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

20-
apds9960 = adafruit_apds9960.apds9960.APDS9960(i2c)
21-
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
22-
lis3mdl = adafruit_lis3mdl.LIS3MDL(i2c)
23-
lsm6ds33 = adafruit_lsm6ds.lsm6ds33.LSM6DS33(i2c)
24-
sht31d = adafruit_sht31d.SHT31D(i2c)
19+
# check for LSM6DS33 or LSM6DS3TR-C
20+
try:
21+
from adafruit_lsm6ds.lsm6ds33 import LSM6DS33 as LSM6DS
22+
lsm6ds = LSM6DS(i2c)
23+
except RuntimeError:
24+
from adafruit_lsm6ds.lsm6ds3 import LSM6DS3 as LSM6DS
25+
lsm6ds = LSM6DS(i2c)
26+
27+
apds9960 = APDS9960(i2c)
28+
bmp280 = Adafruit_BMP280_I2C(i2c)
29+
lis3mdl = LIS3MDL(i2c)
30+
sht31d = SHT31D(i2c)
2531
microphone = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
2632
sample_rate=16000, bit_depth=16)
2733

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

4349
print("\nFeather Sense Sensor Demo")
4450
print("---------------------------------------------")
45-
print("Proximity:", apds9960.proximity)
46-
print("Red: {}, Green: {}, Blue: {}, Clear: {}".format(*apds9960.color_data))
47-
print("Temperature: {:.1f} C".format(bmp280.temperature))
48-
print("Barometric pressure:", bmp280.pressure)
49-
print("Altitude: {:.1f} m".format(bmp280.altitude))
50-
print("Magnetic: {:.3f} {:.3f} {:.3f} uTesla".format(*lis3mdl.magnetic))
51-
print("Acceleration: {:.2f} {:.2f} {:.2f} m/s^2".format(*lsm6ds33.acceleration))
52-
print("Gyro: {:.2f} {:.2f} {:.2f} dps".format(*lsm6ds33.gyro))
53-
print("Humidity: {:.1f} %".format(sht31d.relative_humidity))
54-
print("Sound level:", normalized_rms(samples))
51+
print(f"Proximity: {apds9960.proximity}")
52+
print(f"Red: {apds9960.color_data[0]}, Green: {apds9960.color_data[1]}, " +
53+
f"Blue: {apds9960.color_data[2]}, Clear: {apds9960.color_data[3]}")
54+
print(f"Temperature: {bmp280.temperature:.1f} C")
55+
print(f"Barometric pressure: {bmp280.pressure}")
56+
print(f"Altitude: {bmp280.altitude:.1f} m")
57+
print(f"Magnetic: {lis3mdl.magnetic[0]:.3f} {lis3mdl.magnetic[1]:.3f} " +
58+
f"{lis3mdl.magnetic[2]:.3f} uTesla")
59+
print(f"Acceleration: {lsm6ds.acceleration[0]:.2f} " +
60+
f"{lsm6ds.acceleration[1]:.2f} {lsm6ds.acceleration[2]:.2f} m/s^2")
61+
print(f"Gyro: {lsm6ds.gyro[0]:.2f} {lsm6ds.gyro[1]:.2f} {lsm6ds.gyro[2]:.2f} dps")
62+
print(f"Humidity: {sht31d.relative_humidity:.1f} %")
63+
print(f"Sound level: {normalized_rms(samples)}")
5564
time.sleep(0.3)

Adafruit_Feather_Sense/feather_sense_sensor_demo/feather_sense_sensor_demo.ino

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
#include <Adafruit_BMP280.h>
77
#include <Adafruit_LIS3MDL.h>
88
#include <Adafruit_LSM6DS33.h>
9+
#include <Adafruit_LSM6DS3TRC.h>
910
#include <Adafruit_SHT31.h>
1011
#include <Adafruit_Sensor.h>
1112
#include <PDM.h>
1213

1314
Adafruit_APDS9960 apds9960; // proximity, light, color, gesture
1415
Adafruit_BMP280 bmp280; // temperautre, barometric pressure
1516
Adafruit_LIS3MDL lis3mdl; // magnetometer
16-
Adafruit_LSM6DS33 lsm6ds33; // accelerometer, gyroscope
17+
Adafruit_LSM6DS3TRC lsm6ds3trc; // accelerometer, gyroscope
18+
Adafruit_LSM6DS33 lsm6ds33;
1719
Adafruit_SHT31 sht30; // humidity
1820

1921
uint8_t proximity;
@@ -24,11 +26,15 @@ float accel_x, accel_y, accel_z;
2426
float gyro_x, gyro_y, gyro_z;
2527
float humidity;
2628
int32_t mic;
29+
long int accel_array[6];
30+
long int check_array[6]={0.00, 0.00, 0.00, 0.00, 0.00, 0.00};
2731

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

36+
bool new_rev = true;
37+
3238
void setup(void) {
3339
Serial.begin(115200);
3440
// while (!Serial) delay(10);
@@ -41,6 +47,28 @@ void setup(void) {
4147
bmp280.begin();
4248
lis3mdl.begin_I2C();
4349
lsm6ds33.begin_I2C();
50+
// check for readings from LSM6DS33
51+
sensors_event_t accel;
52+
sensors_event_t gyro;
53+
sensors_event_t temp;
54+
lsm6ds33.getEvent(&accel, &gyro, &temp);
55+
accel_array[0] = accel.acceleration.x;
56+
accel_array[1] = accel.acceleration.y;
57+
accel_array[2] = accel.acceleration.z;
58+
accel_array[3] = gyro.gyro.x;
59+
accel_array[4] = gyro.gyro.y;
60+
accel_array[5] = gyro.gyro.z;
61+
// if all readings are empty, then new rev
62+
for (int i =0; i < 5; i++) {
63+
if (accel_array[i] != check_array[i]) {
64+
new_rev = false;
65+
break;
66+
}
67+
}
68+
// and we need to instantiate the LSM6DS3TRC
69+
if (new_rev) {
70+
lsm6ds3trc.begin_I2C();
71+
}
4472
sht30.begin();
4573
PDM.onReceive(onPDMdata);
4674
PDM.begin(1, 16000);
@@ -65,7 +93,12 @@ void loop(void) {
6593
sensors_event_t accel;
6694
sensors_event_t gyro;
6795
sensors_event_t temp;
68-
lsm6ds33.getEvent(&accel, &gyro, &temp);
96+
if (new_rev) {
97+
lsm6ds3trc.getEvent(&accel, &gyro, &temp);
98+
}
99+
else {
100+
lsm6ds33.getEvent(&accel, &gyro, &temp);
101+
}
69102
accel_x = accel.acceleration.x;
70103
accel_y = accel.acceleration.y;
71104
accel_z = accel.acceleration.z;
@@ -77,7 +110,7 @@ void loop(void) {
77110

78111
samplesRead = 0;
79112
mic = getPDMwave(4000);
80-
113+
81114
Serial.println("\nFeather Sense Sensor Demo");
82115
Serial.println("---------------------------------------------");
83116
Serial.print("Proximity: ");
@@ -157,4 +190,4 @@ void onPDMdata() {
157190

158191
// 16-bit, 2 bytes per sample
159192
samplesRead = bytesAvailable / 2;
160-
}
193+
}

0 commit comments

Comments
 (0)