Arduino Serial Port Library

Arduino Serial Port Library 4,8/5 5448 votes

After I became proficient with Arduino I found myself trapped in its development environment (IDE). I needed to escape from the simplicity of the serial port and transform the platform into a usable engineering tool. I tried saving to SD cards, but decided adding more hardware was superfluous; I tried Bluetooth and WiFi, but again, barring specific Internet of Things applications, I found those to be roundabout ways of achieving what I wanted: a simple datalogging system. I ultimately arrived at Python's pySerial library, which reads directly form the serial port and was a complete solution to my predicament.

  1. Arduino Serial Port Library
  2. Arduino Serial Port Library In Michigan
  3. Arduino Serial Port Library In Windows 7
  4. Arduino Serial Port Library In Windows 10

Serial Port Options. HardwareSerial - Best performance. Always use this first, if available! Teensy and Teensy have one HardwareSerial port which is available (not used for uploading or the Arduino Serial Monitor). Arduino Mega has 3 extra HardwareSerial ports. Arduino Uno has none. AltSoftSerial - Can simultaneously transmit and receive. This video is an in-depth tutorial on how you can use Arduino with your C software via Serial Port library. Please like, comment, and share this video if you liked this video or learnt something.

Using pySerial is much easier than one might expect, as the most fundamental implementation consists of only three lines of code:

These three simple lines read a single row of data from the serial port. In the case of Raspberry Pi, the serial port (on my Arduino) is located at '/dev/ttyACM0'. You may also find yours there, or at an integer increment (ttyACM1, ttyACM2, etc.), or perhaps a different address completely. Check your Arduino IDE serial port for the exact location. After successfully reading a line from your Arduino, verify that it is in the desired format. I chose to print and read a single line, but you may prefer comma separated or similar formats. The code above isn't particularly interesting, but it verifies that pySerial is working and that you are parsing data correctly from your serial port. Once the method above is understood, we can advance onto loops and recording data in real-time.

NOTE: I will be using a DHT11 temperature sensor to produce data on the Arduino end. Since this is a tutorial on reading data from the serial port using Python, not Arduino, I recommend visiting a DHT11 tutorial to learn how to print temperature data from the sensor to the serial port (see here, or here).

I will be using a while loop and keyboard interrupt (CTL-C) to loop and halt the datalogging. The data from the serial port also needs to be converted from unicode to float (or another datatype) so that the data can be processed in Python. In my case, I am using 'utf-8' to decode, which is Arduino's default encoding and the most commonly used character encoding format. You may also notice the 'ser.flushInput()' command - this tells the serial port to clear the queue so that data doesn't overlap and create erroneous data points. Sometimes the conversion via float() can create errors, but this is due to overprinting from the Arduino's end. If you receive such an error, restart the python script and try again.

Now we have a working real-time data printer in Python. Decidedly, this isn't particularly interesting because we aren't saving or plotting the data, so we'll cover how to do both of those next.

In the code below I have implemented a way to save the serial data in real-time to a .csv file.

Now we have a working datalogger! This is as simple as it gets, and it's remarkably powerful. The three lines that start as: ' withopen('test_data.csv','a') as f: ' look for a file called 'test_data.csv' and create it if it doesn't exist. The 'a' in parentheses tells Python to append the serial port data and ensure that no data is erased in the existing file. This is a grand result because it not only takes care of saving the data to the .csv file, but it creates one for us and prevents overwriting and (most times) corruption. How considerate!

To take things a bit further, I decided to aggrandize the content here and include a real-time plot. I find real-time plotting a useful tool when acquiring data of any kind. It is much easier to find faults in visualizations than it is to find them in streaming values.

Arduino Serial Port Library

NOTES: while I was using Raspberry Pi, I came across an issue between reading the serial port, saving to .csv, and updating the plots. I found that updating the plot occupied a lot of processing time, which resulted in slower reading of the serial port. Therefore, I advise anyone who is using the method below to assess whether you are reading all the bytes that are being outputted by the Arduino. I found that I was missing bytes or they were getting backed up in the queue in the buffer. Do some tests to verify the speed of your loop. This will prevent lost bytes and dropouts of data. I found that my loop took roughly half a second to complete, which means that my serial port should not be outputting more than 2 points per second. I actually used 0.8 seconds as the time between data records and it appeared to catch all data points. The slow loop is a result of the plotting, so once you comment out all of the plot code, you will get a much higher data rate and .csv save rate (just as above).

Plot produced by matplotlib in Python showing temperature data read from the serial port. Merlin season 6 full episodes. During this plot, the sensor was exposed to a heat source, which can be seen here as an increase from 31 to 35 degrees C.

Arduino Serial Port Library

This tutorial was created to demonstrate that the Arduino is capable of acting as an independent data logger, separate from wireless methods and SD cards. I found Python's pySerial method a while ago, and I wanted to share its capabilities with makers and engineers that may be having the same issues that I was encountering. Printing data to Arduino's serial port and then reading it through Python gives the user the freedom to investigate the data further, and take advantage of the advanced processing tools of a computer, rather than a micro controller. This method also allows the user to bridge the gap between live data and laboratory measurements. With real-time datalogging via the serial port, one can mimic the laboratory setup of acquisition, analysis, and live observation. Often, with Arduino the user is trapped in the serial port, or is relegated to communication via protocols, which can take time and energy. However, importing the data into Python frees the user of middle-men and allows the data to be processed in any way preferred. I use pySerial often, whether for recording temperature data using thermocouples, or high-frequency hall sensor measurements to monitor moving parts. This method can be used to take pressure measurements in the laboratory, or even record calibration data to improve your instrumentation accuracy; the possibilities are truly endless.

Arduino Serial Port Library In Michigan

ReferenceLanguage Libraries Comparison Changes

Arduino Serial Port Library In Windows 7

SoftwareSerial Library

The Arduino hardware has built-in support for serial communication on pins 0 and 1 (which also goes to the computer via the USB connection). The native serial support happens via a piece of hardware (built into the chip) called a UART. This hardware allows the Atmega chip to receive serial communication even while working on other tasks, as long as there room in the 64 byte serial buffer.

The SoftwareSerial library has been developed to allow serial communication on other digital pins of the Arduino, using software to replicate the functionality (hence the name 'SoftwareSerial'). It is possible to have multiple software serial ports with speeds up to 115200 bps. A parameter enables inverted signaling for devices which require that protocol.

The version of SoftwareSerial included in 1.0 and later is based on the NewSoftSerial library by Mikal Hart.

Limitations

The library has the following known limitations:

  • If using multiple software serial ports, only one can receive data at a time.
  • Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
  • Not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
  • On Arduino or Genuino 101 the current maximum RX speed is 57600bps
  • On Arduino or Genuino 101 RX doesn't work on Pin 13

If your project requires simultaneous data flows, see Paul Stoffregen's AltSoftSerial library. AltSoftSerial overcomes a number of other issues with the core SoftwareSerial, but has it's own limitations. Refer to the AltSoftSerial site for more information.

Examples

  • Software Serial Example: Use this Library.. because sometimes one serial port just isn't enough!
  • Two Port Receive: Work with multiple software serial ports.

Functions

  • SoftwareSerial()
  • available()
  • begin()
  • isListening()
  • overflow()
  • peek()
  • read()
  • print()
  • println()
  • listen()
  • write()

Corrections, suggestions, and new documentation should be posted to the Forum.

Arduino Serial Port Library In Windows 10

The text of the Arduino reference is licensed under aCreative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.