Skip to content

Commit c2d8e44

Browse files
authored
Merge pull request #262 from arduino/karlsoderby/nanorp2040-mp-files
Nano RP2040 Data Logger [MKC-477]
2 parents e699626 + f4d8b50 commit c2d8e44

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: 'Nano RP2040 Datalogger with MicroPython'
3+
difficulty: easy
4+
compatible-products: [nano-rp2040-connect]
5+
description: 'Learn how to save data in .csv format on the Nano RP2040 Connect, using MicroPython.'
6+
tags:
7+
- Datalogger
8+
- MicroPython
9+
author: 'Karl Söderby'
10+
hardware:
11+
- hardware/03.nano/boards/nano-rp2040-connect
12+
software:
13+
- web-editor
14+
---
15+
16+
## Introduction
17+
18+
The [Nano RP2040 Connect](https://store.arduino.cc/nano-rp2040-connect) board has on-board storage that allows you to turn the device into a data logger without any extra components.
19+
20+
In order to utilize this feature, we need to install the latest release of [OpenMV's flavor of MicroPython](https://github.com/openmv/openmv/).
21+
22+
This tutorial can be completed with only the Nano RP2040 Connect board and open-source software.
23+
24+
***If you are unfamiliar with Arduino & Python, you can learn more by reading the [Python with Arduino](/learn/programming/arduino-and-python) and the [Nano RP2040 Connect Python API Guide](/tutorials/nano-rp2040-connect/rp2040-python-api) articles.***
25+
26+
## Goals
27+
28+
The goals of this tutorial are:
29+
30+
- Install OpenMV MicroPython firmware on the board.
31+
- Learn how to save data in a `.csv` format directly on the Nano RP2040 Connect.
32+
33+
## Hardware & Software Needed
34+
35+
- [Thonny IDE](https://thonny.org/).
36+
- [Arduino Nano RP2040 Connect](https://store.arduino.cc/nano-rp2040-connect).
37+
38+
## Install MicroPython
39+
40+
**1.** The first step is to install the latest version of the OpenMV firmware (MicroPython) on the Nano RP2040 board. This version is available through the link below:
41+
42+
- [OpenMV firmware v4.3.1. (download)](https://github.com/openmv/openmv/releases/download/v4.3.1/firmware_v4.3.1.zip)
43+
44+
**2.** Unzip the contents, and locate the `firmware.uf2` file inside of the **ARDUINO_NANO_RP2040_CONNECT** folder.
45+
46+
**3.** Force the bootloader on the Nano RP2040 Connect, by connecting a jumper wire between `GND` and `REC` pins as shown in the image below. When the mass storage device opens, drag the `firmware.uf2` onto it, and the latest version will install.
47+
48+
![Connect GND + REC + press the reset button to open mass storage.](assets/SHORT-REC-NANORP2040CONNECT.png)
49+
50+
**4.** Install and open the [Thonny IDE](https://thonny.org/). Navigate to **Run > Select Interpreter** and choose the **"MicroPython(generic)"** from the list. Your board should now appear in the other dropdown menu:
51+
52+
![Thonny board/port selection.](assets/thonny-select-interpreter.png)
53+
54+
If your board appears, it has been successful. In this case, it is called `Board in FS mode (/dev/cu.usbmodem11201)`.
55+
56+
## Data Logger Example
57+
58+
Now that the OpenMV MicroPython firmware is installed on your device, and it is detected using Thonny, we can create our datalogger.
59+
60+
The script for the datalogger is quite basic, and has the following functionality:
61+
62+
- Create a `.csv` file
63+
- Read the value of an analog pin, and log it, using the `file.write()` function.
64+
- Repeat 25 times and then finish script.
65+
- Each time a reading is recorded, the built-in LED flashes.
66+
67+
### Code
68+
69+
The script for this tutorial can be found below:
70+
71+
```python
72+
import machine
73+
from machine import Pin
74+
import time
75+
76+
adc_pin = machine.Pin(29)
77+
adc = machine.ADC(adc_pin)
78+
led = Pin(6, Pin.OUT)
79+
readings = 0
80+
81+
# create a file named "data.csv"
82+
file=open("data.csv","w")
83+
file.write("data"+"\n")
84+
85+
while True:
86+
87+
led.value(1)
88+
reading = adc.read_u16()
89+
print("ADC: ",reading)
90+
91+
time.sleep_ms(100)
92+
93+
# convert and write the reading from the analog pin
94+
file.write(str(reading)+"\n")
95+
96+
led.value(0)
97+
time.sleep_ms(100)
98+
readings += 1
99+
100+
# if 25 readings are done, finish the program
101+
if readings >= 25:
102+
file.close()
103+
break
104+
```
105+
106+
Copy paste this code into the Thonny editor, and click on the **Green Play Button (F5)**. The values recorded are also printed in the terminal, so we can compare it later. After running, it should look like this:
107+
108+
![Thonny IDE, data printed in terminal.](assets/thonny-terminal.png)
109+
110+
When you run the script, the board should now start blinking fast, every 100 milliseconds, and it will do so 25 times (as is specified in the code, the number can be changed).
111+
112+
### Accessing Data
113+
114+
Once done, navigate to Finder / Explorer, and locate a drive called **"NO NAME"**. This should now include a `data.csv` file. This contains the 25 readings we just made by running the script.
115+
116+
***If you are using a Mac, you may need to change a setting that allows you to see external disks. If you can't see the drive, go to Finder > Preferences and tick the boxes that appear.***
117+
118+
![The "NO NAME" drive with a data.csv file.](assets/storage-device.png)
119+
120+
Congratulations, you have now successfully recorded data and stored it in a `.csv` file onboard the Nano RP2040 Connect.
121+
122+
***Please note that you should never open this file whenever a file management operation is ongoing. This will most likely corrupt your file and you won't be able to obtain the data. Best practice is to record the data, wait a little, and then open up the `data.csv` file.***
123+
124+
## Conclusion
125+
126+
In this tutorial, we turned a Nano RP2040 Connect board into a data logger, without the use of any external components (such as an SD card).
127+
128+
This is an incredibly useful tool whenever you are working with data collection, and the script found in this tutorial can be easily be altered to fit your project.
129+
130+
While we in this tutorial only recorded values from an analog pin, there are many other things to do, such as:
131+
132+
- Record data from the onboard IMU.
133+
- Record data from an external sensor.
134+
- Record events that occurred along with a timestamp (for example, how many times a sensor value's threshold was met).

0 commit comments

Comments
 (0)