How to read serial data from an Arduino in Linux with C: Part 1

EDIT: Part 2, Part 3, and Part 4 of the writeup.

Preface: I bought an Arduino a few months ago. Many of the projects I want to tackle require that the Arduino send data to a computer through the serial link. There is a simple way of doing this in Python and Processing, but since I’m neck deep learning Unix programming in C I figured I should stick with it. This 4 (or 5) part guide will be the tutorial I wish I had come across a month ago.

Part 1: The Sketch

Before tackling reading serial data from an Arduino, you have to get the Arduino to output serial data. This guide will be assuming the following sketch is running on the Arduino.


void setup()

{

Serial.begin(9600);

}

void loop()

{

Serial.print("Hello World\n");

delay(1000);

}

It’s pretty simple. Once per second the string “Hello World” will be sent to the PC via the serial connection. Upload the sketch and verify that it’s working through the Serial Monitor tool in the Arduino IDE.

I recommend doing two more sanity checks.

First is to make sure you know what serial port the Arduino is on. In Linux it’s going to be something like /dev/ttyACM0 or /dev/ttyUSB0. Mine is the former. If you can successfully load the sketch then the Arduino IDE knows the right serial port, and the name is on the bottom right of the sketch window.

Second is to make sure that you can see the serial data in something other than the Arduino serial monitor. There is a neat tool called GNU Screen that can do this easily. My Arduino is attached to serial port /dev/ttyACM0 and the sketch is set to run at 9600 baud, so at the terminal I can type:

>>screen /dev/ttyACM0 9600

and I can see something similar (but not exactly identical) to the Arduino serial monitor. To exit screen type “ctrl-a \” without the quotes.

In part 2 I’ll cover compiling and using (what I believe is) the only example online of talking to an Arduino in C. It’s a robust program, but is very “general use” and confusing for someone like me that’s still getting a grip on Unix programming. I’m going to cover it because if you can get it to compile and then work we can write a simpler program that does the one thing we need it to do.

This entry was posted in Hobbies, Programming. Bookmark the permalink.

One Response to How to read serial data from an Arduino in Linux with C: Part 1

  1. Fred Finster says:

    I use PuppyLinux.org Lucid Puppy 5.2.8 version. GNU Screen program URL Links
    http://www.gnu.org/software/screen/
    http://aperiodic.net/screen/start
    I used Puppy Package Manager to download the Screen_4.0.3 version from the Ubuntu repository, so check your distributions software repository for the screen program.
    These articles are helpful. Thank you, Chris H.
    Since my need was for /dev/ttyUSB0 serial for my UNO R3+ OSEPP arduino, I modified the serial guess section of file arduino.mk as follows:
    # default serial device to a poor guess (something that might be an arduino)
    SERIALDEVGUESS := 0
    ifndef SERIALDEV
    SERIALDEV := $(firstword $(wildcard \
    /dev/ttyUSB? /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbserial* /dev/tty.usbmodem*))
    SERIALDEVGUESS := 1
    endif

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s