A tiny arduino board for the raspberry pi

I showed off at the Milton Keynes Raspberry Jam#8 a minimal arduino board that I had built that plugs onto the Raspberry Pi. It only has a handful of components and you can build it for only a few quid, and use the Raspberry Pi to program it.

But Why?

Well, the Raspberry Pi is a great little computer and really good for experimentation. But so is the Arduino. Why would you want two computers?

Actually, this is a common architecture in real embedded systems – you have one computer that runs the main applications and handles the top level decision making, and a smaller computer that handles some of the interfacing to sensors. Mobile phones, for example, have multiple computers inside them – often an app processor and a baseband processor – sometimes these are both inside the same package (multi core), sometimes separate chips. But, each tiny computer has it’s own purpose.

The Raspberry Pi and the Arduino do overlap quite a lot in features, but the RaspberryPi has a huge amount of software with it (evidence the 4GB memory card you need to run it), and the Arduino has tiny bits of software (evidence the 32K of memory it has).

By far the best example I have found to date of why you would want two computers, is Amy Mather’s “Game of life” in ref [1], where she uses a Raspberry Pi as the main application brain, and the Arduino to handle the real-time display updating. The Raspberry Pi sends a small binary message over the serial port to the Arduino, which processes this command independently of what else the Linux operating system is doing on the Raspberry Pi.

A lot of ground work has already been done to get the Arduino IDE and other tools running on the Raspberry Pi, which I will just refer to in this blog post. Running the Arduino IDE on a Raspberry Pi is great, because it’s a cheap little computer compared to a Laptop. The Arduino boards are great, and you can just plug one in via the USB port to your laptop or your Raspberry Pi, but there’s a lot of “unneeded stuff” that you can do without.

In this tutorial I walk through how to make a small Arduino like device for about £3, that plugs right on top of the Raspberry Pi. You can program it and communicate with it from the Raspberry Pi, and later just remove it and power it from an external battery and it will work on it’s own – great for building into other projects, which is where we started this journey from with our Star Ship Enterprise airfix model – but more of that another day!

Installing Everything

Starting from the Gertboard manual V2 ref [2] P41 ATMega Device, follow the install instructions linked to Gordon’s website.


This installs openjdk, the arduino ide and also a debian package of bits that Gordon has put together for the raspberry pi specific bits.

Wiring up your own minimal Arduino

 Purpose RPI ATMega328P
 SCLK    23  19
 MOSI    19  17
 MISO    21  18
 RESET   24  1
 0V      6   8,22
 3V3     1   7

I put a 100nF cap between 0V and 3V3 close to the ATmega for decoupling. You might chance it without that, and then you have just a chip, a bit of veroboard, a connector and some wires (and probably an LED and a resistor if you want it to do something useful).

I didn’t connect pins 20,21,22, but if you want to use the analog inputs, these need to be connected as per the arduino or gertboard schematics. I didn’t bother, I’ll add them later.

I used tri-pad veroboard, and I cut it through one of the tri-pads so that the RaspberryPi connector appeared right at the edge without any wastage.

You will see that I mounted the veroboard upside down, seems odd, but it works out well that way and makes it much easier to solder the Raspberry Pi connector on. I used a 28 pin turned DIL socket for the ATMega chip, and I used kynar wire (I have loads in stock) of different colours, but you could just use any wire that is to hand. Be careful when soldering to the Raspberry Pi connector as you don’t want to short the pins. You could of course just use a bit of breadboard and one of the various prototyping adaptors from Adafruit.

The red and black wires are the power supply, I have also run that to a small connector you see at the bottom left so I can later plug in a battery pack with 2 AAA cells to power it independently. The ATMega runs from 1.8 to 5.5V so we don’t even need a regulator.

The white wires are the GPIO’s to the SPI port, and the green wires are to the LEDs. You might start with just a single LED and add more later once it works.

ATMega fuses

The ATMega has what it calls programmable fuses – these can be set and cleared, I have a memory that there are only a limited number of times you can change them, but they set various things like the clock rate, how the reset occurs, what peripherals are enabled, and the startup timer. These are all detailed in the Atmel data sheet in ref [7] but I’ve worked out a sensible set of values for you. There are 3 fuses in banks of 8 bits to set.

It seems that once you have programmed the lfuse to use the external crystal, if that crystal is not there, the chip will not start properly. Thus, this sequence worked for me because I set the chip up on the gertboard first – if you are setting the chip up on your own veroboard and don’t fit a crystal, you may have to temporarily fit one (if the clock source fuse is already set to expect the crystal) to disable it, and then remove it.
The ATMega data sheet says the device comes with CLKDIV8 preprogrammed, so if this is a brand new chip you should be alright, but I can’t tell because I’ve programmed the fuses in my gertboard ones a few times now.

