From Makers Local 256
Jump to: navigation, search


To make the Cerealbot code work, you have to have some sort of AVR microcontroller. This can range anywhere from a full-fledge Arduino, to a somewhat cheaper Diavolino, to a bare breadboarded ATmega328.

I'm using a Diavolino wired to the RPi via GPIO headers. Again, there are good resources out there, no need to copy me exactly.

Wiring to RPi Headers

ATmega Pins
Raspberry Pi 2 Headers

There are two ways I'm interested in using to program the ATmega: UART and ISP. UART uses the RX/TX pins on the controller, is a little safer, and requires the Arduino bootloader. ISP Uses MISO/MOSI and writes the chip directly, no bootloader required. Either method is straightforward with a tool such as an ISP programmer or FTDI adapter, this page is for detailing methods to cut those out and instead use the RPi headers.

UART - bootloader

On the Pi's headers, there's a default serial Rx/Tx associated with /dev/ttyAMA0 and console boot. We're going to yank it away from the OS and use it to speak with and program the ATmega328.

I'm using a high-low logic level converter to protect the Pi. This is crucial; the arduino is capable of burning out the headers on the Pi without some sort of voltage divider. You can also just use a resistor, just make sure the arduino cannot push 5V back onto the Pi.

The following table uses the HARDWARE (aka "Board") GPIO mappings! You can reference this image, and use the numbers in the circles, not by the labels!

Arduino Pin ATmega328 Pin RPi GPIO
Rx (0) PD0 (2) UART0_TXD (8)
Tx (1) PD1 (3) UART0_RXD (10)
GND GND (8,22) GND (6)
RTS (RST) PC6 (1) GPIO (11)

Connect Vcc and GND pins from the Arduino to an external source, I don't suggest wiring them to the header.

Configure UART0

We need to kick Rasbian off of the header GPIO:

At the end of /etc/inittab, change

#Spawn a getty on Raspberry Pi Serial line
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100


#Spawn a getty on Raspberry Pi Serial line
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

In /boot/cmdline.txt, remove any of console=ttyAMA0,115200 or kgdboc=ttyAMA0,115200

Create /etc/udev/rules.d/80-gpio.rules, and add:

KERNEL=="ttyAMA0", SYMLINK+="ttyS0",GROUP="dialout",MODE:=0666

ISP - no bootloader

This works... I know it... But I need to go back and verify at some point. Email me if you need this.



I recommend using PlatformIO over a makefile.


An alternative that I'm testing to makefiles is PlatformIO. Suffice to say its a pretty promising system to help extend the Arduino sketch to as many MCUs as possibly with minimal effort. Installation on my desktop was straightforward, but there's a gotcha or two to setting it up on the RPi.

ArchlinuxArm requires:

  • ncurses5-compat-libs
  • libftdi-compat
  • libtinfo (6-7 also provides libtinfo.so.5 file)

Install - Raspbian - RPi 2

Check that python2.7 and pip are installed.

Make sure pip, setuptools, and virtualenv are updated.

sudo pip install --update pip
sudo pip install --update setuptools
sudo pip install --update virtualenv

Create and activate our pio_env space for platformio:

# Create virtual environment:
virtualenv ~/pio_venv
# Effectively set the venv as python's locus:
source ~/pio_venv/bin/activate
# When you're done with the venv, close with:

Run deactivate only when you're done working with platformio. Next we need to install platformio and scons:

pip install --egg SCons
pip install platformio

If you receive an error about '--egg' then re-do the pip install --update commands above to make sure the tools in the venv are up-to-date. Now we need to get the right build tools through platformio:

platformio platforms install atmelavr

If you're using a USB programmer, that should be it! Otherwise, see the section below to set things up for programming over the ATmega's Rx/Tx lines.

Configure for UART GPIO

If you've got a bootloader on the ATmega, wire it as discussed above, and then set things up for PlatformIO to be able to program the chip. To do this you must have transparent 'sudo' installed, which drops you into root without asking for a password. This is the default behaviour for Raspbian. I'd like to find some other way to do this privilege escalation.

Go get a version of avrdude-rpi:

cd ~
git clone https://github.com/ctag/avrdude-rpi.git

This was initially created by deanmao. I've only tweaked some things to my liking.

Then, use these new files to transparently add DTR bumping to avrdude:

cd ~/.platformio/packages/tool-avrdude
mv avrdude avrdude-original
cd ~/avrdude-rpi
cp avrdude-autoreset ~/.platformio/packages/tool-avrdude/avrdude
cp autoreset ~/.platformio/packages/tool-avrdude/autoreset

Now PlatformIO should be able to write a new program to the ATmega via the arduino bootloader!

Building - Uploading

Go to the microcontroller project directory in the cerealbox git. It should have a platformio.ini file in it, along with src/ and lib/

You may have to stop cerealbot-serial.service first, if it's running.

My platformio.ini

platform = atmelavr
framework = arduino
board = diecimilaatmega328
# targets = upload
upload_port = /dev/ttyAMA0

To build and upload:

cd ~/cerealbox/avr/printr_ctl
platformio run
platformio run --target upload


Since I'm making use of the Arduino Bootloader to flash programs to the ATmega without ICSP, we're going to stick with regular "sketches" for now, not real microcontroller firmware. I do intend to mess with that later though.

Compiling sketches without a graphical X server has apparently been an issue for some time with Arduino folk. Even though the official IDE touts CLI building, it fails without a X screen for me.

Install packages:

sudo apt-get install arduino-mk arduino-core avrdude avr-libc gcc-avr

Setup the GPIO reset workaround:

git clone https://github.com/deanmao/avrdude-rpi
cd avrdude-rpi
sudo mv /usr/bin/avrdude /usr/bin/avrdude-original
sudo ln -s /home/pi/avrdude-rpi/avrdude-autoreset /usr/bin/avrdude
sudo ln -s /home/pi/avrdude-rpi/autoreset /usr/bin/autoreset

That should do it! Pretty straightforward really.

Compiling Stuff

Go into a source folder with a .ino file:

cd ~/cerealbox/avr/src
make clean
sudo make raw_upload

Output from make raw_upload should be like:

pi@bns-daedalus ~/cerealbox/avr/src $ sudo make raw_upload                                                                                             
/usr/bin/avrdude -q -V -p atmega328p -C /etc/avrdude.conf -c arduino -b 57600 -P /dev/ttyAMA0  \
                -U flash:w:build-cli/printr_avr.hex:i
done with autoreset

avrdude-original: AVR device initialized and ready to accept instructions
avrdude-original: Device signature = 0x1e950f
avrdude-original: NOTE: FLASH memory has been specified, an erase cycle will be performed
                  To disable this feature, specify the -D option.
avrdude-original: erasing chip
avrdude-original: reading input file "build-cli/printr_avr.hex"
avrdude-original: writing flash (4902 bytes):
avrdude-original: 4902 bytes of flash written

avrdude-original: safemode: Fuses OK


Arduino168/328 Pinmap



SURE Electronics 3208




https://github.com/gauravmm/HT1632-for-Arduino <- Awesome

http://bailey.st/blog/2011/07/12/how-to-connect-sure-electronics-8x32-led-matrix-display-to-arduino/ <- Wiring

GPIO To Arduino