using leonardo to upload sketches to atmega168/328

yes...i wnt to use it to burn the arduino bootloader onto blank atmega chips and then use it to upload sketches to the newly bootloaded chip becuase i don't want to buy an FT232 breakout board

That question came up before, two options were given over here: Program AT328 with Leonardo - #2 by tim7 - Microcontrollers - Arduino Forum

If your goal is just to run some code on the targets, you are better off without bootloader: just burn your compiled sketches with ArduinoISP (I assume you used that to burn the bootloader into the targets?)

If you really want to use the target as an arduino on a breadbord, and program it from the IDE etc... You'd need to run a sketch on the leo that copies everything received from Serial (usb) to Serial1 (the regular uart) and vice versa. You'd have to connect respectively tx/rx on the leo to rx/tx on your target.

You can even combine the two functionalities into one sketch and use a jumper to select between isp mode and usb2serial mode (I saw the idea here: http://arduino.cc/forum/index.php/topic,101690.0.html, though the situation with the leonardo is a bit simpler: augmenting a sketch (ArduinoISP) with USB to serial conversion instead of adapting a bootloader)

Uno and Leonardo can be used as true ISP programmers/basic debuggers for most target AVRs. You do not have to remove any chip and you do not need to burn any bootloader.
The main idea is to use avrdude:
see: http://arduino.cc/forum/index.php/topic,125248.0.html
(Have enough patience and read the topic to the end.)
When I have time I will make a step by step tutorial.

PeterVH:
That question came up before, two options were given over here: Program AT328 with Leonardo - #2 by tim7 - Microcontrollers - Arduino Forum

If your goal is just to run some code on the targets, you are better off without bootloader: just burn your compiled sketches with ArduinoISP (I assume you used that to burn the bootloader into the targets?)

If you really want to use the target as an arduino on a breadbord, and program it from the IDE etc... You'd need to run a sketch on the leo that copies everything received from Serial (usb) to Serial1 (the regular uart) and vice versa. You'd have to connect respectively tx/rx on the leo to rx/tx on your target.

You can even combine the two functionalities into one sketch and use a jumper to select between isp mode and usb2serial mode (I saw the idea here: http://arduino.cc/forum/index.php/topic,101690.0.html, though the situation with the leonardo is a bit simpler: augmenting a sketch (ArduinoISP) with USB to serial conversion instead of adapting a bootloader)

i saw the post about using the leonardo but the way to dit is not specified there...it woud be nice to get the sketch which will copy everything from Serial to Serial1

In its simplest form it is no more than this:

// for now, the baud rate is hard coded
// it should match what the target's bootloader uses
static long baud = 57600;

void setup() {
  Serial.begin(baud);
  Serial1.begin(baud);
}

void loop() {
  if (Serial.available()) {
    char c = (char)Serial.read();
    Serial1.write(c);
  }
  if (Serial1.available()) {
    char c = (char)Serial1.read();
    Serial.write(c);
  }
}

I tested it uploading a blink sketch to an atmega328p with some duemilanove bootloader.
It requires some practice though: keep the target in reset. Hit the download icon. Release the reset button right after the compilation phase is done...

What we really need is autoreset support. I'll cook something...

What would also be nice is that the leo automatically sets Serial1's baud rate, when the pc sets the "baudrate" of the virtual com port.

riyadhalnur:
the tutorial on the arduino site uses uno which has a removable microcontroller but the Leonardo doesn't have that option. how do I go about doing that?

There is a tutorial about Arduino Uno as an ISP programmer that teaches you how to program a Target AVR without removing the microcontroller from Arduino board.

see: http://pdp11.byethost12.com/AVR/ArduinoAsProgrammer.htm

I believe the method will also work for Leonardo.

I'm really sorry for hijacking this thread, but if I had to define a topic for my problem, this would be exactly it.

So I've got this setup with an Atmega32U4 and an Atmega168 on a custom board. The Atmega32U4 is equipped with the Leonardo Bootloader (caterina) and works like a charm. The Atmega168 is supposed to run one of the other Arduino bootloaders. For my test setup, I use an old Duemilanove with an Atmega328.
So what I'm trying to do right now is to use the /dev/ttyACMn interface of the Leonardo to program the Duemilanove which is connected to the secondary (serial1) UART.
Or simply:

[PC]===[Leonardo]===[Due]

The Code on the Leonardo looks like this:

int led = 7;         // incoming serial byte
int reset = PORTD4;
int stillReset = 0;
void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(led, OUTPUT); 
  pinMode(reset, OUTPUT);
  digitalWrite(reset, LOW);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

}

void loop()
{
  if (Serial.available() > 0) {
    if (stillReset > 0){
      digitalWrite(reset,HIGH);
    }
    if (stillReset == 0){
      digitalWrite(reset,LOW);
      stillReset ++;
    }

    digitalWrite(led, HIGH);
    Serial1.write(Serial.read());               
  }
    if (Serial1.available() > 0) {
      digitalWrite(led, LOW);
    Serial.write(Serial1.read());               
  }
}

As you can see, the Due is reset when the first byte is received from the PC. My analysis seemed to show that the PC is sending the character '0' three times and waiting for the Arduino to reply. But even without the automatic reset, if I try it manually, it only shows me a bunch of sdk-failed errors.
Any idea why this is not working? Is the /dev/ttyACMn really equal to the /dev/ttyUSBn or /dev/ttySn?

Thx for your help!

@kopfkopfkopfaffe: I am glad you ask. I just finished a write up of an experiment that exactly does that.

It is an improved version of sketch posted earlier in this thread.
It now handles autoreset and sets the baud rate of the uart.
It can be used to download sketches via the ide, but also to communicate with the sketch itself.
A minor hack in the core is needed though.

Here it is:

But even without the automatic reset, if I try it manually, it only shows me a bunch of sdk-failed errors.

My guess is that you have serial buffer overruns. Follow the link above and have look at the fixes proposed in "ArduinoISP on the Leonardo"

PeterVH, your other tutorial on modifying the leonardo helped a lot. i made a breadboard arduino and programmed it via the leonardo but i coduln't do it via the IDE. it kept giving me device signature failures. so i used avrdude from cmd with the -F command at the end to override the signature check and everything worked perfectly

Good to hear, but I find it very suspicious you had to use -F. What signature did avrdude report? Was it 0 (then you still have a problem in your wiring or setup) or just not the signature for your target.

I find it very suspicious you had to use -F

I read the post of riyadhalnur yesterday and did a test using an Uno not a Leonardo because I do not have one. I tried to check if using the -F option will change the error message thrown by avrdude in case a SPI connection is broken.
The reported error changes but it remains an evident error.
If I have good connections between Arduino and the target AVR no error is displayed.

My settings:
Arduino Uno + ArduinoISP as in ISP Programmer
Connections: Arduino Uno (SS, Miso, Mosi, SCK) -> taget on breadboard Atmegae328p (Reset, Miso, Mosi, SCK).

  1. Good ISP connections:
c:\>avrdude -P COM3 -b 19200 -c avrisp -p m328p -n
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.17s
avrdude: Device signature = 0x1e950f
avrdude: safemode: Fuses OK
avrdude done.  Thank you.
  1. SS->RESET connection broken
c:\>avrdude -P COM3 -b 19200 -c avrisp -p m328p -n
avrdude: stk500_program_enable(): protocol error, expect=0x14, resp=0x50
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51
avrdude done.  Thank you.
  1. SS->RESET connection broken but -F added
c:\>avrdude -P COM3 -b 19200 -c avrisp -p m328p -n -F
avrdude: stk500_program_enable(): protocol error, expect=0x14, resp=0x50
avrdude: initialization failed, rc=-1
avrdude: AVR device initialized and ready to accept instructions
avrdude: Device signature = 0x000000
avrdude: Yikes!  [b]Invalid device signature[/b].
avrdude: Expected signature for ATMEGA328P is 1E 95 0F
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51
avrdude done.  Thank you.

Conclusion: -F does not fix anything as long as you have wrong SPI wiring and you do not get the "use -F" error message if you have good connections, excepting the case when the AVR oscillator (external) does not work or the target AVR is not powered!

avrdude kept reporting the device signature to be 1E--0B when it says it expects the signature to be 1E--06

I still have issues with your sketch. The auto reset works fine, I can see that on the scope, see attached image. The reset line is green and is released once DTR is triggered, letting the slave Arduino run it's bootloader. (Which it does.). Shortly after that, the blue line (Leonardo TX) issues 3 requests to the bootloader of the slave Arduino, which does not respond. So the upload fails. Right now I have no idea why. If I use the regular USB connection of the slave Arduino, it works. I'd once been able to flash my Duemilanove with an external FTDI, but for some reason, that also fails now.

@kopfkopfkopfaffe: the plot of RESET makes me think you did not put the series cap of 100nF. (see the ascii art schematics in my post).

avrdude kept reporting the device signature to be 1E--0B when it says it expects the signature to be 1E--06

Ok, so you have an atmega168p instead of an atmega168. To avoid mistakes, it is better to invoke avrdude with -pm168p instead of using -F.
Not all avrdude installations have an entry for atmega168p in their avrdude.conf; if not, use the avrdude from the arduino IDE.

Oh, it's measured BEFORE the cap. Plus I can see the Arduino reset, because the high frequency blink-sketch stops and starts again after avrdude fails. So that works.

I am affraid this is an os dependent thing. I am using kubuntu. I just tried in win xp, it does't work for me either. I don't understand why, I see no difference between the leo setting a DTR_PIN in function of the line status, and the ftdi chip doing so for its own dtr pin. I must think about this...

What os are you on?

We'll get there, I found back a pic18f4550 with which I built the same thing. It works on windows too, I just need to find out what it does differently.