kpower
Published © GPL3+

Capture data from Arduino to CSV using pySerial

Use pySerial to capture data from an Arduino system and write the result to a CSV file

BeginnerFull instructions provided1 hour2,266
Capture data from Arduino to CSV using pySerial

Things used in this project

Hardware components

Nano 33 BLE Sense
Arduino Nano 33 BLE Sense
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE
python3

Story

Read more

Schematics

Connection Diagram

Code

Logging Sketch

Arduino
Sketch required on Arduino Nano to generate data
/*
*Simple sketch to demonstrate sending data over Serial to a computer
*Computer recieves data and writes to a CSV file
*Computer uses pySerial to receive and transmit data
*This example uses interger variable, other data types are possible
*/
int counter = 0;
int incomingChar;

void setup(){
  Serial.begin(115200); // Start up Serial Port
  delay(700);// Wait for Serial to settle
  Serial.println("Simple Logging Sketch - send serial data to CSV");
}

void loop(){
  // Wait for computer to send character
  // Once character received, start sending data to computer
  if (Serial.available() > 0){
    incomingChar = Serial.read();
    
    // Send data to computer via Serial
    for (int i = 0; i < 10; i++){
      Serial.print(counter);
      Serial.print(",");
      Serial.print(counter + 1);
      Serial.print(",");
      Serial.println(counter + 2); // \n will end read by computer
      counter++;
      delay(3000);
    } 
  
  // Once data send is finished, send 'stop' to complete
  Serial.println("stop");
  Serial.flush();
  }
  
}

Python program with pySerial to read from Arduino Nano

Python
import serial
from datetime import datetime
import csv

#Open a csv file and set it up to receive comma delimited input
logging = open('logging.csv',mode='a')
writer = csv.writer(logging, delimiter=",", escapechar=' ', quoting=csv.QUOTE_NONE)

#Open a serial port that is connected to an Arduino (below is Linux, Windows and Mac would be "COM4" or similar)
#No timeout specified; program will wait until all serial data is received from Arduino
#Port description will vary according to operating system. Linux will be in the form /dev/ttyXXXX
#Windows and MAC will be COMX
ser = serial.Serial('/dev/ttyACM0')
ser.flushInput()

#Write out a single character encoded in utf-8; this is defalt encoding for Arduino serial comms
#This character tells the Arduino to start sending data
ser.write(bytes('x', 'utf-8'))


while True:
    #Read in data from Serial until \n (new line) received
    ser_bytes = ser.readline()
    print(ser_bytes)
    
    #Convert received bytes to text format
    decoded_bytes = (ser_bytes[0:len(ser_bytes)-2].decode("utf-8"))
    print(decoded_bytes)
    
    #Retreive current time
    c = datetime.now()
    current_time = c.strftime('%H:%M:%S')
    print(current_time)
    
    #If Arduino has sent a string "stop", exit loop
    if (decoded_bytes == "stop"):
         break
    
    #Write received data to CSV file
    writer.writerow([current_time,decoded_bytes])
            
# Close port and CSV file to exit
ser.close()
logging.close()
print("logging finished")

Logging CSV file

Python
Example of CSV file generated
10:32:34,0 ,1 ,2
10:32:37,1 ,2 ,3
10:32:40,2 ,3 ,4
10:32:43,3 ,4 ,5
10:32:46,4 ,5 ,6
10:32:49,5 ,6 ,7
10:32:52,6 ,7 ,8
10:32:55,7 ,8 ,9
10:32:58,8 ,9 ,10
10:33:01,9 ,10 ,11

Credits

kpower
19 projects • 6 followers
Qualified Electrical Engineer with experience in software and hardware development
Contact

Comments

Please log in or sign up to comment.