Saturday, July 5, 2014


Wow, it's been a while since I've made a post... I've been playing around with the the Adafruit GPS for a while now as well as messing with different OLED's as well. I came across a board from @mikerankin that combined both into one with an Arduino Pro Mini. AVR programming has also been taking a lot of my spare time and so I thought this would be a great project to consolidate everything I've learned so my goal was to write native C code compiled with avr-gcc and upload it to the board with avrdude instead of using the Arduino IDE (though that works with this board and would be much easier; but where's the challenge there!). Here are a few pics from the work in progress...

These pictures show the main screen and the board itself. Upper left corner is the temperature reading, upper right is the number of satellites that the GPS receiver is connected to. Bottom shows the format of the time (12h or 24h format is supported). Bottom right is the date and in the middle is the time.

This picture shows the trip summary as it tracks distance much like an odometer of a car. I changed the distances from metric to US measurements, but forgot to change the labels accordingly (oops! I know what I'll be doing after I post this)
This next screen shows the altitude (upper left), number of satelliets (upper right), course (below number of satellites) and speed in miles per hour.
This is the back of the GPS Board and you can see the Atmega 328 microprocessor , FTDI chip, resistors, LEDs, etc. You can refer to Mike Rankin's page for schematics and other details.

These pictures show the status LED for the lipo battery (red is charging and green is charged).

Lastly, this is a side view of the board. You can see the GPS module that the OLED hovers above on.

There are many more features that I have not discussed (ie. resetting the trip meter, saving a lat/lon position, saving data, etc). Those are all included in the original sketch which was done by Karman and posted to his blog. However, that sketch didn't work out of the box for me and kept crashing. I've since modified it to toggle between metric and US measurements, fix the crashing bug, and a few other minor fixes with some graphics and font modifications.

Saturday, March 22, 2014

ATtiny85 and V-USB on a breadboard

Using an ATTiny85 chip, I programmed it to emulate a USB device and stuck it on a breadboard with a mini USB breakout board. It's not the nicest layout on a breadboard, but it gets the job done. Plugging the USB cable into the computer, it runs the code and a USB slave device shows up. I have a custom driver communicating with the chip over USB.

Thursday, March 6, 2014

Trinket with V-USB

A few months ago I stumbled upon Joonas Pihlajamaa's tutorial about AVR ATtiny USB Tutorial. His tutorial is very informative and got me interested in communicating with AVR chips via USB. In his tutorial, he used an ATtiny2313 chip on a breadboard with different components to demonstrate things. Since, I'm not an electrical engineer and don't know too much about putting all that together I didn't pursue his setup.

In came Adafruit's Trinket which is conveniently mounted on a PCB and has a USB interface with a ATtiny85 chip. I then set out to try to replicate his tutorial on this hardware. I didn't know much about avr-gcc so I got to learn about that in the process. In my prior post, I detailed out how to setup your environment, compile a simple program, and upload it to the trinket so refer to that if you are reading this first.

A lot of the magic happens in the V-USB library which does most of the heavy lifting for us. Since the ATtiny85 doesn't have a USB stack on that chip, we must use this library and software to emulate it. The configuration from Joonas' tutorial was a bit different as port's and pins differ between the ATtiny2313 and ATtiny85. 99% of the code is already listed in his tutorial so I am not going to re-invent the wheel here as you can refer to his tutorial for the code. Here is the hex file you will need to upload to the trinket. Using his usbtest binary, you can interface with your trinket over USB!

Sunday, February 23, 2014

avr-gcc adafruit trinket example

I've been interested in learning more about programming microcontrollers and saw that Adafruit has a low cost board (called trinket) that is very convenient since it can be programmed over USB. When I first got my hands on this, I was making sketches for it using the Arduino IDE, but wanted to dive in at a lower level and be able to write C code and reprogram the Attiny85 chip that is on board. There are many different ways to do this, but here is the way I went about doing it.

Install the following packages
sudo apt-get update
sudo apt-get install gcc-avr gdb-avr binutils-avr avr-libc avrdude

You should now have an environment that you can compile and link code for Atmel microprocessors. The last package (avrdude) is what we will use to program the chip. Here is an example of how to blink the LED on pin 1 of the trinket (the red LED).

// blink.c
#include <avr/io.h>
#include <util/delay.h>

// prototype
void wait_sec(int seconds);

int main (void)
   // direction register
   DDRB |= 0x03; // pin 0 and pin1 as output
      PORTB |= 0x02; // LED ON
      wait_sec(1); // sleep
      PORTB &= 0x01; // LED OFF
      wait_sec(1); // sleep
   return 0;

// sleeps the specified amount of seconds
void wait_sec(int seconds)
   int i;
   for(i=0; i<seconds; i++)

You can compile the code with avr-gcc. At the time of writing this post, I'm using avr-gcc version 4.7.2. For this contrived example, I will not create a makefile, but it's usually good practice to do so.

avr-gcc -I. -I. -g -mmcu=attiny85 -DF_CPU=8000000UL -Os -fpack-struct -fshort-enums -funsigned-bitfields -funsigned-char -Wall -Wstrict-prototypes -Wa,-ahlms=blink.lst -c blink.c -o blink.o
avr-gcc -Wl,-Map, -mmcu=attiny85 -lm  -o myproject.out blink.o 
avr-objcopy -j .text                    \
  -j .data                       \
  -O ihex myproject.out myproject.hex
avr-objcopy -j .eeprom                  \
  --change-section-lma .eeprom=0 \
  -O ihex myproject.out
avr-objcopy: --change-section-lma .eeprom=0x0000000000000000 never used

If things went smoothly, you should now have a file called myproject.hex. This is the binary we will program the chip with using avrdude. To program the trinket, press the reset button so the red LED blinks which signifies it's in bootloader mode. Execute the following to upload and program the chip.
sudo avrdude -c usbtiny -p attiny85 -U flash:w:myproject.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e930b
avrdude: reading input file "myproject.hex"
avrdude: input file myproject.hex auto detected as Intel Hex
avrdude: writing flash (116 bytes):

Writing | ############################                       | 55% 0.03savrdude: 6 retries during SPI command
Writing | ################################################## | 100% 0.07s

avrdude: 116 bytes of flash written
avrdude: verifying flash memory against myproject.hex:
avrdude: load data flash data from input file myproject.hex:
avrdude: input file myproject.hex auto detected as Intel Hex
avrdude: input file myproject.hex contains 116 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.01s

avrdude: verifying ...
avrdude: 116 bytes of flash verified

avrdude: safemode: Fuses OK

avrdude done.  Thank you.
Once the chip is programmed, you should see the blink code running.

Sunday, February 9, 2014

I2C Library for BBB and Pi

I've been getting numerous requests to share my I2C Library. There are a few other things I'm adding to my github repository before I publish it, but in the meantime, here is the I2C library. Using and/or downloading it means you do so at your own risk.

This is nothing fancy as I only did what I needed to for my projects. This library works on both Raspberry Pi and BeagleBone Black for my Adafruit 128x64 OLED module.

i2c.cpp and i2c.h

Tuesday, September 10, 2013

Beaglebone Black with 128x64 OLED

I compiled my I2C OLED library on my beaglebone black and with little effort, I was able to get it working. My BBB is running Ubuntu instead of Angstrom (instructions to get that flashed are in a previous post). This code was the same one running on the raspberry pi in my prior posts.

Steps taken were:
  1. Download code from github
  2. run make
  3. run the compiled executable
The I2C class was defaulted to use the address of the OLED so I didn't need to change anything. :)

Here are some images of displaying a BMP on the OLED.

Monday, September 2, 2013

Simple Xbee Tutorial

I also got a few XBee modules with USB XBee Adapters and I started to mess around with them. The reason I got the adapters is primarily two fold. First, the XBee pins are 2mm and not the standard 0.1" so they do not fit on the standard breadboard. Secondly, the USB adapter makes programming the XBee very simple. I am using X-CTU to do the programming, but you can also do it over command line using the terminal, but it's a little more complicated.

My first simple project was having  1 XBee (the coordinator in API mode) talking serial to an arduino. The second XBee (the router in AT mode) would be reading a state of a pin and reporting its value every second. It would send that information to the coordinator and the coordinator would send it to the arduino where it can be seen via the serial monitor. A contrived setup and test of the XBee, but yet it made me have to learn a few more things and research on how it works.

XBee #1 - the controller in API mode
XBee #2 - the router in AT mode

The XBee #2 (router) is hooked up to an arduino solely for power and is not using any other features on the arduino. The XBee's I/O pin has been configured (via X-CTU) to read the state of the pin every second. To change the state, I have a momentary push button and a LED wired in. Pushing the button will cause the pin to read HIGH and the LED to go on. Releasing the button will cause the pin to read LOW and the LED to go off. Here are a few more pictures to illustrate.

Understanding the data
I have the arduino (XBee #1 controller) programmed to write out the frame that the XBee #2 (router) sent to it. By analyzing the frame (of hex values) we can see the digital pin being toggled as well. The highlighted lines second to the last byte (0x10) shows that digital pin 4 read high. Why 0x10 you ask? That is because bytes 19 and 20 represent the digital pin data. byte 20 represents 8 digital pins with bit 0 corresponding to digital pin 0 and bit 7 to digital pin 7. So, 0x10 (16 decimal) is 10000 in binary which tells us that digital pin 4 is reading high.
State of digital pin changed