-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnano-33-sense-serial-example.ino
333 lines (299 loc) · 11 KB
/
nano-33-sense-serial-example.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
nano-33-sense-serial-example.ino
Copyright (c) 2020 Dale Giancono. All rights reserved..
This program outputs all raw sensor data from the Arduino Nano 33 BLE
Sense board via serial at a 20Hz rate. It also calculates the RMS
value of the microphone buffer and outputs that data. It is intended
as a quick way to become familiar with some of the sensor libraries
available with the Nano 33 BLE Sense, and highlight some of the
difficulties when using a super loop architecture with this board.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**********/
/*INCLUDES*/
/**********/
/* For MP34DT05 microphone */
#include <PDM.h>
/* For LSM9DS1 9-axis IMU sensor */
#include <Arduino_LSM9DS1.h>
/* For APDS9960 Gesture, light, and proximity sensor */
#include <Arduino_APDS9960.h>
/* For LPS22HB barometric barometricPressure sensor */
#include <Arduino_LPS22HB.h>
/* For HTS221 Temperature and humidity sensor */
#include <Arduino_HTS221.h>
/********/
/*MACROS*/
/********/
/* Set these macros to true with you want the output plotted in a way that
* can be viewed with serial plotter. Having them all true creates a pretty
* meaningless graph, as the scaling will be way off for each sensor, and there
* will be too much data to view */
#define SERIAL_PLOT_MP34DT05 (true)
#define SERIAL_PLOT_LSM9DS1 (true)
#define SERIAL_PLOT_APDS9960 (true)
#define SERIAL_PLOT_LPS22HB (true)
#define SERIAL_PLOT_HTS221 (true)
/* This value was also used in the PDM example, seems like a good enough reason to
* continue using it. With this value and 16kHz sampling frequency, the RMS sampling
* period will be 16mS */
#define MICROPHONE_BUFFER_SIZE_IN_WORDS (256U)
#define MICROPHONE_BUFFER_SIZE_IN_BYTES (MICROPHONE_BUFFER_SIZE_IN_WORDS * sizeof(int16_t))
/******************/
/*LOCAL VARIABLES*/
/******************/
/******************/
/*GLOBAL VARIABLES*/
/******************/
/* MP34DT05 Microphone data buffer with a bit depth of 16. Also a variable for the RMS value */
int16_t microphoneBuffer[MICROPHONE_BUFFER_SIZE_IN_WORDS];
int16_t microphoneRMSValue;
/* variables to hold LSM9DS1 accelerometer data */
float accelerometerX, accelerometerY, accelerometerZ;
/* variables to hold LSM9DS1 gyroscope data */
float gyroscopeX, gyroscopeY, gyroscopeZ;
/* variables to hold LSM9DS1 magnetic data */
float magneticX, magneticY, magneticZ;
/* variables to hold LPS22HB barometric pressure data */
float barometricPressure;
/* variables to hold APDS9960 proximity, gesture and colour data */
int proximity, gesture, colourR, colourG, colourB;
/* variables to hold HTS221 temperature and humidity data */
float temperature, humidity;
/* Used to count 1000ms intervals in loop() */
int oldMillis;
int newMillis;
/* Used as a simple flag to know when microphone buffer is full and RMS value
* can be computed. */
bool microphoneBufferReadyFlag;
/****************************/
/*LOCAL FUNCTION PROTOTYPES*/
/****************************/
/****************************/
/*GLOBAL FUNCTION PROTOTYPES*/
/****************************/
/* This function is called each time PDM data is available. It will be used to fill the
* microphone buffer that we will then use to calculate RMS values */
void Microphone_availablePDMDataCallback(void);
/* This function computes the RMS value based on the data contained within the microphoneBuffer
* If the microphone buffer has a word length of 256, and the sample rate for the microphone is 16kHz,
* then this RMS value is taken over (1/16000)*256 = 16mS */
void Micophone_computeRMSValue(void);
/****************************/
/*IMPLEMENTATION*/
/****************************/
void setup()
{
/* Serial setup for UART debugging */
Serial.begin(115200);
/* Wait for serial to be available */
while(!Serial);
/* PDM setup for MP34DT05 microphone */
/* configure the data receive callback to transfer data to local buffer */
PDM.onReceive(Microphone_availablePDMDataCallback);
/* Initialise single PDM channel with a 16KHz sample rate (only 16kHz or 44.1kHz available */
if (!PDM.begin(1, 16000))
{
Serial.println("Failed to start PDM!");
/* Hacky way of stopping program executation in event of failure. */
while(1);
}
else
{
/* Gain values can be from 0 to 80 (around 38db). Check out nrf_pdm.h
* from the nRF528x-mbedos core to confirm this. */
/* This has to be done after PDM.begin() is called as begin() always
* sets the gain as the default PDM.h value (20).
*/
PDM.setGain(50);
}
/* IMU setup for LSM9DS1*/
/* default setup has all sensors active in continous mode. Sample rates
* are as follows: magneticFieldSampleRate = 20Hz, gyroscopeYroscopeSampleRate = 109Hz,
* accelerationSampleRate = 109Hz */
if (!IMU.begin())
{
Serial.println("Failed to initialize IMU!");
/* Hacky way of stopping program executation in event of failure. */
while(1);
}
/* Set sensitivity from 0 to 100. Higher is more sensitive. In
* my experience it requires quite a bit of experimentation to
* get this right, as if it is too sensitive gestures will always
* register as GESTURE_DOWN or GESTURE_UP and never GESTURE_LEFT or
* GESTURE_RIGHT. This can be called before APDS.begin() as it just
* sets an internal sensitivity value.*/
APDS.setGestureSensitivity(50);
if (!APDS.begin())
{
Serial.println("Error initializing APDS9960 sensor.");
/* Hacky way of stopping program executation in event of failure. */
while(1);
}
/* As per Arduino_APDS9960.h, 0=100%, 1=150%, 2=200%, 3=300%. Obviously more
* boost results in more power consumption. */
APDS.setLEDBoost(0);
/* Barometric sensor setup for LPS22HB*/
if (!BARO.begin())
{
Serial.println("Failed to initialize barometricPressure sensor!");
while (1);
}
/* Temperature/Humidity sensor setup for HTS221*/
if (!HTS.begin())
{
Serial.println("Failed to initialize humidity temperature sensor!");
/* Hacky way of stopping program executation in event of failure. */
while(1);
}
/* Initialise timing variables. */
oldMillis = 0;
newMillis = 0;
/* Initialise micophone buffer ready flag */
microphoneBufferReadyFlag = false;
}
char buffer[200];
void loop()
{
/* The sensors that use I2C must be checked to see if data is available, so
* this is checked each loop. This include the IMU and Gesture/light/proximity
* sensors. Other sensors (barometric pressure and temperature/humidity)
* will give a value when we ask for it. These values are requested each
* 1000ms using millis() in a hacky way, but it works.
*
* Data is output via serial every 50ms (20Hz). There is no good way to plot of
* the data from the sensors together due the differing sample rates, but this
* represented a decent compromise as changes will still be observable in all
* sensor data.
*/
/* Get the new millis() value which helps time serial plotting and getting
* of pressure, temperature, and humidity values. */
newMillis = millis();
/* Every 50ms plot all data to serial plotter. */
if((newMillis - oldMillis) % 50)
{
#if (SERIAL_PLOT_MP34DT05 == true)
memset(buffer, 0 , sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%d,", microphoneRMSValue);
Serial.print(buffer);
#endif
#if (SERIAL_PLOT_LSM9DS1 == true)
memset(buffer, 0 , sizeof(buffer));
snprintf(
buffer,
sizeof(buffer),
"%f,%f,%f,%f,%f,%f,%f,%f,%f,",
accelerometerX,
accelerometerY,
accelerometerZ,
gyroscopeX,
gyroscopeY,
gyroscopeZ,
magneticX,
magneticY,
magneticZ);
Serial.print(buffer);
#endif
#if (SERIAL_PLOT_LPS22HB == true)
memset(buffer, 0 , sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%f,", barometricPressure);
Serial.print(buffer);
#endif
#if (SERIAL_PLOT_APDS9960 == true)
memset(buffer, 0 , sizeof(buffer));
snprintf(
buffer,
sizeof(buffer),
"%d,%d,%d,%d,%d",
proximity,
gesture,
colourR,
colourG,
colourB);
Serial.print(buffer);
#endif
#if (SERIAL_PLOT_HTS221 == true)
memset(buffer, 0 , sizeof(buffer));
snprintf(
buffer,
sizeof(buffer),
"%f,%f,",
temperature,
humidity);
Serial.print(buffer);
#endif
Serial.println();
}
/* Every 1000ms get the pressure, temperature, and humidity data */
if((newMillis - oldMillis) > 1000)
{
barometricPressure = BARO.readPressure();
temperature = HTS.readTemperature();
humidity = HTS.readHumidity();
oldMillis = newMillis;
}
/* If new acceleration data is available on the LSM9DS1 get the data.*/
if(IMU.accelerationAvailable())
{
IMU.readAcceleration(accelerometerX, accelerometerY, accelerometerZ);
}
/* If new gyroscope data is available on the LSM9DS1 get the data.*/
if(IMU.gyroscopeAvailable())
{
IMU.readGyroscope(gyroscopeX, gyroscopeY, gyroscopeZ);
}
/* If new magnetic data is available on the LSM9DS1 get the data.*/
if (IMU.magneticFieldAvailable())
{
IMU.readMagneticField(magneticX, magneticY, magneticZ);
}
/* If new proximity data is available on the APDS9960 get the data.*/
if (APDS.proximityAvailable())
{
proximity = APDS.readProximity();
}
/* If new colour data is available on the APDS9960 get the data.*/
if (APDS.colorAvailable())
{
APDS.readColor(colourR, colourG, colourB);
}
/* If new gesture data is available on the APDS9960 get the data.*/
if (APDS.gestureAvailable())
{
gesture = APDS.readGesture();
}
/* If the microphone buffer is full, compute the RMS value */
if(microphoneBufferReadyFlag)
{
Micophone_computeRMSValue();
microphoneBufferReadyFlag = false;
}
}
void Microphone_availablePDMDataCallback()
{
// query the number of bytes available
int bytesAvailable = PDM.available();
if(bytesAvailable == MICROPHONE_BUFFER_SIZE_IN_BYTES)
{
PDM.read(microphoneBuffer, bytesAvailable);
microphoneBufferReadyFlag = true;
}
}
void Micophone_computeRMSValue(void)
{
uint16_t sum = 0;
for(int i = 0; i < MICROPHONE_BUFFER_SIZE_IN_WORDS; i++)
{
sum = sum + pow(microphoneBuffer[i], 2);
}
microphoneRMSValue = sqrt(sum/MICROPHONE_BUFFER_SIZE_IN_WORDS);
}