This article is about how to program an Arduino Nano board using a cheap programmer USBtinyISP. First of all, I bought this programmer on Amazon Japan for just 440 Yen, shipping cost included. It arrived from China in about 1 week. The programmer had a 10 pin connector included, in order to connect it to the Arduino Nano board ISP connector. It looks like this. The USB cable was not included, but I already had one. I also bought the Arduino Nano on Amazon Japan, for just 480 Yen (it was a fake model from China).
The first step is to create a simple program to be flashed on the Arduino Nano board, which has an Atmel ATmega328 microcontroller. The board can be programmed also using the Arduino IDE, since the microcontroller has a boot-loader software preinstalled on it. However, since I wanted to flash a HEX file created with Atmel Studio, I had to use a programmer. The simple program which I created has few lines of code, and continuously toggles Arduino LED 13 (which is microcontroller pin Port B5) ON/OFF. Since my purpose was also to understand how C language code is converted into Assembly code, in case of function calls, I created a function which waits for 100ms. This makes the code a little bit more complicated of what it could be.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <avr/io.h> #define F_CPU 16000000UL // 16 MHz #include <util/delay.h> uint8_t cicli_ON = 5; uint8_t cicli_OFF = 25; uint8_t aspetta_100ms(uint8_t n_cicli){ for (uint8_t i = 0; i<n_cicli; i++){ _delay_ms(100); } return 0; } int main(void) { DDRB = 0xFF; // all output pins /* Replace with your application code */ while (1) { PORTB = 1 << 5; aspetta_100ms(cicli_ON); PORTB = 0; aspetta_100ms(cicli_OFF); } } |
DDRB register is set to 255 (hex: 0xFF) in order to set all pins of port B as outputs. Then, inside the while cycle, pin B5 is continuously turned ON and OFF, by setting PORTB to 0x20 (1<<5) and 0.
The program is compiled by clicking on Build -> Build Solution. As you can see from the screenshot, the size of the program is very small (only 218 bytes). The binary file needs to be transferred on the microcontroller ROM memory, using the programmer. This job can be done by avrdude (this is the name of the programmer software). It is included in the WinAVR package, which can be downloaded here.
In order to program the Arduino Nano microcontroller, the programmer must be connected to the 6-pin connector on the Arduino board (ISP connector). Then, open the command prompt, and type the following commands. For more info, have a look at this detailed guide.
avrdude -c usbtiny -p m328p -U flash:w:Nano_HelloWorld.hex
In case everything was fine, you should see something like the screenshot below. Your program is now on the Arduino microcontroller flash memory, and it is running.
Just some details about the command:
- "-c usbtiny" tells to the programming software that the programmer type is USBtinyISP.
- "-p m328p" means that the microcontroller is an Atmel ATmega 328p. The last part of the command,
- "-U flash:w:Nano_HelloWorld.hex", shows that the HEX file (Intel HEX format) is called "Nano_HelloWorld.hex", that it must be written ("w") on the flash memory of the microcontroller.
The source code (Atmel Studio project) is available here: nano_helloworld. Below, a video shows how the board behaves after programming. The LED turnsĀ ON for 500ms (5 cycles x 100ms delay) and OFF for 2500ms (25 cycles x 100ms).
Hello i got some quest's.
You using Linux?
I search how to compile and load Arduino scetches with the Arduino IDE not using the aduino Bootloader.
Means mine programms are directley going to run after power up the Arduino Nano.
Yes, I am using Linux on Raspberry Pi. On my PC, instead, I am using Windows 10. Arduino IDE on Windows is better than the one on Linux, so I suggest you to use the one for Windows.
I am using a usbtiny as the programmer and the target is an Arduino Nano. I can upload sketches with the Arduino IDE. But when I use avrdude, I consistently get the signature error. -F doesn't seem to do anything to over ride that check.