Skip to content

Commit e205139

Browse files
committed
Added sensor test sketches
1 parent b8cde4b commit e205139

File tree

3 files changed

+358
-0
lines changed

3 files changed

+358
-0
lines changed

examples/test_IMU/test_IMU.ino

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Active Learning Labs
3+
Harvard University
4+
tinyMLx - Sensor Test
5+
6+
Requires the Arduino_LSM9DS1 library library
7+
*/
8+
9+
#include <Arduino_LSM9DS1.h>
10+
11+
int imuIndex = 0; // 0 - accelerometer, 1 - gyroscope, 2 - magnetometer
12+
bool commandRecv = false; // flag used for indicating receipt of commands from serial port
13+
bool startStream = false;
14+
15+
void setup() {
16+
Serial.begin(9600);
17+
while (!Serial);
18+
19+
// Initialize IMU
20+
if (!IMU.begin()) {
21+
Serial.println("Failed to initialize IMU");
22+
while (1);
23+
}
24+
25+
Serial.println("Welcome to the IMU test for the built-in IMU on the Nano 33 BLE Sense\n");
26+
Serial.println("Available commands:");
27+
Serial.println("a - display accelerometer readings in g's in x, y, and z directions");
28+
Serial.println("g - display gyroscope readings in deg/s in x, y, and z directions");
29+
Serial.println("m - display magnetometer readings in uT in x, y, and z directions");
30+
}
31+
32+
void loop() {
33+
String command;
34+
35+
// Read incoming commands from serial monitor
36+
while (Serial.available()) {
37+
char c = Serial.read();
38+
if ((c != '\n') && (c != '\r')) {
39+
command.concat(c);
40+
}
41+
else if (c == '\r') {
42+
commandRecv = true;
43+
command.toLowerCase();
44+
}
45+
}
46+
47+
// Command interpretation
48+
if (commandRecv) {
49+
commandRecv = false;
50+
if (command == "a") {
51+
imuIndex = 0;
52+
if (!startStream) {
53+
startStream = true;
54+
}
55+
delay(3000);
56+
}
57+
else if (command == "g") {
58+
imuIndex = 1;
59+
if (!startStream) {
60+
startStream = true;
61+
}
62+
delay(3000);
63+
}
64+
else if (command == "m") {
65+
imuIndex = 2;
66+
if (!startStream) {
67+
startStream = true;
68+
}
69+
delay(3000);
70+
}
71+
}
72+
73+
74+
float x, y, z;
75+
if (startStream) {
76+
if (imuIndex == 0) { // testing accelerometer
77+
if (IMU.accelerationAvailable()) {
78+
IMU.readAcceleration(x, y, z);
79+
80+
Serial.print("Ax:");
81+
Serial.print(x);
82+
Serial.print(' ');
83+
Serial.print("Ay:");
84+
Serial.print(y);
85+
Serial.print(' ');
86+
Serial.print("Az:");
87+
Serial.println(z);
88+
}
89+
}
90+
else if (imuIndex == 1) { // testing gyroscope
91+
if (IMU.gyroscopeAvailable()) {
92+
IMU.readGyroscope(x, y, z);
93+
94+
Serial.print("wx:");
95+
Serial.print(x);
96+
Serial.print(' ');
97+
Serial.print("wy:");
98+
Serial.print(y);
99+
Serial.print(' ');
100+
Serial.print("wz:");
101+
Serial.println(z);
102+
}
103+
}
104+
else if (imuIndex == 2) { // testing magnetometer
105+
if (IMU.magneticFieldAvailable()) {
106+
IMU.readMagneticField(x, y, z);
107+
108+
Serial.print("Bx:");
109+
Serial.print(x);
110+
Serial.print(' ');
111+
Serial.print("By:");
112+
Serial.print(y);
113+
Serial.print(' ');
114+
Serial.print("Bz:");
115+
Serial.println(z);
116+
}
117+
}
118+
}
119+
}

