This tutorial explains how an Arduino Leonardo could be used as an Atmel ISP programmer so that we could burn hex files into Atmel microcontrollers natively, this way we could save up on having to buy a programmer.
As it was said in the previous tutorial in order to program our software into the microcontroller memory we need an external programmer, which price tend to be around a few tens of dollars (there are cheaper non official Atmel options).
The most economical option always is to use the resources that we already have available, specially if we have an Arduino hanging around, we could flash it with a sketch to make it an ISP programmer, this way we could download a program created in Atmel Studio into the microcontroller program memory space.
We can use any Arduino based board as an ISP programmer, in this example we will grab an Arduino Leonardo.
In order to program a microcontroller with an Arduino the steps indicated below will be taken:
1. Convert an Arduino Leonardo into an ISP programmer.
The first thing is to bring up the Arduino IDE and get the sketch that turns it into an ISP programmer bunt into its memory.
Select the Arduino ISP example and open it up, in the case of having an Arduino Leonardo there is a line of code to change in the Arduino ISP project sample.
The line with the macro #define RESET located at the top of the file, should contain SS and that shall be replaced by 10 as the picture above depicts, it is worth mentioning that this change is not applicable to any other Arduino boards and they could use with the default Sketch file as it comes.
Once we have applied the change click on upload and that will convert our Arduino into a programmer for any 8-bit ISP based Atmel microcontrolllers.
2. Arduino Leonardo – Microncontroller connections.
Next would be to connect our target board to the Arduino to verify that could be used as a programmer, in this example we are going to program the Attiny841 which is integrated in the board we used in the 8 bit microcontroller tutorials.
With the purpose of connecting the Arduino to the programming pins of the microcontroller we will review the different lines. To comply with an ISP connection there are 4 signals/connections to terminate: RESET, SCK, Serial Data in and Serial Data out.
The information could be looked up in the ATtiny841 microcontroller datasheet at page 226, where there is a descriptions of mapping of the already mentioned signals:
Therefore, we will need to connect the Arduino to the SCK (PA4), MISO (PA5), MOSI (PA6) and RESET (PB3) ATtiny841 pins.
The Arduino has a dedicated set of pins that will match up to use it as a programmer, this information could be found in the links:
There is a useful comment in the Sketch file:
// This sketch turns the Arduino into a AVRISP // using the following arduino pins: // // pin name: not-mega: mega(1280 and 2560) // slave reset: 10: 53 // MOSI: 11: 51 // MISO: 12: 50 // SCK: 13: 52
- Arduino MOSI to be connected to the microcontroller Serial Data in signal pin.
- Arduino MISO to be connected to the microcontroller Serial Data out signal pin.
- Arduino SCK to be connected to the microcontroller Serial Clock signal pin.
- Arduino RESET configured by request (10) to the microcontroller RESET signal pin.
The connection diagram could be represented in more detail by the following graphical diagram:
In the diagram the different connections are performed:
- Red wire: 5V Arduino output to V+ ecTiny841 pin.
- Black wire: ground Arduino output to G- ecTiny841 pin.
- Blue wire: Arduino Leonardo pin 10 to B3 (RESET) ecTiny841 pin.
- White wire: SCK Arduino ISP connector to A4 (SCK/Serial Clock) pin.
- Green wire: MISO Arduino ISP connector to A5 (MISO/Serial Data out) pin.
- Yellow wire: MOSI Arduino ISP connector to A6 (MOSI/Serial Data in) pin.
The previous set up would result in a breadboard distribution similar to the following picture:
In order to connect the ISP Arduino pins to the ecTiny841 we will use the wires shown in the picture, they are terminated with a socket and a plug at each end respectively. To have a set of this type of prototyping cables could become handy if we intend to use boards like Arduino.
It is to notice how cumbersome this approach of connecting the Arduino to our target microcontroller is, hence if we intend to program microcontrollers on a regular fashion it could justify the purchase of a programmer ready to connect directly to the programmer connector of our microcontroller.
From this point onwards, we should have an Arduino configured as an ISP programmer and already connected to the microcontroller, next we would need to take care of our development machine and look at the software required to download the program into the microcontroller.
3. Programming Software.
In order to get our microcontroller burnt with our own code in the computer that will be hosting our IDE we need to use an application called AVRDUDE, we will find it in the Arduino folder were its IDE is and we could invoke it though command line, however for this example we will install it with a graphical user interface to make things very easy.
This interface is fairly self explanatory, it will let us read and write the microcontroller non volatile memories including the fuses.
Attiny841 is one of the latest models within the Atmel ATtiny family, by the time of writing this tutorial, and unfortunately it has yet to be integrated to AVRDUDESS, if we browse the list by clicking on “Select an MCU..” and it is not listed we will have to explicitly add it by editing a configuration file. If it wasn’t there, only in that case we will need to follow the instructions below.
We will have to open the folder where the application was installed and edit the file avrdude.conf with our favourite text editor.
In this link there the lines we will need to edit the avrdude.conf file with to add the Attiny841 in AVRDUDESS, in particular this is the text required that we will need to copy and paste into the avrdude.conf.
Let’s open the drop down microcontroller selection menu and if everything has gone well we should see our ATtiny841 listed.
Ok, everything should be ready to program our ATtiny841 via the Arduino Leonardo to utilize it as an ISP programmer.
4 .How to program the microcontroller with Arduino Leonardo + AVRDUDESS.
Let’s open AVRDUDESS and connect our Arduino Leonardo to the USB port of our PC and the programming pins of the target microcontroller.
In the drop down view we have to pick “Arduino” in the case of using a Leonardo and the serial COM port selected by our PC as result of connecting the Arduino to the host machine, the capture condenses these steps.
If everything has been configured and connected correctly by placing the Detect button at the top right corner, we should get a message back verifying that the microcontroller is connected to the Arduino.
In the previous tutorial we explained how to generate an hex file as result of building our project via the Atmel Studio and download it into our microcontroller, in this case we are not using an officially supported programmer from Atmel, therefore we cannot use the integrated tool and program it directly from Atmel Studio, instead the process should be done through AVRDUDESS.
Atmel stores the .hex file in the project folder that we created to develop our program.
The programming method would be as simple as selecting our .hex file by typing or selecting the location path in the Flash section and pressing the Go button.
If everything goes well the .hex file will reside in the microcontroller flash memory and at the bottom of the AVRDUDESS there is a log hopefully indicating so.
There is also a visual indication since the TX and RX Arduino LEDs will be blinking unevenly during the programming process, and once it has been downloaded the microcontroller will begin executing the new program instructions right away as shown in the following video.
These have been the components and necessary steps to program an Atmel 8 bit microcontroller with an Arduino board, depending on what Arduino we have there are different connections to make, always the signals MOSI, MISO, SCK and a digital output pin from the Arduino will have to match up with Serial Data in, Serial Data out, Serial Clock and RESET signals of our target microcontroller.