From XinCheJian
Jump to navigation Jump to search


Programming a Swarm Robot

To make a SwarmRobot do behave and react as you desire, it needs to be given instructions. The process of writing these instructions is called coding or programming. Writing the instructions usually done in a special development editor and written a specific programming 'language'. The SwarmRobot project is mainly using the Arduino development environment, which uses the 'C' language.

Once you have written you code, it has to be compiled and uploaded into the SwarmRobot microprocessor. Note that writing your code is often called programming AND that uploading the code is also called programming! But these are two very different activities!

The first coding (programming) activity involves writing the commands that the microprocessor will execute. This is done on a computer, then the code is compiled (converted to binary format that the mocroprocessor understands).

This binary code is then uploaded to the microprocessor through the the second process called programming or 'burning'. Uploading the code requires a hardware device that does the programming. In some configurations this programmer is built in to the system.

Note that most Arduino systems have a built in ability to self program, using a self loading ability through bootloader code in the microprocessor.

The AtTiny microprocessor used for the SwarmRobots require using an ISP programmer, as specified in the parts list.

To write your code and upload it to the SwarmRobot microprocessor, you need to install the Arduino environment and add additional files to configure it for using an AtTiny. These steps are described in the following sections.

Sample code for the InfraRed and follow/avoid shields is in this repository SwarmRobots github.

If you want want a copy of the source code, just click though in your browser, then copy the code to the Arduino environment.

However you are strongly encouraged to spend a little time to share your code back with everyone. You can quickly learn how to do this through these introductory links to using the github source repository:

Installing the Arduino environment


The Arduino site has excellent documentation on installing the Arduino development environment for many different operating systems.

There is also a trouble shooting guide and the excelent support in the forums.

Configuring Arduino environment for AtTiny

To program an AtTiny using the Arduino Extract the contents of the archive into the "hardware" folder, an additional AtTiny cores library is required.

The source code repository for the library used in the SwarmRobot project is located at: [1]