If using a gertboard first, make sure you fit J7 to the lower position to bring the RPI3V3 through to the ATMega chip, otherwise it will be running off of the GPIO’s and there is not enough current to program the fuses and it will fail.

Now at this point in Gordon’s instructions, you run avrsetup. If you are using it with a crystal, or on the GertBoard, all will be fine, but if you want to run it off your own veroboard without the crystal you need to change the lfuse from 0xE7 to 0×62. This also gives you an extra 2 pins for general purpose IO use.

sudo nano /usr/local/bin/avrsetup

change lfuse from 0xE7 to 0×62 for the RC oscillator

avrsetup – choose option 1, should program with a happy response.
Looks all OK – Happy ATmega programming!
At this point I took the chip out of my gertboard and plugged it into my veroboard and re-ran avrsetup with the 0×62 clock fuse to verify that I did indeed get a happy response.

Loading the Blink Sketch

From the desktop, choose Electronics/Arduino IDE

Note led=13 is an arduino pin name, not an ATMega pin name. Arduino pin 13 comes out on ATMega pin 19 which is your SCK line. Change this to led=0, which is PD0 coming out on pin 2 of the ATMega chip.

If you are using the RC osc, it runs slower than the IDE is configured for the gertboard, so I changed the delays from 1000 to about 100 to get a resonable flash. There are other settings of the clock register that can be used to adjust the timing rate, and the ATMega data sheet details how you can perform a calibration of the RC osc to increase accuracy, should you need it (you might need this for higher baud rates for example, but we had 9600 working fine between the Pi and the arduino with this setup).

Setting up the programming options in the Arduino IDE

TOOLS/board/gertboard with atmega328 (GPIO)
TOOLS/programmer/Raspberry pi GPIO

You should now have an LED flashing from pin 2 of the ATMega328P.

We’ve had the blink sketch, the fading LED sketch, the sound sketch, and also the serial read and serial write sketches working already – that’s all we really need for our Starship Enterprise chip, as we’ll be programing it and controlling it initially from the Raspberry Pi, and later on adding a bluetooth module to it so that we can remotely control it from an Android App. I’ll detail the rest of that project as we make progress on it.

Pin Cross Reference

I got all of this from the GertBoard schematic and the Arduino Uno schematic, but it’s handy to have this reference all together:

 Arduino     ATMega328P pin
 x XTAL/PB7  10
 x XTAL/PB6  9
 13 SCK/PB5  19
 12 MISO/PB4 18
 11 MOSI/PB3 17
 10 SS/PB2   16
 9 OC1/PB1   15
 8 ICP/PB0   14
 7 PD7       13
 6 PD6       12
 5 PD5       11
 4 PD4        6
 3 PD3        5
 2 PD2        4
 1 PD1/TXD    3
 0 PD0/RXD    2
 x PC6/RES    1
 AD5 PC5     28
 AD4 PC4     27
 AD3 PC3     26
 AD2 PC2     25
 AD1 PC1     24
 AD0 PC0     23

Future Work

While it’s neat to build it on the veroboard, I think I might use this proto plate device from AdaFruit in the future to build arduino/Pi experimentor boards. There is just enough space to fit the socket for the Arduino, all the necessary SPI pins come out on a connector that you can patch over to the ATMega chip, and the prototying area is just big enough to fit an XBee to later (which is something that I will be doing for another project). HumblePi and SliceOfPi are probably also well suited to using to build a small Arduino, and the benefit of all 3 of these boards is that you don’t have to do the fiddly wiring to the Raspberry Pi connector.

However, an Arduino/Pi plate on a bit of veroboard is something you can knock together at short notice late on a Sunday afternoon (which is what I needed and what I did in the end).


[1] Mather, A. “Conway’s game of life”, CPC Raspberry Jamboree, March 2013.
[2] Gertboard manual: http://www.farnell.com/datasheets/1683444.pdf
[3] Gertboard Schematic: back of v1: http://www.farnell.com/datasheets/1642649.pdf
[4] drogon.net instructions: http://projects.drogon.net/raspberry-pi/gertboard
[5] Arduino Schematic: http://arduino.cc/en/uploads/Main/arduino-uno-schematic.pdf
[6] ATMega328P on farnell (qty breakpoint 10=£1.71) http://uk.farnell.com/atmel/atmega328p-pu/mcu-8bit-avr-32k-flash-28pdip/dp/1715487
[7] ATMega328P datasheet: http://www.atmel.com/Images/doc8161.pdf

Related works

Please do also look very closely at this excellent work by shrimping.it (@shrimpingit), they have some very good tutorials and worksheets there for download.



This entry was posted in Older Stuff. Bookmark the permalink.

Comments are closed.