Build Arduino Sketches from CLI with Make


The Arduino IDE has a lot of nice features. It's so easy, it's really one of the main reasons why you would choose to buy an Arduino over other options. It can be annoying sometimes though. If you want to distance yourself from the IDE, but still like the Arduino plaform, it's really as easy as just making a really simple text file and running "make".

By getting a command line interface, you can also automate the build and upload process. You can detect ports, or upload to multiple Arduinos at once.


Ubuntu command:

sudo apt-get install avrdude make gcc-avr arduino-mk


Create a text file called "Makefile" in the same folder as your project

ARDUINO_DIR  = /usr/share/arduino

TARGET = projectName         # The same as your ino file name 
BOARD_TAG = uno # can also be mega2560 ARDUINO_PORT = /dev/ttyACM0 # be sure to change this to the port used by your board
#Can add Arduino libraries. Example:
#ARDUINO_LIBS = EEPROM ARDUINO_DIR = /usr/share/arduino AVR_TOOLS_PATH = /usr/bin include /usr/share/arduino/

Once you have that file, all you need to do is just say:

make upload

If you aren't using an Arduino Uno, you can check which boards are available with:

make show_boards

Advanced Usage

It might be helpful to just autodetect all of the Arduinos connected and upload the same image to all of them. Here is a nice simple way to do that with bash:


ARDUINO_DIR  = /usr/share/arduino

TARGET = tempTargetName
BOARD_TAG    = uno
ARDUINO_PORT = ${arduino}

ARDUINO_DIR  = /usr/share/arduino
AVR_TOOLS_PATH = /usr/bin

include /usr/share/arduino/

Then make a bash file. This will be executed instead of the make command.


#look for any Arduinos.
#The current ones were connected recently and within the same minute
for a in `ls -lt /dev/ttyACM* | awk 'BEGIN{first = 1} {if (first) {current = $9; first = 0} if ( $9 == current ) { print $10} else{exit} }' ` do echo "uploading to $a" make arduino=$a upload if [ $? -ne 0 ]; then echo "nonzero return status" exit fi done

Now, all you need to do to compile and upload to all recently connected arduinos is run: