Command-line compilation and upload

What change would like to see? I would like to see command-line parameters for the ide to pass a .pde file for compilation and or compilation&upload, much like the two buttons, yet as simple as calling a compiler. This would, in my imagine, be something like:

Compiling only: arduino -c "file" [-mcu "328"]

Compile and Upload arduino -c "file" -u [-mcu "328"] [-p "com3"]

For the whole matter i would suggest the following paramters:

c "file" - Compile the file with the target settings of the IDE u - Upload to Port already set inside the IDE

Optional: -mcu - Override the Target device -p - Override the standard port

Any callbacks could stay the same as inside the IDE.

Why? This would enable people (like me) using external editors to simply build via commandline. Currently external editors are only supported with having the IDE opened, this woul remove that factor and would also make it easier for people writing their own IDEs to keep up with Arduino Syntax and Includes.

Would this cause any incompatibilities with previous versions? If so, how can these be mitigated?

I cant think of any incompatibilities.

Just mirrored from the google code's page.

I fully agree that command-line development should be as easy as the IDE, but with the choice of editor and without the need for a graphical environment in which to run the IDE.

I've made a relatively gigantic wrapper script for the Arduino Makefile to help accomplish this - it would have been simpler to just create a new script + Makefile from scratch, but the advantage of wrapping the original Makefile is that I don't need to maintain the script much when Arduino is updated.

The script itself is intended only for Linux (although other *nix environments should work, too, but command-line setup hasn't been made as easy for e.g. Mac OS X), and it's currently available here:

The way it works is you define some environmental variables (hopefully just once) to set the location of your Arduino installation, the device node to access the device (preferably something like /dev/arduino created via udev rules) and then a new program can be made and uploaded like so:

mkdir MyProgram cd MyProgram edit MyProgram.pde ardmake ardmake upload

(Where ardmake is what I renamed the script to in my local installation, the full name would be awkward to type every time.)

It strives to autodetect libraries from the #include-directives in a manner similar to Arduino, and does so recursively (i.e. libraries can use other libraries), and supports custom library paths. Settings for the microcontroller are all read from Arduino's boards.txt (an environmental variable is used to set the board name, e.g. "diecimila" or "sanguino" or whatever). I've also put in some support for easy flashing with AVRISP and AVRDragon, including fuses and bootloaders.

Of course, all of this is just tested on my system (Ubuntu Linux 9.04, avr-gcc and avrdude installed from Ubuntu packages)? I'm releasing the script in the hope that someone else will find it useful. But I haven't touched the Arduino IDE in six months now; everything I've needed to do has been done on the command-line via the script.

Arkku, that is a very professional job you did there. Compliments on your work.

Thanks, and also thanks for pointing out the typo in the instructions, I've uploaded a corrected version to the same place. =)

Couple things. First off this script looks very promising for cli development. One issue is that there is no "reboot" so you either have to press the button manually or, you get: avrdude: ser_recv(): programmer is not responding avrdude: stk500_recv(): programmer is not responding

anywho, I have gotten a .hex successfully uploaded, yet it isn't working on the arduino?

There is a paste of my program, and upload log. Any help would be much appreciated.

I fixed the upload error by dropping arduino 0017 into my home dir '/home/fsk141/arduino' I forgot that it needs write permission :(. Anywho, the only thing not working is the reset, and that should be fairly straightforward.

perl -MDevice::SerialPort -e 'Device::SerialPort->new("/dev/ttyUSB0")->pulse_dtr_on(1000)';

This is the only command that I can get to refresh the DTR :( Anywho, I've been trying to implement this in the script, but keep getting errors :(...

918 do_autoreset:
919 \t@echo Sending reset to prepare for upload...
920 #\tstty -F $(PORT) hupcl || true
921 \tperl -MDevice::SerialPort -e 'Device::SerialPort->new("/dev/ttyUSB0")->pulse_dtr_on(1000)';
922 \t@echo


/home/fsk141/.scripts/ardmake: line 921: syntax error near unexpected token `('
/home/fsk141/.scripts/ardmake: line 921: `\tperl -MDevice::SerialPort -e 'Device::SerialPort->new("/dev/ttyUSB0")->pulse_dtr_on(1000)';'


The stty-command works for me, and I'm hesitant to add a Perl-dependency to the script itself. However, if the Perl-command works for you, try the following (at the place where you inserted it):

\tperl -MDevice::SerialPort -e "Device::SerialPort->new(\"$(PORT)\")->pulse_dtr_on(1000);"

(Untested, I don't have the required Perl library installed.)

As for the complied code not running, I'm not sure what might be wrong. I never had any problems uploading code that compiles properly, and I just tried your LED.pde successfully.

Have you set the ARDUINO_BOARD environmental variable to match the board you are using? It defaults to "diecimila" (as it happens to be what I have), but newer boards should use "atmega328" or "mega". (Check out hardware/boards.txt in your Arduino dir for boards types.)

I assume the same program works for you from the Arduino IDE?

Let me know if there's something that needs fixing in the script; just because it's always worked for me doesn't mean it works for anyone else. The defaults are also rather selfishly tuned for my installation; but all the crucial stuff can be overridden with environmental variables.

Update: We seem to have solved the reset issue discussed above.

I've also fixed a few other bugs and rewritten most of the configuration to be somewhat more user-friendly (although the target audience of this script is still the experienced command-line user, as others will be using the IDE).

The new version is now available. I should probably make a web-page to document it better, but the helps are also rewritten so it might be easier to get into.

You should also try the latest version of avrdude (in SVN: It should perform the auto-reset automatically using the "arduino" programmer type. It would be great to know if that removes the need to set HUPCL on the serial port.

Hi mellis,

The latest avrdude also does do the trick, so I changed the script to try to detect when the avrdude being used supports the "arduino" programmer type and switch to using it with the "upload" and "download" targets. (The old behaviour with the stty + sleep reset, which seemed to fix the reset issues described above without resorting to Perl, is accessible by using "upload_autoreset" and "download_autoreset" instead.)

Users of the script, please let me know if these changes broke something, e.g. if the avrdude reset turns out to be less reliable than the stty + sleep reset. The new version of the script is at the old location.

Added an experimental "serial monitor" to the script; I think this completes the feature set of the command-line environment to include everything (and more) that's in the current graphical IDE (sans editor capabilities, of course, but vim takes care of that; after all, free choice of editor is probably the primary reason why people like me use the command-line environment).

Of course, on the downside the script is rather Linux-specific and in many places something of a huge kludge. =)

Awesome! Thank you so much for sharing this!

Compile and upload are working perfectly for me on Ubuntu 9.10. Serial isn't though (not really a big issue personally, I'm fine using screen for now.)

It's giving me the following error:

Starting serial monitor on port /dev/ttyUSB0 at 115200 bps.
Your input will be sent to the serial port. Press Ctrl-C to stop.

ardmake: 769: cannot create /dev/ttyUSB0: Invalid argument

The same happens when I run it as root.

When using the following it works fine:

stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts && screen /dev/ttyUSB0 115200

And again thanks for sharing, this was really what I needed at the moment!