implements Elegance {

// Elwyn Malethan's musings on software development, mountain biking and general navel–gazing...

Articles published in category Irrigarduino

Arduino Uno with pure C

One of my primary objectives once I got my head around how to write code for my Uno using the Arduino IDE was to make this more releveant to my aim of re-learning C. Arduino IDE provides a layer of abstraction and avoids some of the boilerplate. But, unfortunately it‘s not real, pure C. There are a number of comprehensive guides as to how to go about programming an Arduino using pure C, GNU Make and AVR tools. I don‘t know much about any of the tools being used so best to follow the links and read up on them.

That last link has an XCode template linked to it, which sounded appealing to me, initially. However the Makefile did not work for me with my Uno. In fact, none of the pages linked had Makefiles that would work for me. For a good couple of hours it was very confusing while I trialled — and mostly errored — my way around my Makefile, trying to figure out what values and configurations were right for the Uno.

Many of the tutorials advocated downloading and installing AVR programming tools such as avr-gcc, avrdude etc. This was not as straight forward on my MacBook Pro as I had hoped (i.e. there was no Homebrew package that contained them all). So I elected to use the AVR tools I found inside the Arduino IDE application.

Below is the Makefile that finally worked for me. Likely variables for other users are ARDUINO_HOME and PORT. The one value that had me stumped for ages was the BAUD value. When wrong, the RX LED on the Uno would light up briefly but the program would not be uploaded and the error message avrdude: stk500_recv(): programmer is not responding would be displayed. Many others more familiar with this sort of thing would have spotted this earlier, I‘m sure.

A working Makefile for Arduino Uno using AVR tools on Mac OS X.

This is for the Blink example.

ARDUINO_HOME=/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/
CC=$(ARDUINO_HOME)bin/avr-gcc
CFLAGS=-Wall -Os -DF_CPU=$(F_CPU) -mmcu=$(MCU)
MCU=atmega168
F_CPU=16000000UL

OBJCOPY=$(ARDUINO_HOME)bin/avr-objcopy
BIN_FORMAT=ihex

PORT=/dev/tty.usbmodemfd131
BAUD=115200
PROTOCOL=stk500v1
PART=$(MCU)
AVRDUDE=$(ARDUINO_HOME)bin/avrdude -C $(ARDUINO_HOME)etc/avrdude.conf -F -V 

RM=rm -f

.PHONY: all
all: blink.hex

blink.hex: blink.elf

blink.elf: blink.s

blink.s: blink.c

.PHONY: clean
clean:
    $(RM) blink.elf blink.hex blink.s

.PHONY: upload
upload: blink.hex
    $(AVRDUDE) -c $(PROTOCOL) -p $(PART) -P $(PORT) -b $(BAUD) -U flash:w:$<

%.elf: %.s ; $(CC$(CFLAGS) -s -o $$<

%.s: %.c ; $(CC$(CFLAGS) -S -o $$<

%.hex: %.elf ; $(OBJCOPY) -O $(BIN_FORMAT) -R .eeprom $$@

First published on Oct 22, 2011. Last updated on: Oct 22, 2011.

Irrigarduino: Learning electronics and re-learning C

This page and the series of pages linked from it are intended as a personal journal to track my progress in working on a pet project of mine. The project is to automate the irrigation of my garden using a home made moisture sensor, some basic electronics, an Arduino Uno board and some C.

I initially started this project, not only because I wanted to automate the irrigation of my garden, but also as a vehicle for re-learning C. I studied C in university over 10 years ago. My university final year project — a dairy herd management application — I even wrote in C++. So I was — at one time — familiar with the two languages.

However, since then I‘ve worked with comparatively benign, forgiving abstraction layers between me and the machine.

As it transpires, the learning curve of the necessary electronics is far greater for me than that of relearning C. This, despite the relative trivialness of the circuitry required for this project.

In this series of pages I‘ll be posting code snippets, circuit diagrams and any links I found useful. This will be mostly for my own record and use. If anybody else finds the information here of any use then that will not only be a surprise, but a pleasant one at that.

Source code

All the source code resulting from this project will be in a Github repository, here: https://github.com/elmomalmo/irrigarduino.

Resources

In preparation for this I have found a number of resources very useful. I‘ll be adding to this list as time goes on.

Pages

First published on Oct 22, 2011. Last updated on: Nov 13, 2011.