Please note the releases since version 15 released 31-07-2012 no longer supports the AtTiny2313 :(.

However the previous version does support the AtTiny2313 and is still available [Previous version AtTiny library]

Steps to configure Arduino environment for AtTiny 2313

1. Download Arduino Tiny library [version 14] You may get a message like "This file has been depricated. Do you wish to continue?", click yes to continue.

Then unzip the downloaded file and read the instructions in the file "readme.txt" to install the library.

Please note the instructions have not been updated after the step "Extract the contents of the archive into the "hardware" folder", but luckily, this is the last step you need to do!

After completing this step, make sure the Arduino environment is closed, then open it and check the Tools-Boards menu to see a long list of AtTiny microprocessors have been added.

Note next is from However please note that as mentioned, by default, the ATtiny’s run at 1 MHz (the setting used by the unmodified “ATtiny45″, etc. board menu items). You need to do an extra step "Configuring the ATtiny to run at 8 MHz (for SoftwareSerial support)" to configure the microcontroller to run at 8 MHz – necessary for use of the SoftwareSerial library. Once you have the microcontroller connected, select the appropriate item from the Boards menu (e.g. “ATtiny45 (8 MHz)”). Then, run the “Burn Bootloader” command from the Tools menu. This configures the fuse bits of the microcontroller so it runs at 8 MHz. Note that the fuse bits keep their value until you explicitly change them, so you’ll only need to do this step once for each microcontroller. (Note this doesn’t actually burn a bootloader onto the board; you’ll still need to upload new programs using an external programmer.)

AtTiny - Arduino IO Command Reference

The AtTiny cores do not support all Arduino IO commands. (Does anybody know which comands are not supported?)

The following {(IO hardware related)} Arduino commands should be supported:

SoftwareSerial (has been updated in Arduino 1.0)

Uploading your code to the AtTiny

Connecting your ISP programmer to the SwarmRobot

If your programmer supplies power to the board, then make sure the SwarmRobot power switch is OFF, or battery disconnected BEFORE connecting your programmer, as many programmers also supply power to the system. If your programmer does not supply power then you need to leave battery connected and turned on!

Make sure you use an ISP programmer with 6 pin connector to match the SwarmRobot.

ISP Programmer and six pin connector

Making an ISP programmer

A simple ISP programmer is included in the SwarmRobot part list, but you might prefer to make one. You can even make one using an AtTiny2313 as used in the SwarmRobot!

The famous [LadyAda has instruction on making an ISP programmer using an Attiny2313], but you will need some more parts.

You also still need at least one ISP programmer to program the programmer program! So you need a programmer or at least get your attiny supplied with the ISP programmer code already loaded! ... and then you need at least two AtTiny - on for the programmer and one for the SwarmRobot. But for groups, this might be an effective approach.

There are many more web sites documenting how to make other simple ISP programmers.

Using an ISP programmer

Open the sketch you wish to load into the SwarmRobot in the Arduino GUI development environment.

In the Tools - Board list, Select the AtTiny2313. If it does not appear in the list, please review the instructions above "Configuring Arduino environment for AtTiny"

Then whenever you wish to upload the code to the AtTiny, select the File menu, then "Upload using Programmer"

Sample ISP programmers: [USBASP ISP programmer] [LittleWire]

Using a USB ASP ISP programmer

If you use [USBASP ISP programmer] as we did, in Windows 7 you may need to install a driver for it. Use instructions [[here] to download a driver and install.

If you get an error like this:

avrdude: error: could not find USB device "USBasp" with vid=0x16c0 pid=0x5dc
.. and yellow triangle in Windows device manager

then you need the driver.

Additional information on the programmer we used:

  • nice long cable = fantastic!
  • Arduino GUI - menu tools, programmer, select USBasp
  • cable on target board connect red to LHS corner - required cable over the top of the connector!
  • cable is 6 pin - many ISP have more pins - SwarmRobot needs 6 pin connector!
  • make sure the jumper is on else you will get message like:
avrdude: error: programm enable: target doesn't answer. 1
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.

Using an Arduino as the ISP programmer

An Arduino system can also be used as an ISP programmer.

To use an Arduino as a programmer to program the microcontroller selected for our project, the ATtiny2313.

Upload this [sketch] to the Arduino (Uno, Dueminalove) from Arduino IDE.


(see attachment .zip for the folder)

  • Save file into a folder called ArduinoISP2
  • Open the project ArduinoISP2.pde in Arduino IDE
  • Select the Tools > Board = Arduino Uno (or Dueminalove)
  • Select the Tools > Serial port of your Arduino Uno (or Dueminalove)
  • Upload

Connecting the Arduino to the ATTINY2313

Insert the 10uF capacitor between Arduino RESET and GND pin (longer pin in RESET).

Connect the Arduino to the ATtiny2313:

Arduino   Attiny2313

13        19 PB7/SCK
12        18 PB6/MISO
11        17 PB5/MOSI
10        1 PA2/Reset
5v        20 VCC
GND       10 GND

Uploading a program to the ATtiny2313

  • In Arduino IDE, change the configuration to ATtiny2313.
    • Tools > Board: ATtiny2313, 8Mhz
    • Tools > Port: your serial port
    • Tools > Programmer: Arduino as ISP
  • Open the blink sample.
  • Change PIN from 13 to 0
  • Upload

>>>IMPORTANT First time programming ATtiny2313, clock speed and programming speed

Brand new AtTiny's have a 'factory default' internal clock speed of 1MHz (8MHz divided by 8 = 1MHz).

So to make sure all time dependant code is accurate, the default divide by 8 needs to be changed.

There are two ways to do this:

  • 1. In Arduino IDE, change the configuration to ATtiny2313.
    • Tools > Board: ATtiny2313, 8Mhz
    • Tools > Burn Bootloader
  • 2. Use your favourite Programmer software (eg AVR8 Burn O Mat) and a programmer (eg USBasp ISP) to change the fuse.
    • The USBasp ISP must have the jumper ON to change the fuse setting.

After the fuse has been change the jumper is no longer required on the USBasp ISP and the AtTiny can be programmed at full speed.

ATtiny2313 full pinout

Use this as a reference:

RST -        - VCC
 P0 -        - P16
 P1 -        - P15
 P2 -        - P14
 P3 -        - P13#
 P4 -        - P12#
 P5 -        - P11#
 P6 -        - P10
#P7 -        - P9
GND -        - P8

# = PWM pin

SwarmRobot sample code

Sample code for the InfraRed and follow/avoid shields is in this repository [SwarmRobots github].

This code has comments included in it that describe what the code is doing!

If you have never used any source control system before or are not familiar with programming, then the recommended way to get all the source and compiled binary files is to just click on the "ZIP" button at top left of the screen.

Then after the download completes, unzip the files. Use one of the several text files to get information of which directory and file is which!

If you want want a copy of the source code, just click though in your browser, then copy the code to the Arduino environment.

However you are strongly encourage to spend a little time to share your code back with everyone. You can quickly learn how to do this through these introductory links to using the github source repository:

All Xinchejian SwarmRobot AFRON wiki pages