Home | pfodApps/pfodDevices | WebStringTemplates | Java/J2EE | Unix | Torches | Superannuation | CRPS Treatment | | About Us
 

Forward Logo (image)      

High Speed NANO 33 BLE
Remote Data Logging and Plotting

by Matthew Ford 30th Sept 2024 (originally posted 29th September 2024)
© Forward Computing and Control Pty. Ltd. NSW Australia
All rights reserved.

High Speed Mobile Data Logging and Plotting
for Arduino NANO 33 BLE
No Android Programming Required

This project shows how to use your Android mobile to plot and log high speed data, like Accelerometer and Gyroscope data, at approximately 100Hz, from an Arduino NANO 33 BLE or BLE Sense using pfodApp and pfodDesigner

The Arduino NANO 33 BLE and Sense are small, low power BLE boards with onboard Accelerometer and Gyroscope which provide data at 104Hz, making them ideal for wearable projects. (The Sense version has additional environmental sensors). There are two version of the NANO 33 BLE and NANO 33 BLE Ver 2, uses different Accelerometer and Gyroscope chips. This project has code examples for both versions

The pfodDesigner free Android app lets you create menu systems to control your NANO 33 BLE (and many other boards) and create charts to plot your data. The pfodDesigner then generate the Arduino sketch that will show those menus/charts on your Android mobile, using (paid) pfodApp. See Arduino NANO 33 Made Easy No Coding required. The free pfodGUIdesigner app lets you create your own interactive GUIs and also generate an Arduino sketch. See Design interactive GUI components for pfodApp for a tutorial. Both these designer app use the same front end as pfodApp, so what you seen on your mobile is exactly what the generated sketch will display.

The pfodApp is a general purpose app that does not have any built in menus. All the interface, menus, buttons, GUIs, charts, etc are completely defined and controlled by the code in your Arduino sketch. GUI designs are very powerful and responsive, see some of the interactive interfaces on the pfodApp home page, but they require more effort to design, using pfodGUIdesigner, and to integrate into your final project. As a first start, using the pfodDesigner is the easiest way to get up and running and that will be used here.

Note About NANO 33 BLE uploads: If the upload fails, try unplugging the board, closing the IDE, re-start the IDE and plug the board back in.

The Components

The Parts List. (Prices as at 30th September 2024 excluding shipping)

1 x Arduino NANO 33 BLE Rev 2 ~US$25.80 (OR Arduino NANO 33 BLE Sense Rev 2 ~US$32.40)
1 x pfodApp Android app V3.0.424 or later
~AU$13 (Note: It must be V3.0.424 or later)

Optional
1 x pfodDesigner Android app V3.0.4241 or later– free (optional but recommended for doing your own designs)
1 x pfodGUIdesigner Android app V3.0.4241 or later – free

Arduino IDE V1.8.19+ and Arduino Mbed OS Nano boards V4.1.5 board support (installed from Arduino IDE) and supporting libraries from this libraries.zip file

High Speed Logging – Quick Start

Here is the final sketch (Nano33BLE_Rev2_HS.ino), and supporting libraries.zip that will chart and log the Gyroscope x,y,z from the Arduino NANO 33 BLE (and Sense). (Nano33BLE_HS.ino is for the original NANO 33 BLE)

Installation:

Rename your existing libraries directory, in the Arduino Sketch directory (e.g. to libraries_mine) and unzip libraries.zip to the Arduino Sketch directory.
Put Nano33BLE_Rev2_HS.ino in a folder called
Nano33BLE_Rev2_HS in the Arduino Sketch directory. (Assuming your board is a Rev2, otherwise use Nano33BLE_HS.ino)
Open the Nano33BLE_HS sketch and set the IMU include for your NANO 33 BLE version if it is not Rev 2. See the top of the sketch. Compile and upload to your board.

Download and install pfodApp, on you Android mobile and click the BLE button to search BLE devices, click on your NANO 33 to open the Edit Connection screen. See pfodAppForAndroidGettingStarted.pdf
Important: Edit the Keep Alive in Seconds to 0 to suppress keep alive msgs. So that only data will be sent when plotting.

Save the connection and then use the Back key to return to the list of connections. Click Nano 33 BLE to connect. Next time you start pfodApp it will automatically connect to Nano 33 BLE if this is the only connection that has been setup.