examples/test_camera/test_camera.ino

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
Active Learning Labs
3+
Harvard University
4+
tinyMLx - OV7675 Camera Test
5+
6+
Requires the the Arduino_OV767X library
7+
*/
8+
9+
#include <Arduino_OV767X.h>
10+
11+
#define BUTTON_PIN 13
12+
13+
bool commandRecv = false; // flag used for indicating receipt of commands from serial port
14+
bool liveFlag = false; // flag as true to live stream raw camera bytes, set as false to take single images on command
15+
bool captureFlag = false;
16+
17+
// Variables for button debouncing
18+
unsigned long lastDebounceTime = 0;
19+
unsigned long debounceDelay = 50;
20+
bool lastButtonState = HIGH;
21+
bool buttonState;
22+
23+
// Image buffer;
24+
byte image[176 * 144 * 2]; // QCIF: 176x144 x 2 bytes per pixel (RGB565)
25+
int bytesPerFrame;
26+
27+
void setup() {
28+
Serial.begin(9600);
29+
while (!Serial);
30+
31+
pinMode(BUTTON_PIN, OUTPUT);
32+
digitalWrite(BUTTON_PIN, HIGH);
33+
nrf_gpio_cfg_out_with_input(digitalPinToPinName(BUTTON_PIN));
34+
35+
// Initialize camera
36+
if (!Camera.begin(QCIF, RGB565, 1)) {
37+
Serial.println("Failed to initialize camera");
38+
while (1);
39+
}
40+
bytesPerFrame = Camera.width() * Camera.height() * Camera.bytesPerPixel();
41+
42+
Serial.println("Welcome to the OV7675 test\n");
43+
Serial.println("Available commands:\n");
44+
Serial.println("single - take a single image and print out the hexadecimal for each pixel (default)");
45+
Serial.println("live - the raw bytes of images will be streamed live over the serial port");
46+
Serial.println("capture - when in single mode, initiates an image capture");
47+
}
48+
49+
void loop() {
50+
int i = 0;
51+
String command;
52+
53+
bool buttonRead = nrf_gpio_pin_read(digitalPinToPinName(BUTTON_PIN));
54+
55+
if (buttonRead != lastButtonState) {
56+
lastDebounceTime = millis();
57+
}
58+
59+
if (millis() - lastDebounceTime >= debounceDelay) {
60+
if (buttonRead != buttonState) {
61+
buttonState = buttonRead;
62+
63+
if (!buttonState) {
64+
if (!liveFlag) {
65+
if (!captureFlag) {
66+
captureFlag = true;
67+
}
68+
}
69+
}
70+
}
71+
}
72+
73+
lastButtonState = buttonRead;
74+
75+
// Read incoming commands from serial monitor
76+
while (Serial.available()) {
77+
char c = Serial.read();
78+
if ((c != '\n') && (c != '\r')) {
79+
command.concat(c);
80+
}
81+
else if (c == '\r') {
82+
commandRecv = true;
83+
command.toLowerCase();
84+
}
85+
}
86+
87+
// Command interpretation
88+
if (commandRecv) {
89+
commandRecv = false;
90+
if (command == "live") {
91+
Serial.println("\nRaw image data will begin streaming in 5 seconds...");
92+
liveFlag = true;
93+
delay(5000);
94+
}
95+
else if (command == "single") {
96+
Serial.println("\nCamera in single mode, type \"capture\" to initiate an image capture");
97+
liveFlag = false;
98+
delay(200);
99+
}
100+
else if (command == "capture") {
101+
if (!liveFlag) {
102+
if (!captureFlag) {
103+
captureFlag = true;
104+
}
105+
delay(200);
106+
}
107+
else {
108+
Serial.println("\nCamera is not in single mode, type \"single\" first");
109+
delay(1000);
110+
}
111+
}
112+
}
113+
114+
if (liveFlag) {
115+
Camera.readFrame(image);
116+
Serial.write(image, bytesPerFrame);
117+
}
118+
else {
119+
if (captureFlag) {
120+
captureFlag = false;
121+
Camera.readFrame(image);
122+
Serial.println("\nImage data will be printed out in 3 seconds...");
123+
delay(3000);
124+
for (int i = 0; i < bytesPerFrame - 1; i += 2) {
125+
Serial.print("0x");
126+
Serial.print(image[i+1], HEX);
127+
Serial.print(image[i], HEX);
128+
if (i != bytesPerFrame - 2) {
129+
Serial.print(", ");
130+
}
131+
}
132+
Serial.println();
133+
}
134+
}
135+
}
136+
137+
void nrf_gpio_cfg_out_with_input(uint32_t pin_number) {
138+
nrf_gpio_cfg(
139+
pin_number,
140+
NRF_GPIO_PIN_DIR_OUTPUT,
141+
NRF_GPIO_PIN_INPUT_CONNECT,
142+
NRF_GPIO_PIN_PULLUP,
143+
NRF_GPIO_PIN_S0S1,
144+
NRF_GPIO_PIN_NOSENSE);
145+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Active Learning Labs
3+
Harvard University
4+
tinyMLx - Built-in Microphone Test
5+
*/
6+
7+
#include <PDM.h>
8+
9+
#define BUTTON_PIN 13
10+
11+
// PDM buffer
12+
short sampleBuffer[256];
13+
volatile int samplesRead;
14+
15+
// Variables for button debouncing
16+
unsigned long lastDebounceTime = 0;
17+
unsigned long debounceDelay = 50;
18+
bool lastButtonState = HIGH;
19+
bool buttonState;
20+
21+
bool record = false;
22+
23+
void setup() {
24+
Serial.begin(9600);
25+
while (!Serial);
26+
27+
pinMode(BUTTON_PIN, OUTPUT);
28+
digitalWrite(BUTTON_PIN, HIGH);
29+
nrf_gpio_cfg_out_with_input(digitalPinToPinName(BUTTON_PIN));
30+
31+
PDM.onReceive(onPDMdata);
32+
// Initialize PDM microphone in mono mode with 16 kHz sample rate
33+
if (!PDM.begin(1, 16000)) {
34+
Serial.println("Failed to start PDM");
35+
while (1);
36+
}
37+
38+
Serial.println("Welcome to the microphone test for the built-in microphone on the Nano 33 BLE Sense\n");
39+
Serial.println("Use the on-shield button to start and stop an audio recording");
40+
Serial.println("Open the Serial Plotter to view the corresponding waveform");
41+
}
42+
43+
void loop() {
44+
bool buttonRead = nrf_gpio_pin_read(digitalPinToPinName(BUTTON_PIN));
45+
46+
if (buttonRead != lastButtonState) {
47+
lastDebounceTime = millis();
48+
}
49+
50+
if (millis() - lastDebounceTime >= debounceDelay) {
51+
if (buttonRead != buttonState) {
52+
buttonState = buttonRead;
53+
54+
if (!buttonState) {
55+
record = !record;
56+
}
57+
}
58+
}
59+
60+
lastButtonState = buttonRead;
61+
62+
if (samplesRead) {
63+
64+
// print samples to serial plotter
65+
if (record) {
66+
for (int i = 0; i < samplesRead; i++) {
67+
Serial.println(sampleBuffer[i]);
68+
}
69+
}
70+
71+
// clear read count
72+
samplesRead = 0;
73+
}
74+
}
75+
76+
void onPDMdata() {
77+
// query the number of bytes available
78+
int bytesAvailable = PDM.available();
79+
80+
// read data into the sample buffer
81+
PDM.read(sampleBuffer, bytesAvailable);
82+
83+
samplesRead = bytesAvailable / 2;
84+
}
85+
86+
void nrf_gpio_cfg_out_with_input(uint32_t pin_number) {
87+
nrf_gpio_cfg(
88+
pin_number,
89+
NRF_GPIO_PIN_DIR_OUTPUT,
90+
NRF_GPIO_PIN_INPUT_CONNECT,
91+
NRF_GPIO_PIN_PULLUP,
92+
NRF_GPIO_PIN_S0S1,
93+
NRF_GPIO_PIN_NOSENSE);
94+
}

0 commit comments

Comments
 (0)