|
| 1 | +--- |
| 2 | +title: 'Data Logger' |
| 3 | +description: 'Learn how to store data on a .csv file using MicroPython' |
| 4 | +author: 'Karl Söderby' |
| 5 | +tags: [MicroPython, Data Storage, CSV] |
| 6 | +--- |
| 7 | + |
| 8 | +Data logging using MicroPython is a great feature, as we can use the board's file system to create files and store data in them. |
| 9 | + |
| 10 | +In this tutorial, we will create a `.csv` file, make some readings from an analog pin, and store the data in the file. The file can then be accessed via the Arduino Lab for MicroPython editor. |
| 11 | + |
| 12 | +***To learn more about the MicroPython file system, visit [this article]().*** |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +Before we start, let's check the requirements: |
| 17 | + |
| 18 | +### MicroPython Compatible Arduino Boards |
| 19 | + |
| 20 | +MicroPython is officially supported on several Arduino boards. Here’s a list of the compatible boards: |
| 21 | + |
| 22 | +- [Portenta C33](https://store.arduino.cc/products/portenta-c33) |
| 23 | +- [Arduino GIGA R1 WiFi](https://store.arduino.cc/products/arduino-giga-r1-wifi) |
| 24 | +- [Portenta H7](https://store.arduino.cc/products/portenta-h7) |
| 25 | +- [Portenta H7 Lite](https://store.arduino.cc/products/portenta-h7-lite) |
| 26 | +- [Portenta H7 Lite Connected](https://store.arduino.cc/products/portenta-h7-lite-connected) |
| 27 | +- [Opta](https://store.arduino.cc/products/opta) |
| 28 | +- [Opta Wifi](https://store.arduino.cc/products/opta-wifi) |
| 29 | +- [Opta RS485](https://store.arduino.cc/products/opta-rs485) |
| 30 | +- [Arduino Nano RP2040 Connect](https://store.arduino.cc/products/arduino-nano-rp2040-connect) |
| 31 | +- [Nicla Vision](https://store.arduino.cc/products/nicla-vision) |
| 32 | +- [Arduino Nano 33 BLE](https://store.arduino.cc/products/arduino-nano-33-ble) |
| 33 | +- [Arduino Nano 33 BLE Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-rev2) |
| 34 | +- [Arduino Nano 33 BLE Sense](https://store.arduino.cc/products/arduino-nano-33-ble-sense) |
| 35 | +- [Arduino Nano 33 BLE Sense Rev2](https://store.arduino.cc/products/arduino-nano-33-ble-sense-rev2) |
| 36 | +- [Arduino Nano ESP32](https://store.arduino.cc/products/arduino-nano-esp32) |
| 37 | + |
| 38 | +### Software Requirements |
| 39 | + |
| 40 | +- [Arduino Lab for Micropython](https://labs.arduino.cc/en/labs/micropython) - Arduino Lab for MicroPython is an editor where we can create and run MicroPython scripts on our Arduino board. |
| 41 | + |
| 42 | +***Note that the editor is also available online, at [Arduino Cloud - Arduino Labs for MicroPython](https://lab-micropython.arduino.cc/)*** |
| 43 | + |
| 44 | +## Board and Editor Setup |
| 45 | + |
| 46 | +1. Open the [Arduino Lab for MicroPython]() application. |
| 47 | +2. Plug the Arduino board into the computer using a USB cable. |
| 48 | + ![Connect board to computer.]() |
| 49 | +3. Press the connection button on the top left corner of the window. |
| 50 | + ![Connect the editor to the board.]() |
| 51 | +4. The connected Arduino board should appear, and we can click it: |
| 52 | + ![Select board.]() |
| 53 | + |
| 54 | +***Need help installing MicroPython on your board? Visit the [MicroPython installation guide]().*** |
| 55 | + |
| 56 | +## Data Logger Example |
| 57 | + |
| 58 | +Copy and paste the script below, and run it: |
| 59 | + |
| 60 | +```python |
| 61 | +import machine |
| 62 | +from machine import Pin |
| 63 | +import time |
| 64 | + |
| 65 | +adc_pin = machine.Pin("A1") |
| 66 | +adc = machine.ADC(adc_pin) |
| 67 | +led = Pin("D13", Pin.OUT) |
| 68 | +readings = 0 |
| 69 | + |
| 70 | +# create a file named "data.csv" |
| 71 | +file=open("data.csv","w") |
| 72 | +file.write("data"+"\n") |
| 73 | + |
| 74 | +while True: |
| 75 | + |
| 76 | + led.value(1) |
| 77 | + reading = adc.read_u16() |
| 78 | + print("ADC: ",reading) |
| 79 | + |
| 80 | + time.sleep_ms(100) |
| 81 | + |
| 82 | + # convert and write the reading from the analog pin |
| 83 | + file.write(str(reading)+"\n") |
| 84 | + |
| 85 | + led.value(0) |
| 86 | + time.sleep_ms(100) |
| 87 | + readings += 1 |
| 88 | + |
| 89 | + # if 25 readings are done, finish the program |
| 90 | + if readings >= 25: |
| 91 | + file.close() |
| 92 | + break |
| 93 | +``` |
| 94 | + |
| 95 | +### How it Works |
| 96 | + |
| 97 | +The data logger example works as follows: |
| 98 | + |
| 99 | +- First, we create a `.csv` file, and open it using `file.open()`. |
| 100 | +- Then, we read the value of an analog pin, and log it, using the `file.write()` function. |
| 101 | +- We repeat 25 times and then finish script by closing the file with `file.close()`. |
| 102 | +- Each time a reading is recorded, the built-in LED flashes. |
| 103 | + |
| 104 | +At the end of the script, a file will appear in your file system called `data.csv`. This is now accessible in the editor, where you can open it, and move it over to your computer. |
| 105 | + |
| 106 | +### Access the File |
| 107 | + |
| 108 | +To access the data, click the "Files" button, and navigate to the `data.csv` file. Open it, and you will see the data inside it. You can now move it to the computer by using the arrows, or simply copy the whole content of the file. |
| 109 | + |
| 110 | + |
| 111 | + |
0 commit comments