Monday, 29 July 2013

Weather Guy

The picture shows a little Weather Station based around an ATmega328. What makes this one interesting is that it gets its weather data from Yahoo! Weather rather than a bunch of sensors. What's interesting about that (to paraphrase Dr Johnson) is that it can be done at all on an 8-bit microcontroller.

The hardware components of this system are not worth drawing a circuit diagram for:
  • Basic ATmega328 with 16MHz crystal and 22pF balancing capacitors (you know the score)
  • An ENC28J60 ethernet module (e.g., from eBay)
  • An ST7735 TFT display with SD-card (e.g., from Adafruit)
  • Stripboard, connectors, etc.
  • Err, that's it!
The software is where the real action lies. This baby (a 600+ line sketch!) crams in the following libraries:
The restrictions of the AVR chip steered the implementation in interesting directions:
  • All string constants stored in flash, using either F() or PSTR().
  • Using the TFT screen for debugging rather than Serial (saves 128 bytes RAM).
  • Graphics downloaded from Yahoo! in advance, converted into bitmaps and stored on a cheap SD card.
  • Buffer-sharing between the XML parser and reading from the SD-card. (In particular, most SD-card libraries require their own 512-byte buffer for output, see this earlier post.)
  • Reclamation of some flash on the AVR using Optiboot.
  • Displaying wind-direction without using transcendental functions (i.e., sin, cos) used the nice trick of successive rotations. (This is apparently due to the legendary Marvin Minsky, in hakmem 149.)
There is almost no free space left on the chip: just under 200 bytes of RAM (which I guess is needed for the stack) and 512 bytes of flash!

Here's another picture showing forecast data:

Saturday, 27 July 2013


The heart of a cheap Arduino clone is an ATmega328 chip. Often sellers of this chip will assume you're going to use it for an Arduino and burn a bootloader onto it before shipping it to you. But what if you get one without a bootloader? What if you want to use a different bootloader, for example Optiboot?

Optiboot is great: it fits inside a single 512-byte sector of flash, freeing up a (sometimes) precious 1.5kB for your programs. It allows downloading at 115200bps, also handy for large sketches. In conjunction with a 100nF capacitor between RTS and RESET, it turns your homebrew clone into an Arduino Uno (from the point-of-view of the IDE anyway).

So (adjusting your avrdude settings accordingly), firstly read the fuse bits:

$ avrdude -q -q -P /dev/ttyUSB0 -b 19200 -c avrisp -p m328p -U hfuse:r:-:h -U efuse:r:-:h -U lfuse:r:-:h

Check this online calculator for the meaning of these bits. In this case, the value of the high bits (0xda) means that 2k is reserved for the bootloader section, we're only going to need 512 bytes:

$ avrdude -q -P /dev/ttyUSB0 -b 19200 -c avrisp -p m328p -U hfuse:w:0xde:m 

Next program the optiboot bootloader (the version shipped with the Arduino IDE is in hardware/arduino/bootloaders/optiboot):

$ avrdude -q -P /dev/ttyUSB0 -b 19200 -c avrisp -p m328p -U flash:w:optiboot_atmega328.hex -U lock:w:0x0f:m

Now when you power up your Arduino clone, you should see a triple-flash on the LED on pin 13 indicating that Optiboot is ready to receive your sketches.

Monday, 15 July 2013

Accessing SD Cards from Microcontrollers

This post reviews the available choices for reading or writing FAT-formatted SD cards from Arduino and Energia over SPI. All of the libraries mentioned here support only 8.3 file-names.

Arduino ships with a library called SD, which is a wrapper (by Adafruit) around the more-comprehensive SdFat library (by William Greiman). Features of this library are:
  • FAT16 and FAT32 support on both standard and high-capacity SD cards
  • File: create, read, write, delete and truncate
  • Directory: create, read, write and delete
  • Multiple volume/partition support
The same author also provides a smaller fat16lib with features:
  • FAT16 support on standard SD cards only
  • File: create, read and write
  • Root-directory support only
Another choice for Arduino is tinyFAT (by Henning Karlsen). Its features are:
  • FAT16 on SD cards
  • File: create, read, write, rename, delete (ASCII and Binary I/O)
  • Root-directory support only, only one file open at a time
Energia, on the other hand ships with a C++ wrapper around a lower-level C-library (by ChaN) called FatFs. This is aimed at embedded systems generally and has broadly similar functionality to SdFat but also supports:
  • Multiple underlying storage technologies (e.g., SD, ATA, USB and NAND)
  • File-system creation
  • Permissions and timestamps
Petit FatFs is a subset of FatFs for 8-bit microcontrollers with limited RAM. Its features are:
  • FAT16 and FAT32 support
  • File: open, read, streaming-read, seek
  • Directory: read
  • Single volume and single file
Petit FatFs also allows features which are not required to be conditionally-compiled out (e.g., FAT32 and directory support). It also allows other libraries' low-level SPI functions to be used, to reduce code-size.

Since FatFs and Petit FatFs were originally Arduino libraries, they can be used on both Energia and Arduino. The converse is not true of FatFs.

An important point which is not obvious is the RAM footprint required. With the sole exception of Petit FatFS, all of the libraries mentioned allocate a static 512-byte buffer --- this is the sector-size of an SD card.