Arduino from the command line

Bored of the arduino interface ? We are too.

We will explain here how you can compile your sketch directly from a terminal, upload them to your arduino and open a serial monitor by listening to the serial port.

arduino interface

To compile your sketches in the commande line, you’ll use make, as you’re probably used to. To enable this feature you must install the arduino-mk package. On Ubuntu you can do a simple:

 sudo apt-get install arduino-mk

You will then need to create a Makefile in the folder containing your sketchbook. This is what it should contain:

 BOARD_TAG    = # Here goes your board type, e.g. : uno
ARDUINO_PORT = /dev/ttyACM0 # Change to your own tty interface

ARDUINO_LIBS = # The libs needed by your sketchbook, examples are : Wire Wire/utility Ethernet...

include /usr/share/arduino/ # This is where arduino-mk installed

For arduino 1.0 and above, you’ll want to give the .ino extension to your source file. You can now use the command make to compile your sketch, and make upload to send it to the board.

If your program is giving you feedback or waiting for instructions from the serial line, you might want to open a serial monitor. But we’re trying to get rid of the arduino interface, ain’t we ? So we’ll rather open a serial listener in the terminal. We’ve wrote a little python script that will correctly display the communication line with your arduino.

#! /usr/bin/python

#    Serial Reader for ARDUINO
#    usefull when tail -f /dev/ttyXXXX doesn't work
#    Change ttyACM0 for your own tty interface

import serial
import time

# The second argument is the baudrate, change according to the baudrate you gave to your Serial.begin command
ser = serial.Serial("/dev/ttyACM0", 115200)

# To avoid communication failure due to bad timings

while True:
    print ser.readline()

Comment by Mike ( :

Why do people ditching the Arduino IDE keep missing the cu command? A nice, powerful serial comm program that’s been a standard part of Unix since before Linus ever heard of Linux. If you’re on Linux, there’s a version available for that as well. Just do “cu -p /dev/ttyACM0 -s 115200″ and viola, you’re talking to your Arduino. No coding, no funky terminal emulator configuration, just a connection.

  • Ivan Kravets

    Thanks a lot for article. If you don’t want to play with make-files or installing system packages, I recommend you to look in to cross platform code builder named PlatformIO. It has support for AtmelAVR(Arduino-based) platform and most popular OS (Mac, Windows, Linux 32/64 + ARM arch). You don’t need to install any tools or tool chains. Just single commands flow:

    # install ATmelAVR platform with pre-built tools/+chains
    $ platformio install atmelavr

    # got to empty folder and initialise new project
    $ platformio init

    # build this project
    $ platformio run

    If you are interested in it please go to Quickstart section of documentation.

    • Spandan Samiran

      I want to compile and upload arduino code using an GUI I have made myself. So how to integrate the compile and upload functions of arduino onto my GUI? any idea?

      • Ivan Kravets

        Please take a look at how does PlatformIO integrate with Sublime Text editor.

        You need to call commands from PlatformIO CLI using platformio -f -c CALLER_ID ..., where CALLER_ID is the ID of your application. For example, to build project platformio -f -c mygui run and upload firmware platformio -f -c mygui run -- target upload.

        I welcome you to our Community where you will receive appropriate help and information.

        Regards Ivan,
        CTO at PlatformIO.Org

  • 9.8m/ss

    Thanks for this great little arduino CLI howto. Mike gave a cu command for the serial port. cu (the GNU version by Ian Lance Taylor) is a separate package on Debian / Ubuntu. Install it with,

    $ sudo aptitude install cu

    And then I can talk to my Arduino Uno with this command,

    $ cu -l /dev/ttyACM0 -s 9600

    Notice avrdude and cu have to lock each other out of the tty port, so only one can run at a time. Try this.

    $ make upload && cu -l /dev/ttyACM0 -s 9600

    Oh, by the way, cu will let go of the serial port when you’re done with it, when you type tilde-dot into it. The tilde has to be the first character on the line. See “man cu” and search “escape character”.


    • Maciej Miklas

      this is cool tip – thank you!