The Start


Hi, What and Why

Plug it in

Rule number 1

Water Sensor

Sound Sensor

Joystick

Tri Colour LED

RTC (Real Time Clock) DS1302

RTC (Real Time Clock) DS3231

Matrix LED step 1

LCD

Stepper Motor

LCD revisited with PCF8574T

Humidity Sensor

Shift Register

RFID tags (RC-522)

7 Segment display

Ultrasonic distance sensor

5V regulator

analogRead and analogWrite

Wiring an Array of Switches

The next step


Other things I have bought

Infra red and Processing

Programming a separate arduino chip

Creating your own PCB

L293D for a DC motor

4 digit 7 segment display

Starting with motors

RF433 Wireless Comms

Sort a character array

More stuff


I2C devices (SDA,SCL)

I2C scanner

SPI devices (MOSI,MISO)

HMC5883L Compass

MMA7361 Accelerometer

Added projects


Message Display System

4WD robot car
4WD robot car II

4WD robot car COMPLETE

MP3 Player

Programming a standalone arduino chip

ATMEGA 328 P


The majority of this info came from open-electronics.org, with a couple of instructions from the arduino tutorial website (arduino.cc), and a little bit of inspiration from me.

My objective is to create a standalone entity, that is programmed to do a single task around my home, based on an arduino chip.

This works with my arduino UNO and my linux laptop; it may or may not work on other arduino boards or systems.
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.
Objective

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
}

Making it work.

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.

Wire the chip.

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.

Connect the chip.

The new part

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.

Step 1

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".
modify boards.txt.

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

Step 2


Modify file preferences.txt.

In linux, this file is located here:
/home/<your username>/.arduino/preferences.txt

One of the lines in this file is :
run.options=
Change this to
run.options=-F

This supplies the -F (force) option to the underlying command line.
This is required, in case you get this error :
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.

Step 3

Upload ArduinoISP to your arduino.

Find the ArduinoISP sketch in your IDE.
ArduinoISP sketch.

Ensure IDE option : Tools -> Boards -> Arduino UNO
ArduinoISP sketch.

Ensure IDE option : Tools -> Programmer -> AVR ISP
ArduinoISP sketch.

Then, verify and upload your sketch as normal, to your arduino.

Step 4

Upload the sketch blink_500 to the TARGET CHIP.
You need to ensure these two IDE options are correctly set to use the target chip :

Ensure IDE option : Tools -> Boards -> The New Board You Created in boards.txt
ArduinoISP sketch.

Ensure IDE option : Tools -> Programmer -> Arduino as ISP
ArduinoISP sketch.

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.
ArduinoISP sketch.

Step 5.

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)

Go back to the top of the page to see the first picture. The target chip on the breadboard is now running the sketch you uploaded to it, and the arduino is running a different sketch.

Hope this works for you, and leave a message if it does !