Program Arduino Sketch Files From Commandline

Follow Dec 06, 2014 · 5 mins read

Introduction

In this article we will look at how to program Arduino Sketch files from commandline

Arduino

Arduino is an open-source physical computing platform based on a simple i/o board and a development environment that implements the Processing/Wiring language.

Once Arduino board is connected to the USB port of Raspberry PI ,It will power on through the port. We can program and communicate with the Arduino throught the USB Port using Serial Communication Protocol.

If the Arduino is detected properly by Raspberry PI a file /dev/ttyUSBX or /dev/ttyACMX` will be created.

Following lines may be observed in the output of dmeg |tail command


 #dmeg |tail 
[21748.171290] usb 1-1.2: new full-speed USB device number 5 using dwc_otg
[21748.285641] usb 1-1.2: New USB device found, idVendor=2341, idProduct=0042
[21748.285679] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=220
[21748.285695] usb 1-1.2: Manufacturer: Arduino (www.arduino.cc)
[21748.285708] usb 1-1.2: SerialNumber: 85334333931351907011
[21748.349390] cdc_acm 1-1.2:1.0: ttyACM0: USB ACM device
[21748.354136] usbcore: registered new interface driver cdc_acm
[21748.354174] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters

The Arduino detected can be also verified form ` lsusb` command output


pi@raspberrypi ~ $ lsusb
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter
**Bus 001 Device 005: ID 2341:0042 Arduino SA Mega 2560 R3 (CDC ACM)**


UDEV Rulse

if /dev/ttyUSBX or /dev/ttyACMX files are not found.It means that the Raspberry PI has not identified that Arduino is connected to it

Let us again refer the output of lsusb command

2341a is the vendor ID 0042 is the product ID

Go To the /etc/udev/rules.d/ directory

Create a file named 99-arduino.rules

<pre class=’brush:python”>

SUBSYSTEMS==”usb”, ATTRS{idProduct}==”2341a”, ATTRS{0042}==”YYYY”, SYMLINK+=”ttyACM%n”

or

SUBSYSTEMS==”usb”, ATTRS{2341a}==”XXXX”, ATTRS{0042}==”YYYY”, SYMLINK+=”ttyUSB%n” </pre>

Finally reload the udev rules

` udevadm control –reload_rules`

Setting the Permissions


pi@raspberrypi ~ $ ls -l /dev/ttyACM0 
crw-rw---T 1 root dialout 166, 0 Jan  1  1970 /dev/ttyACM0
pi@raspberrypi ~ $ 


We can see that only root users and users belonging to group dialout have permission to access the device files and hence communicate over the USB-Serial Interface.

To enable other users to communicate over the interface we need to add the user to the dialout gropup

usermod -a -G group-name username

Also check the /var/lock or /run/lock directory for permissions which are created at boot time


pi@raspberrypi ~ $ ls -ld /run/lock
drwxrwxrwt 2 root root 40 Dec  6 15:17 /run/lock

This 777 permission hence no issues

If users other that root do not have permission to access the directory then we need to change the configuration

You can include the commands to run on boot

sudo chmod o+rwx /run/lock

or

sudo chmod o+rwx /var/lock

ARDUINO IDE

The arduino integrated development environment allows editing, compiling and uploading sketches (programs) for Arduino (and compatible) microcontroller boards.

Normally, running the arduino command starts the IDE, optionally loading any .ino files specified on the commandline.

Running the compilation and build from commandline is only available for Arduino version 1.5.x or higher.This version is not available in standard repositories.The command line options can be found at link https://github.com/arduino/Arduino/blob/ide-1.5.x/build/shared/manpage.adoc

Another alrenative is to install anduino-mk utility ` sudo apt-get install arduino-mk`

The arduino-mk will install general purpose Arduino make file at /usr/share/arduino/Arduino.mk

In this article we will look at this approach

Creating Makefile

First step is to create a makefile in the same directory as the sketch file


BOARD_TAG    = mega2560         # Here goes your board type, e.g. : uno
ARDUINO_PORT = /dev/ttyACM0 # Change to your own tty interface
AVR_TOOLS_DIR=/usr/share/arduino/tools

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

ISP_PROG	   = stk500v2
ISP_PORT = /dev/ttyACM0
AVRDUDE_ISP_BAUDRATE=115200


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

If you are not sure of board name to be used leave it blank initially and type the below command


make show_boards`

.....
mega         Arduino Mega (ATmega1280)
**mega2560     Arduino Mega 2560 or Mega ADK**
mini         Arduino Mini w/ ATmega168

....

In the present case the board was Arduino Mega hence the board name in the Makefile should be entered as mega2560

The ISP Program should contain the protocol used to commuicate with the Arduino Bootloader

Arduino MEGA (1280 and 2560) bootloader uses the stk500v2 protocol operating at baud rate of 115200 while Arduino Uno uses stk500v1 protocol.The boot loaders and baud rate are are board specific.The exact configuration for the board can be found by programming the Arduino from IDE with verbose upload output enabled from Preferences Menu.

Below is verbose output for Arduino-Mega board


/usr/share/arduino/hardware/tools/avrdude -C/usr/share/arduino/hardware/tools/avrdude.conf -v -v -v -v -patmega2560 -cstk500v2 -P/dev/ttyACM0 -b115200 -D -Uflash:w:/tmp/build6124755445833805312.tmp/test.cpp.hex:i

we can now use the command make to compile your sketch, and make upload to send it to the board. The makefile uses avrdude utility to upload the hex file to arduino.


sudo make
sudo make upload

If the arduino utility is waiting from user inputs it can be passed through the serial interface. A serial listner can be opened on the Raspberry pi for programming pruposes

The code for the same is found below


 #! /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
ser.setDTR(True);
time.sleep(1);
ser.setDTR(False)

while True:
    print ser.readline()


References

  • http://playground.arduino.cc/Linux/All
  • https://github.com/sudar/Arduino-Makefile
  • http://hardwarefun.com/tutorials/compiling-arduino-sketches-using-makefile
  • http://baldwisdom.com/bootloading/
Written by