|
Proof of concept : My normal arduino running a sketch that blinks an LED once a second. A separate arduino on a breadboard, programmed to do something different: in this case, run a sketch that blinks an LED twice a second. Both green wires are connected to pin 13 on each arduino. |
|
The blink sketch is nearly the first in the examples tutorial, because it is so simple.
If the blink sketch doesn't work, then the wiring is wrong !
To save confusion later, I saved the tutorial blink sketch as two sketches, blink_1000 and blink_500 with obviously the
delay set to 500 in one of them so it blinks twice a second.
/* Blink. Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
int led = 13;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
|
Now we've seen the objective, lets start at the beginning.
The arduino ATMEGA328P chip comes as a kit, with two capacitors and a crystal. Many UK suppliers have
them, at just over £3. (in May 2014).
Start at the beginning by buying the chip and wiring it to a breadboard.
I recommend beginners buy 2 of them, and keeping one spare. It's then easy to replace the chip on your arduino with
an identical chip, if you accidentally overwrite and can't rewrite your bootloader.
|
Connect them to a breadboard as shown here. The dimple in the IC is on the left, so pin 1 is bottom left in this picture. |
|
Next, connect the target chip to the arduino; 4 cables and 1 resistor (10K).
Do NOT get confused between arduino pin numbers, and the target chip pin numbers.
Arduino pin 10 -> 10K resistor -> target chip pin 1
Arduino pin 11 -> target chip pin 17
Arduino pin 12 -> target chip pin 18
Arduino pin 13 -> target chip pin 19
|
Connect the target chip (on the breadboard) to the arduino. |
|
If, like me, you have just started with uploading sketches to an arduino, then this is the new bit that requires your concentration.
This process will overwrite the bootloader on the target chip. This should not be a problem; the chip is intended for use on a standalone system, so no bootloader is required, and the sketch that it runs can be rewritten by rerunning these steps.
This should reprogram the target chip on the breadboard.
Step 1 : modify file boards.txt. This is critical to get right !
Step 2 : modify file preferences.txt
Step 3 : Send sketch ArduinoISP to your arduino
Step 4 : Change your IDE options and send sketch blink_500 to the arduinoISP sketch on the arduino, which will write it to the target chip on the breadboard.
Step 5 : It's complete. Tidy up.
You now have an arduino chip on the breadboard which, given a 5V power supply, will always run that blink_500 sketch. You can of course reprogram this chip at anytime with any other sketch.
Modify file boards.txt
|
Find your file boards.txt, open it in an editor of your choice, and add some new lines to it. This is how I did it in linux; wonder if M$ still has notepad ? In linux, you will need your root password to write the file. When you restart your IDE, the new section will appear as a new board, using menu options "Tools -> Boards". |
|
When you have the file open in an editor of your choice, add this section of new lines (below):
For interest, note the line "upload using arduino:arduinoISP".
If you get an error "Unable to find file pins_arduino.h", then you have missed out the line "build.variant=standard".
This line is missing from the official arduino tutorial.
For later reading, look at website http://www.engbedded.com/fusecalc for a list of the fuse settings.
One of the fuse settings is to alter the chip so that it can't be re-written to a second time.
Leave the fuse options to be the same as your existing UNO fuse options.
Hence my 'NO' comment below. I found this one the hard way.
ADD THESE LINES TO FILE boards.txt atmsa16.name=ATmega in Stand Alone (w/ Arduino as ISP) atmsa16.upload.protocol=stk500 atmsa16.upload.maximum_size=32768 atmsa16.upload.speed=115200 atmsa16.upload.using=arduino:arduinoisp atmsa16.bootloader.low_fuses=0xff atmsa16.bootloader.high_fuses=0xde ### NO ! NO ! NO ! atmsa16.bootloader.high_fuses=0xdf atmsa16.bootloader.extended_fuses=0x05 #### atmsa16.bootloader.extended_fuses=0x07 atmsa16.bootloader.path=optiboot atmsa16.bootloader.file=optiboot_atmega328.hex atmsa16.bootloader.unlock_bits=0x3F atmsa16.bootloader.lock_bits=0x0F atmsa16.build.mcu=atmega328p atmsa16.build.f_cpu=16000000L atmsa16.build.core=arduino atmsa16.build.variant=standard |
Binary sketch size: 1,072 bytes (of a 32,768 byte maximum)
avrdude: Yikes! Invalid device signature.
Double check connections and try again, or use -F to override
this check.
|
Find the ArduinoISP sketch in your IDE. |
|
|
Ensure IDE option : Tools -> Boards -> Arduino UNO |
|
|
Ensure IDE option : Tools -> Programmer -> AVR ISP |
|
Then, verify and upload your sketch as normal, to your arduino.
|
Ensure IDE option : Tools -> Boards -> The New Board You Created in boards.txt |
|
|
Ensure IDE option : Tools -> Programmer -> Arduino as ISP |
|
Upload the sketch with UPLOAD USING PROGRAMMER option
|
Ensure IDE option : File -> Upload using programmer. Do NOT try and upload the sketch in the normal way. |
|
You're done. Tidy up.
Once you get the message 'Sketch uploaded', you should be done.
Remove the cables from the breadboard (Arduino pins 10, 11, 12 and 13), starting with the reset cable (target chip pin 1).
Reset your preferences.txt file to remove the -F (force) option. (This is optional)
To prove that the sketch is running, connect an LED (with resistor) to the correct pin and watch it blink.
You need to match the pin numbers on the target chip (1-28) with the correct arduino pin numbers (Digital 0-13, Analog 0-5
and the power etc).
The pin mapping can be found here or
here or google ATmega328P pin mapping.
Note with interest that you have also overwritten the target chip bootloader. This has been set in the boards.txt
options, you'll need to investigate further to see how these work.
Load a different sketch into your arduino (ENSURE you reset the two IDE options, board and programmer,
that you changed above)