Sorry Windoze users. Unfortunately, this tutorial is for Linux (and cygwin?) platforms. It may also work for MAC, so give it a try.
While the Arduino integrated development environment is fine for starting out and making single program applications, it soon becomes cumbersome for multi-programming. The first step in breaking the bonds of the Arduino IDE is to build your applications using command line tools.
In this tutorial you'll learn how to build and upload a simple example application (Blink). The steps are applicable to all Arduino platforms, programmed wireless or not.
- INSTALL NECESSARY SOFTWARE:
Even though the applications are being built outside the Arduino IDE, it still needs to be installed on your system. This is because all the required library files are co-located with the IDE. The first step is to download the Arduino environment for your flavor of Linux (32 or 64 bit), if you don't already have it installed.
This tutorial assumes you have Arduino-0022. Some of the examples shown here may need editing to reflect the version you have installed.
You will also need to make sure the avrdude, gcc-avr, Java and Python packages are installed. Use your package manager.
- CREATE A PROJECT FOLDER
Start your terminal program - all these examples assume BASH as the terminal shell. You can verify your terminal shell by using the command:
user@Speedy-QuadCore ~ $ echo $SHELL
If it is BASH - you can find the version (not that it matters) by typing:
user@Speedy-QuadCore ~ $ /bin/bash --version
GNU bash, version 4.0.33(1)-release (i486-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
From your home directory, the next set of commands will create a copy of the Bink example into it's own project folder.
~ $ mkdir Blink
~ $ cp -t Blink arduino-0022/examples/1.Basics/Blink/*.*
~ $ cd Blink
- DOWNLOAD THE MAKEFILE
The attachments section at the end of this tutorial contains a link to the makefile used to build the application. Download, copy it to the 'Blink' directory you just created, then open it up in your favorite editor. Don't worry if you don't understand the makefile syntax. The tutorial will guide you through the necessary editing.
The file is named Arduino_Makefile_Template. It will need to be renamed 'makefile' after you edit it.
~ $ mv Arduino_Makefile_Template makefile
~ $ rename Arduino_Makefile_Template makefile
- CONFIGURE THE MAKEFILE
Open the makefile (assuming you haven't already opened it), and scroll down through the comments. A lot of the information on how to configure the makefile is listed in the comments, but the specifics for STRUIX are written here for your reference.
The first lines needing your attention are the TARGET and VERSION names
# Name of the program and source .pde file:
TARGET = SOURCE_FILE_NAME
VERSION = 0.1
Replace 'SOURCE_FILE_NAME' with the name of your Arduino application file. Don't add the '.pde' - just the main file name.
'VERSION' is for your reference and will be added to the name of the archive tarfile, should you choose to use the archiving option.
Next is to call out any special Arduino libraries your application uses. STRUIX makes use of the Wire and TWI libraries, so they are already added for you in this version of the makefile.
# Standard Arduino libraries it will import:
ARDLIBS = Wire twi
Next, call out any user defined librararies
# User-specified (in ~/sketchbook/libraries/) libraries:
Now the makefile needs the location of your Arduino installation - In this case for Arduino 0022. Your installation location may be different.
ARDUINO_DIR = $(HOME)/arduino-0022
In order to use the 'upload' option, avrdude will need to know the name of your serial port programmer. If you don't know already, plug the programmer (assuming you have a USB programmer) into the USB port, wait for the system to recongnize it, then issue this command:
user@Speedy-QuadCore ~ $ ls /dev/ttyU*
The device will likely show up as 'ttyUSB'-something. In any case put that device path after the 'PORT' specification in the makefile.
# Serial port used
PORT = /dev/ttyUSB0
The communication rate for Lilypad and STRUIX is already set in this version of the makefile.
# Speed: use 19200 for Diecimila, 57600 for Lilypad (STRUIX) 328
DOWNLOAD_RATE = 57600
Now tell the makefile where the avr compiler and uploading tools are located. You can find the path to the avr tools by using the following command from the command line. You may need to enter your super-user password.
user@Speedy-QuadCore ~ $
sudo find / -name avr-gcc
Edit the makefile with the proper path - in this case
# Where are tools like avr-gcc located on your system?
AVR_TOOLS_PATH = /usr/bin
For the STRUIX 328, the processor is - wait for it - the ATMega328p. In this instance, the makefile is already set up.
# MCU: Use atmega168 for a Diecemila, atmega328p for a Duo-whatever.
MCU = atmega328p
The CPU frequency for STRUIX is 8MHz and is also all ready to go.
# CPU Frequency
F_CPU = 8000000
The programmer type is pre-set to arduino, although other options are available in the makefile.
# Programmer to use
AVRDUDE_PROGRAMMER = arduino
Next is the method by which the CPU is reset - starting the bootloader - just prior to uploading the application program. The following inline Python script, seems to work well with both the Sparkfun FTDI programmer & XBee explorer (also FTDI) boards.
# Python version(s) needs python-serial :
# Python verion for wireless (XBEE) programming - specifically tried with the Sparkfun XBEE Explorer w/RTS jumper mod
RESET_DEVICE = python -c "import serial; import time; s = serial.Serial('/dev/ttyUSB0', 57600); s.setRTS(True); s.setRTS(False)"
After all that, we're done configuring the makefile.
- COMPILE THE PROGRAM
Issue the 'make' command at the prompt.
user@Speedy-QuadCore ~ $
If all goes well - and it should, you will see at the end, some statistics about the application just built.
text data bss dec hex filename
0 1334 0 1334 536 applet/Blink.hex
If you like, you can get rid of all the temporary files by issuing the following command
user@Speedy-QuadCore ~ $
- UPLOAD THE PROGRAM
The last step is to upload the program just compiled to the board. Make sure the programmer is plugged in to the CPU and issue the command:
user@Speedy-QuadCore ~ $
You should see some progress bars and messages indicating all went well. After that the Pin 13 LED should be blinking on your CPU.
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.02s
avrdude: Device signature = 0x1e950f
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "applet/Blink.hex"
avrdude: input file applet/Blink.hex auto detected as Intel Hex
avrdude: writing flash (1334 bytes):
Writing | ################################################## | 100% 0.91s
avrdude: 1334 bytes of flash written
avrdude: safemode: Fuses OK
avrdude done. Thank you.
That's it! Now you have a basic idea of how to build and upload Arduino applications without the IDE.