Module Project Template.


This device has been designed as an example application of an expandable system of modules for building mobile robots following the standards set out in the discussion on the Society Of Robots forum.

These modules can be connected to each other using an i2c bus and Master modules like this one can also be connected via a UART to a wide variety of equipment. (eg, PC serial and USB ports.)

The communication between these modules is documented here:
The physical layout for PCBs is documented here:

This particular module acts as a Master node on the i2c bus and has one UART which can be connected to a PC serial port or USB port with the addition of a simple level shifter. With a little configuration and editing of the local_function in main.c the logic of the AVR ATmega8 I/O pins can be changed on the local module or any similar module connected to the i2c bus. All available I/O on the ATmega8 has been brought out to header pins making this a good platform for testing firmware for new module designs before thy have been built.

Who Can Build This Module?

Anybody is allowed to build this module under the terms of the GNU General Public License (GPL) detailed below.
This is *not* recommended as your first microcontroller project though. The additional complexity of an i2c networked microcontroller project puts it beyond the scope of novice builders.
If you are interested in building your first microcontroller project we recommend SORs $50 robot.

If on the other hand you have sucsessfuly built and programmed a microcontroller board in the past this module (and the other modules to come in the series) may be just what you are looking for. Welcome.

You will need to be able to read a circuit diagram, source and purchase the parts, either make your own PCB or use some alternative (eg. stripboard), compile and upload the firmware to the microcontroller.

If you have not done *all* of these tasks at some point in the past we recommend a simpler project, or at least wait until there a few finished module designs out there.

As this is a template module rather than a finished one you will also have to do a little C programing to do anything useful with it. The hard work of writing the i2c and UART code has been already done for you though.

Building and Installing.

1. Circuit.
The circuit diagram was created using the Freeware version of CadSoft's EAGLE Layout Editor.

The schematic can be found in the subdirectory "circuit".

circuit diagram

**Insert Parts List here**

There is an Eagle board file in the "circuit" directory for making your own PCB but there is no reason why it couldn't be created on stripboard as well.
I have successfully created these PCBs using the "Toner Transfer" method and the Eagle PCB is optimised for home PCB manufacture rather than by professional board houses.

2. Firmware.
The firmware for this project requires avr-gcc and avr-libc (a C-library for the AVR controller). Please read the instructions at for how to install the GNU toolchain (avr-gcc, assembler, linker etc.) and avr-libc.

Once you have the GNU toolchain for AVR microcontrollers installed, you can run "make" in the subdirectory "firmware". You may have to edit the Makefile to use your preferred downloader.

Each module on the network must have unique addresses for all interfaces. For this module that means editing "main.h" and giving LOCAL_ADDR and UART_ADDR1 unique hex addresses between 0x02 and 0xFF. These addresses must be even numbers.
eg. The following 2 lines in "main.h":
#define LOCAL_ADDR 0xA0

#define UART_ADDR1 0xA2

If working with a brand-new controller, you may have to set the fuse-bits to use the external crystal, enable watchdog timer, etc:
make fuse

It is good practice to make sure there are no old hex files left from previous builds:
make clean

Afterwards, you can compile and flash to the device:
make upload


Although this module could be built as a ATmega8 development board the firmware is written with linking it to a network of similar i2c devices in mind.
Each of these modules needs a unique i2c address *and* a unique UART address as discussed in the Firmware section above.
Modules can be connected by the 5 pin i2c and power bus connectors seen at the top left and bottom left of the PCB picture.
For initial testing one module on the bus should have a UART level shifter such as a MAX232 connected to the ATmega8's UART pins (header SV3). This level shifter should be connected to a PCs serial port running terminal emulation software (eg. hyperterminal). Packets can then be typed directly into the terminal emulation software to send commands to various modules.
Packet format:

~a<i2c address> [~b<UART address>] ~s ~0<data byte 0> ~1<data byte 1> ~2<data byte 2>
~a<i2c address> [~b<UART address>] ~s ~l<length of data string> ~d<data string>
~a<i2c address> ~r ~0<number of bytes to read>

<i2c address>: A one byte address of the i2c address of the packets destination.
<UART address>: (Optional) If the packet is destined for a UART connected to the node addressed by <i2c address> this should contain the one byte address of the UART.
<data byte 0>: One byte of data in decimal format. (0 to 255)
<length of data string>: Number of characters to send in <data string>. (Not yet fully tested.)
<data string>: Data in string format. Max of ??? characters. (Not yet fully tested. Does not currently allow space characters in data.)
<number of bytes to read>: Number of bytes to read from an i2c slave in decimal format. (1 to 32)

Packets addressed to the local module currently change the logic of the header pins SV7.
It is suggested connecting SV7 to SV24 (connected to LEDs) to give some visual output while experimenting with these modules.

Example Packets.

To understand the following examples refer to the following picture.
The red squares are 2 of the modules described in this doc.
Presume the packets are being typed into a PC connected to the UART labeled 008.
Microcontroller network diagram


~A002 Addresses the packet to node 002.
~B004 (Optional) If the packet arrives at a master node and ~B exists and is not equal to 000 then the master node (in this example 002) will consult a lookup table to see which UART (or other port) to send the packet out of. In this case the packet will be sent to the UART.
~S Signifies that this packet is being Sent (ie, this is an i2c write).
~L004 Signifies the length of the data contained in the packet. (In this case, 4 bytes long.)
~d Signifies the start of the packet content (or data).

Result: The packet "~A002~B004~S~L004~dData" arrives at the equipment connected to the UART labeled 004 in plain text.


~A002 Addresses the packet to node 002.
~S Signifies that this packet is being Sent (ie, this is an i2c write).
~0100 1st data byte sent has value "100"
~1101 2nd data byte sent has value "101"
~2102 3rd data byte sent has value "102"

Result: The packet "~A002~S~0100~1101~2102" arrives at the module with the i2c address 002. Some programing of the Firmware of this module must take place to do something useful with the bytes contained in data[0], data[1] and data[2]


~A002 Addresses the packet to node 002.
~R Signifies this packet is Reading data from node 002. (ie, an i2c read.)
~0003 Reads 3 bytes from node 001

Result: Three characters are requested from the module with the i2c address 002. Providing the programing of the Firmware of this module has allowed for it, the 3 bites appear on the UART of the equipment sending the packet. (In the example described, the 3 bytes appear on the PCs screen.)

~a2 ~s ~0 10 ~1 5 ~2 254

This packet is equivalent to "~A002~S~0010~1002~2254" .
The spaces are ignored. Lower case and upper case packets are identical. Numbers do not need leading "0"s.

Thanks for the Code Guys!

This module could not have been made without numerous open source projects involved in the AVR_GNU and AVR_LIBC projects.
Of particular help was Procyon AVRlib from where i took the AVR i2c routines.

About the Licence.

My work, as i have already covered in the previous section, is based partly on the work of others.
Any changes i have made or code i have written are licensed under the GNU General Public License (GPL). The others work, are licensed under the GNU General Public License (GPL).

A copy of the GPL is included in License.txt.

Duncan Law.
module_template.zip223.74 KB
UART_i2c.png18.56 KB
atmega8_test_circuit.png26.81 KB
atmega8_test_PCB.png26.76 KB