Click the Plot Gyroscope button to start plotting and logging the Gyroscope data. The plot is auto scaling.
Going Back to the Plot Gyroscope button stops the data flow.

Use one finger to drag the plot and two fingers to zoom in.

You can also view the Chart in Landscape

To Save the data, click the top right … menu and select Raw Data

To open the Raw Data Screen.

and use the Save button to save the CSV data file, Nano_33_BLE.txt, to your mobile's Downloads directory. From there you can transfer it to your computer for importing into a spreadsheet for further analysis.

Creating your own Menu and High Speed Charts.

This section covers creating your own menu and high speed charts. You may want to do this so that you can vary or display parameters of the NANO 33 BLE sketch between taking logs and charting results

Designing the Initial Menu and Generating the Arduino Code

To design the initial menu, you can use the free pfodDesigner app. Arduino NANO 33 Made Easy covers using creating menus and adding Adding a Chart and Logging Data covers adding a chart. The pfodDesigner page provide a range of other tutorials on creating you own menus. For more interactive and custom interfaces you can use the free pfodGUIdesigner

In this tutorial there will be just one menu item, a Chart Button, which will open the plot of the Gyroscope data. Follow Arduino NANO 33 Made Easy and How to Display/Plot Arduino Data on Android to create a simple menu with just a Chart Button.

Here the Edit Text has been used to enter Plot Gyroscope as the chart button name. Clicking on the Plot Gyroscope button opens the Chart editing screen which lets you choose the X-axis display, Separate or Combined plots and the Plot Data Interval.

Clicking the Edit Plot … buttons lets you edit each plot.

In the Editing Plot screen you can set the display max, min and the input data range. The generated sketch will then scale the data range to the (max,min) range. In this case, since the Gyroscope data is already correctly scaled, you can ignore those settings. The respective scaling code will be commented out later.

Finally generate the Arduino code that will display that menu and chart on your mobile, via pfodApp. Note you should have previously chosen the Target as Nano 33 BLE. The Change Target button lets you choose a wide range of target boards to generate code for.

When the generated code is displayed, save it and then exit the pfodDesigner app and transfer the generated file pfodDesignerV3.txt to your computer.

Here is that initial generated sketch pfodDesignerV3.txt, Rename your existing libraries directory, in the Arduino Sketch directory (e.g. to libraries_mine) and unzip libraries.zip to the Arduino Sketch directory. Then compile and load that sketch in your NANO 33 BLE board. Connecting pfodApp, as described at the top of this project, will display the Plot Gyroscope button and an empty chart.

This initial sketch is then modified, in stages, to one that sends and charts and logs the Gyroscope data at ~100Hz. First add the Gyroscope readings, or whatever readings you want to send.

Add #include "Arduino_BMI270_BMM150.h" for Rev 2 boards or #include <Arduino_LSM9DS1.h> for the original boards.
Add the Gyroscope globals
float x,y,z;
In setup() start the IMU

  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }

In loop() read the Gyroscope data

  if (IMU.gyroscopeAvailable()) {
    IMU.readGyroscope(x, y, z);
  }

and change the sendData() method to send the Gyroscope variables

void sendData() {
  if (plotDataTimer.justFinished()) {
    plotDataTimer.restart(); // restart plot data timer
    parser.print(millis()-plot_msOffset);// time in milliseconds
    parser.print(','); parser.print(x); // plot the Gyroscope values
    parser.print(','); parser.print(y);
    parser.print(','); parser.print(z);
    parser.println(); // end of CSV data record
  }
}

Finally near the top of the sketch, reduce the unsigned long PLOT_DATA_INTERVAL from 1000 to 1ms

unsigned long PLOT_DATA_INTERVAL = 1;

The sketch, HS_Gyro_Rev2.ino has incorporates these changes. Its IMU include is for a Rev 2 board. Uploading that sketch to your NANO 33 BLE Rev2 board gives you a live plot of the Gyroscope when you press the Plot Gyroscope button.

Opening the Raw Data screen (from the … menu top right), shows you the CSV data and lets you save the plot data to a file for later download and processing. If you did not Delete the Raw Data from previous runs, you will find multiple data sets one after another. The latest is at the bottom of the file. Looking at the milliseconds field, the first one, shows that there is about 20ms (50Hz or so sample rate) between each sample even though the PLOT_DATA_INTERVAL was set to 1ms

