Home > Projects > Metering – Instrument Projects > Simple Arduino data-collection

Simple Arduino data-collection

Summary of Simple Arduino data-collection


This article demonstrates a simple method to use an Arduino as a tethered A/D converter for direct data collection on a computer. The system involves an Arduino sending analog and time-stamp data pairs via serial communication, triggered by a Python script that specifies the number of points and output filename. The solution utilizes Pylab for plotting and requires a brief delay between initializing serial connections and requesting data to ensure reliability.

Parts used in the Arduino Data Collection Project:

  • Arduino Uno
  • Analog pin 0 (A0)
  • Light sensor
  • Computer (Macintosh, Linux, or Windows)
  • Python programming environment
  • Pylab library
  • Matplotlib package
  • NumPy package
  • SciPy package
  • PySerial library

At this year’s “Arduinos in the Physics Lab” workshop at the AAPT meeting, one of the participants asked for a simple way of using the Arduino as a tethered A/D converter for data collection direct to a computer. This is my quick & dirty demonstration solution.

Here’s the code for the Arduino. It waits for a single byte ‘N’ to arrive on the serial port, then once that byte arrives it sends out N data pairs formatted as tab-separated millis() and analogRead() values. The readings are separated by roughly 10 milliseconds. This version of the code only reports the values of analog pin 0 (A0), but it can be easily modified to return other (or more) ports.

Simple Arduino data-collection

For the computer end, I used Python: here’s the code. This was done on a Macintosh, with Pylab installed so I can use matplotlib to handle the plotting nicely. On Linux or Windows the port will be described differently, and if the program fails for you on the line ‘import pylab as pl’ then … well, install pylab on your system. It’s a great wrapper package for scipy, numpy, and matplotlib. The program expects two arguments: the number of points to collect and the filename where points should be saved.

Here’s a sample output plot, showing relatively meaningless data from a light sensor.

One glitch I found was that there needs to be a short delay between starting the serial communications to the Arduino and sending the request for N data points. I do not know whether this is a problem with the Arduino in general, or with the Arduino Uno I was using as a testbed, or with the pyserial library, or with the Macintosh implementation of pyserial…

 

For more detail: Simple Arduino data-collection

Quick Solutions to Questions related to Arduino Data Collection:

  • How does the Arduino trigger data transmission?
    The Arduino waits for a single byte 'N' to arrive on the serial port before sending N data pairs.
  • What format do the Arduino data values take?
    Data is sent as tab-separated millis() and analogRead() values separated by roughly 10 milliseconds.
  • Can this code monitor multiple analog ports?
    The current version only reports analog pin 0, but it can be easily modified to return other or more ports.
  • What arguments does the Python program require?
    The program expects two arguments: the number of points to collect and the filename where points should be saved.
  • Which Python libraries are necessary for plotting?
    You need Pylab installed, which acts as a wrapper package for scipy, numpy, and matplotlib.
  • Does the operating system affect the port description?
    Yes, on Linux or Windows the port will be described differently than on a Macintosh.
  • What happens if the import pylab line fails?
    If the program fails on that line, you must install pylab on your system.
  • Why is there a delay needed before sending requests?
    A short delay is required between starting serial communications and sending the request for data points to avoid glitches.
  • Is the source of the serial communication glitch known?
    The author does not know if the issue lies with the Arduino, the pyserial library, or the Macintosh implementation.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top