Programming Arduino from command line


I have an Arduino Duemilanove with an ATMega328P processor. I’m trying to compile part of the Wiring library from the command line (without using the Ardiuino IDE). Everything works fine, however, the AVR’s timing doesn’t seem to be correct.

I’m using the following base Makefile:

CC = avr-gcc
LD = $(CC)
OBJCOPY = avr-objcopy
AVRDUDE = avrdude
RM = rm

CFLAGS = -std=gnu99 -O3 -Wall -fno-exceptions -ffunction-sections -fdata-sections -I$(LIB_DIR)/bld -mmcu=$(CPU_TYPE) -DF_CPU=$(CPU_FREQ) -DARDUINO=$(ARDUINO) -c
LDFLAGS = -O3 -Wall -lm
OBJCOPYFLAGS = -O ihex -R .eeprom
AVRDUDEFLAGS = -C $(LIB_DIR)/resources/avrdude.conf -p $(CPU_TYPE) -c arduino -b 57600 -v -P $(PORT) -D -U flash:w:$(TARGET_HEX):i

OBJECTS = $(patsubst %.c,%.o,$(wildcard *.c))
LIBS = $(LIB_DIR)/libwiring.a

	$(LD) $(LDFLAGS) -o $@ $^


all: $(TARGET_HEX)

install: $(TARGET_HEX)


%.o: %.c
	$(CC) $(CFLAGS) $^

and the actual project makefile:

LIB_DIR = /home/h2co3/Arduino/libwiring
PORT = /dev/ttyUSB4

TARGET = blink
CPU_TYPE = atmega328p
CPU_FREQ = 16000000L


Depending on the compiler optimization flag (-Os, -O3, -O0 etc.), the timer is eithe too slow or too rapid; I experienced (with -Os and -O0) that it’s so fast that the arduino can’t respond to the programmer, and it hangs, and it requires tricky dis- and reconnect cycles to be accessible again. Also, when I want to blink a LED like this:

#include <arduino/wiring.h>

#define LED_PIN 13

int main()

	pinMode(LED_PIN, OUTPUT);
	while (1)
		digitalWrite(LED_PIN, LOW);
	return 0;

despite the equal delay() intervals, the LED turns off for approximately 0.5 second, then on for 3 seconds.

What’s happening?

All I can suggest is that you do a “Verify” in the IDE after turning on verbose build output in the IDE preferences. That will show you how the IDE does the build. You can then take those commands an make a makefile out of them.

Thanks, I've already done that... That's why I'm surprised experiencing this problem. It really should work correclty, shouldn't it.

The Arduino 1.0 IDE puts "#include <Arduino.h> at the top of every sketch. Previously it was “Wprogram.h” which was inherited from Wiring. Perhaps one of those would supply the declarations you need.

Thanks, where can I find that header?

Same place you found wiring.h.