10787,-29.36,-5.00,108.95
10809,65.37,-6.84,106.20
10831,322.20,-9.34,63.96
10854,837.59,-22.64,-45.10

This is because, by default, the BLE connection is set to INDICATE which forces each packet sent to be ack by the Android Mobile, before the next back can be sent. The BLE write blocks until that ack is received. To get faster data transfer you can disable INDICATE by commenting out the #define BLE_INDICATE at the top of the sketch. Running the sketch again gives data like this.

18875,-969.54,-107.06,66.83
18876,-969.54,-107.06,66.83
18883,-969.54,-107.06,66.83
18885,-1154.91,-129.03,92.90
18886,-1154.91,-129.03,92.90

The data rate is now above 100Hz but with repeated readings since the Gyroscope itself only provides new reading at about 100Hz. Also you may find some problems connecting as the plot data starts being sent as soon as pfodApp connects and competes with the pfodApp msgs that display the menu and since Indicate is no longer being used, occasionally a packet will be lost. If pfodApp was waiting that response packet, it will timeout and start a reconnection.

The final set of modifications only sends plot data when the chat is showing and only when there is a new data from the Gyroscope.

Add two new variables

bool plotData = false;  // only send data when chart open
bool haveNewData = false; // set to true when new data read and set to false when data sent

Set plotData false whenever a new pfodApp command is received and then set it true when sending back the chart msg to open the plot. haveNewData is set true when new data is read from the Gyroscope and set to false once it has been sent. The sendData() method becomes

void sendData() {
  if (plotData && haveNewData) { // only send data if chart is showing and have new data
    parser.print(millis()-plot_msOffset);// time in milliseconds
    parser.print(','); parser.print(x); // plot the Gyroscope values
    parser.print(','); parser.print(y);
    parser.print(','); parser.print(z);
    parser.println(); // end of CSV data record
    haveNewData = false; // clear flag
  }
}

The default number of data points displayed on the chart is 500, with this data rate (~100Hz) that is only about 5sec of data which then scrolls off the chart as new data arrives. Increasing the number of points to 1000 gives a little under 9sec of data displayed on the chart. Adding `1000 after the chart name does this

parser.print(F("{=Gyroscope`1000~ss.S"));

See pfodSpecification.pdf page 71 onwards for all the plotting options.

This is the final sketch, Nano33BLE_Rev2_HS.ino (and Nano33BLE_HS.ino is for the original NANO 33 BLE). On the pfodApp side, the connection to the NANO should have its Keep Alive set to 0 to stop pfodApp sending messages while the chart is open, that would stop the sendData.

As noted above occasionally a BLE packet is lost, however the pfodBLESerial class buffers the prints until either the packet if full OR a newline is found. This means CSV data lines of less than 240 chars will be sent complete in one packet. If a packet is lost, then those samples are lost but the other samples are not corrupted. If the CSV data lines exceed 240 chars than a missing packet results in a partial line that combines with the next one leading to corrupt data, so keep the plot data CSV lines to less than 240 chars. For the Gyroscope data above the line is less than 40char so there is a lot of space left to send additional data fields, like the accelerometer data, in the same line.

Note: The chart only plots data from CSV lines with the same number of fields as | in the chart msg. See pfodSpecification.pdf page 71 onwards for all the plotting options.

Conclusion

This tutorial has shown how you can easily plot and log the Arduino NANO 33 BLE Gyroscope (and Accelerometer) data as fast as it is produced.
No Android programming is required. pfodApp handles all of that.
Almost No Arduino coding is required. The (free) pfodDesignerV2 generates complete working sketches that you can modify to send the data you want.

AndroidTM is a trademark of Google Inc. For use of the Arduino name see http://arduino.cc/en/Main/FAQ


The General Purpose Android/Arduino Control App.
pfodDevice™ and pfodApp™ are trade marks of Forward Computing and Control Pty. Ltd.




Forward home page link (image)

Contact Forward Computing and Control by
©Copyright 1996-2024 Forward Computing and Control Pty. Ltd. ACN 003 669 994