1

I have used below program for ESP8266 as ISP to upload a program to an ATmega16.

ISP program uploaded in ESP8266

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266AVRISP.h>

const uint8_t reset_pin = 5;
const uint16_t port = 328;

ESP8266AVRISP avrprog(port, reset_pin);

void setup() {
    Serial.begin(9600);//115200
    Serial.println("");
    Serial.println("Arduino AVR-ISP over TCP");
    avrprog.setReset(false); // let the AVR run

    // listen for avrdudes
    avrprog.begin();
}

void loop() {
    static AVRISPState_t last_state = AVRISP_STATE_IDLE;
    AVRISPState_t new_state = avrprog.update();
    if (last_state != new_state) {
        switch (new_state) {
            case AVRISP_STATE_IDLE: {
                Serial.printf("[AVRISP] now idle\r\n");
                Serial.println("idle...");
                // Use the SPI bus for other purposes
                break;
            }
            case AVRISP_STATE_PENDING: {
                Serial.printf("[AVRISP] connection pending\r\n");
                Serial.println("pending...");
                // Clean up your other purposes and prepare for programming mode
                break;
            }
            case AVRISP_STATE_ACTIVE: {
                Serial.printf("[AVRISP] programming mode\r\n");
                Serial.println("Active...");
                // Stand by for completion
                break;
            }
        }
        last_state = new_state;
    }else{
      Serial.println("newstate not equal to old");
    }
    // Serve the client
    if (last_state != AVRISP_STATE_IDLE) {
        avrprog.serve();
        Serial.println("serve..");
    }
}

Then I have changed the board to ATmega16 and used programmer as AVR ISP and fetched/written below simple blink program to upload in ATmega16.

void setup() {
  // initialize digital pin 13 as an output.
  pinMode(1, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(1, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  Serial.println("light on");
  digitalWrite(1, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
  Serial.println("light off...");
}

Tried uploading blink program to ATmega16 using ESP8266 as ISP by Sketch =>Upload Using Programmer. This resulted to compilation followed by uploading status in Arduino IDE for longer period. I didn't get any response after 10 mins of time. Then i have tried opening Serial Monitor which shows newstate not equal to old.

I am not sure what went wrong in my coding? How can I debug this issue ?

1
  • I have been never sucessfull with the ISP Project. C:\avrdude-5.4-win>avrdude -c arduino -p m2560 -P net:192.168.178.38:328 -u -F avrdude: ser_drain(): read error: Falscher Parameter. avrdude: ser_drain(): read error: Falscher Parameter. avrdude: ser_drain(): read error: Falscher Parameter. avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x14 avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x10 avrdude: stk500_initialize(): (b) protocol error, expect=0x10, resp=0x01 avrdude: initialization failed, rc=-1 avrdude: AVR device initialized and ready to accept instr
    – Arnold
    Commented Nov 28, 2017 at 8:08

1 Answer 1

8

1. Uploading the sketch to your ESP8266

  • Tools > Board > select the appropriate ESP8266 board
  • File > Examples > ESP8266AVRISP > Arduino_Wifi_AVRISP
  • Change lines 7 and 8 to match the SSID and password of your WiFi router
  • Sketch > Upload
  • After the upload completes unplug the ESP8266 from your computer.

2. Generating the .hex file and the upload command

  • Open the sketch you want to upload to your ATmega16
  • Tools > Board > ATmega16
  • File > Preferences > Show verbose output during: > upload (check)
  • Sketch > Upload
  • The sketch will compile and then the upload will fail. This is the expected behavior. The goal of this process is to generate the .hex file that will be uploaded and the correct upload command.
  • Scroll the black console window at the bottom of the Arduino IDE window up until you see a line that looks something like:

>

E:\Arduino\hardware\tools\avr/bin/avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM54 -b115200 -D -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_288143/Blink.ino.hex:i

3. Connecting the ESP8266 to the ATmega16

Connect the ESP8266 to your ATmega16 board as follows:

+----------+-----------+
| ESP8266  | ATmega16  |
+----------+-----------+
| GPIO12   | MISO      |
| GPIO13   | MOSI      |
| GPIO14   | SCK       |
| GPIO5    | RESET     |
+----------+-----------+

You also need to power the ATmega16. It is recommended to use 3.3 V since this is what the ESP8266 is running at. Make sure the ground of the ESP8266 is connected to the ground of the ATmega16.

4. Starting the ESP8266AVRISP

  • Plug the ESP8266's USB cable back in to your computer
  • Tools > Serial Monitor
  • Select 115200 from the baud rate menu in the bottom right corner of Serial Monitor
  • You should see something like

IP address: 192.168.0.100
Use your avrdude: avrdude -c arduino -p -P net:192.168.0.100:3000 -t # or -U ...

5. Uploading via ESP8266AVRISP

  • The second line you saw in the Serial Monitor in step 4 is the sketch's attempt to give you the AVRDUDE command you need to run to upload to your ATmega16 via the ESP8266AVRISP. Unfortunately they got a bit vague at the end. Apparently they are trying to steer you towards using AVRDUDE in terminal mode via the -t option but also mentioning that -U might be preferred. I recommend the latter since the Arduino IDE has helpfully generated the -U command for you already.
  • Since you need to run a custom AVRDUDE command to upload to your ATmega16 over the network you will not be able to do the upload via the Arduino IDE. Start the command line on your computer.
  • Now you need to merge the upload command you generated when you did the intentionally failed upload to the ATmega16 in step 2 with the command that was printed to Serial Monitor in step 4. In my examples, this merged command would be:

>

E:\Arduino\hardware\tools\avr/bin/avrdude -CE:\Arduino\hardware\tools\avr/etc/avrdude.conf -v -c arduino -p atmega16 -P net:192.168.0.100:3000 -Uflash:w:C:\Users\per\AppData\Local\Temp\arduino_build_288143/Blink.ino.hex:i
  • Your command will be similar to mine but the path to avrdude, avrdude.conf, the IP address, port, and path to the .hex file will be different. Enter your merged AVRDUDE command at the command line.
  • The upload should now proceed successfully.

Comments

The above is quite a complex process. Although it will be significantly easier with subsequent uploads, it will likely still not be as easy as an upload via the Arduino IDE. I would not recommend regularly using the ESP8266 as an AVR ISP simply because it's what you have on hand. Instead I would strongly recommend you to just buy a cheap (~$3 USD with free shipping from China) 6 pin USBasp from eBay.

The primary benefit of using the ESP8266 for this application is that it can be done over the WiFi network rather than the traditional USB connection but it's questionable how often that will truly be useful.

2
  • 1
    Followed all the procedures and received below output when I run avrdude command. ser_drain(): read error: The parameter is incorrect. stk500_disable(): protocol error, expect=0x14, resp=0x10 stk500_initialize(): (b) protocol error, expect=0x10, resp=0x01 Mentioned complete error list in my question.
    – Simbu
    Commented Sep 13, 2017 at 14:50
  • You just saved my cheap Digispark-Attiny85 clone that I previously messed up... An ESP8266 was all I had around. This worked perfectly. Thanks!
    – dentex
    Commented Dec 8, 2020 at 8:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.