Firmware · Hardware · Reverse engineering
Bringing an Arduino Mega back with an ESP32 as the programmer
An ESP32 acts as an ISP programmer to restore both chips of an Arduino Mega 2560 altered with HoodLoader2, without buying a programmer.
- Year
- 2026
- Status
- Working
- Role
- Firmware and electronics
- Repository
- Repository coming soon
01 · Problem
An Arduino Mega 2560 running HoodLoader2 / ArduDucky could no longer be programmed from the IDE. The altered firmware lived on both chips: the main ATmega2560 and the ATmega16U2 that bridges USB.
02 · Approach
I wrote a dual ISP programmer for a Heltec ESP32: it enters programming mode, checks each chip signature, erases, writes the original fuses, flashes the stk500v2 bootloader or the DFU + USB-Serial firmware, verifies byte by byte and writes the lock bits. A divider (1 kΩ / 2.2 kΩ) drops MISO from 5 V to 3.44 V.
03 · Result
The Mega is back to factory state and can be programmed again from the Arduino IDE and PlatformIO. The process is guided from the serial monitor: you move the same 5 wires from the ICSP1 header to ICSP2 and you are done.
04 · Stack
- ESP32 (Heltec WiFi Kit 32)
- C++ / Arduino
- PlatformIO
- ISP / SPI
- ATmega2560
- ATmega16U2
- Python
Why two chips need restoring
| Chip | Where | What it does | What gets flashed |
|---|---|---|---|
| ATmega2560 | Center of the board | Runs your sketch | stk500v2 bootloader (7,454 bytes) |
| ATmega16U2 | Next to the USB jack | USB ↔ serial bridge | DFU + USB-Serial firmware (15,668 bytes) |
HoodLoader2 modifies both. If you only restore the main one, the IDE still won’t see the board: the USB chip is the one talking to the PC.
Wiring
ESP32 GPIO 5 ──────────────► RESET (ICSP pin 5)ESP32 GPIO 18 ──────────────► SCK (ICSP pin 3)ESP32 GPIO 23 ──────────────► MOSI (ICSP pin 4)ESP32 GPIO 19 ◄── divider ─── MISO (ICSP pin 1)ESP32 GND ──────────────► GND (ICSP pin 6)
MISO (5 V) ──[1 kΩ]──┬── GPIO 19 → 5 V × 2.2 / 3.2 = 3.44 V └─[2.2 kΩ]── GNDSignals leaving the ESP32 (3.3 V) are read by the Mega as logic high; only MISO needs to come down. The Mega is powered from its own USB, never from the ESP32’s 3.3 V.
Reference fuses
| Chip | Low | High | Extended | Lock |
|---|---|---|---|---|
| ATmega2560 | 0xFF |
0xD8 |
0xFD |
0x0F |
| ATmega16U2 | 0xEF |
0xD9 |
0xF4 |
0x0F |
The firmware embeds the bootloader and the 16U2 firmware as C arrays, generated with a small Python script from the official .hex files.