Read Code from Arduino: .hex Backup with avrdude, Erase, Reset
Can you read code from an Arduino? Why source code never comes back, a .hex backup with avrdude, blank sketch, EEPROM clearing, ESP32 esptool and lock bits.
When people want to read code from an Arduino, they usually picture pulling the program back to the computer and opening it as an .ino file. The short answer: that is not possible. The board does not hold the source code you wrote, only the machine code the compiler produced. You can read that machine code as a .hex file with avrdude, copy it one to one to another board or keep it as a backup, but variable names, comments and the layout of your lines never come back.
In this guide you will first see why the code cannot be recovered and what stays on the board and what is lost. Then you will make a .hex backup with real avrdude commands for the Uno, Nano, Mega and Leonardo, restore it, and read the EEPROM and fuse bytes with an ISP programmer. After that you will reset the board with a blank sketch and an EEPROM-clearing sketch, use esptool read_flash and erase_flash on the ESP32, and finally learn how to protect your own code with lock bits.

Why can't you read the code back from an Arduino?
When you press Upload, the Arduino IDE first turns your .ino file into C++, the avr-gcc compiler converts it into instructions the processor understands, and the result is written to a .hex file. Only that file goes to the board. The compiler replaces variable names with addresses and functions with jump instructions, throws away unused code and rearranges the rest for speed or size. That is why opening the .hex file with avr-objdump shows you assembly instructions, and decompilers do not give you readable Arduino code either, only a C draft that takes hours to understand. If you really want to save a project, keeping the source code on your computer, in the cloud or in a Git repository is the only safe way.
| Item in the source code | What happens on the board? | Does it come back from a backup? |
|---|---|---|
| Comments | Removed completely at compile time | No |
| Variable and function names | Turned into memory addresses | No |
| Libraries | Only the machine code of the functions you used remains | Their names do not |
| Text constants (Serial.print messages, Wi-Fi name and password) | Stored in flash exactly as written | Yes, readable as plain text |
| Pin numbers and numeric constants | Embedded inside the instructions | Only through assembly analysis |
| Data in the EEPROM | Kept unchanged in a separate memory | Yes, via ISP or a sketch |
| Bootloader | Sits at the very end of flash | Yes, it is part of the read |
The text constants row carries an important warning: the Wi-Fi password, API key or door-lock code you typed into your sketch sits in the .hex file in readable form. Think about who could read it before you give your board away or sell it as a product. On the memory side there are three separate regions: the program lives in flash, variables used while running live in SRAM, and permanent settings are kept in EEPROM. SRAM is lost when power is removed; flash and EEPROM keep their contents for years. For details on these memory regions see the Arduino low memory guide.
| Board | Processor | Flash / bootloader | EEPROM | avrdude programmer and speed |
|---|---|---|---|---|
| Arduino Uno R3 | ATmega328P | 32 KB / 0.5 KB (Optiboot) | 1 KB | -c arduino -b 115200 |
| Arduino Nano (new bootloader) | ATmega328P | 32 KB / 0.5 KB | 1 KB | -c arduino -b 115200 |
| Arduino Nano (Old Bootloader) | ATmega328P | 32 KB / 2 KB | 1 KB | -c arduino -b 57600 |
| Arduino Mega 2560 | ATmega2560 | 256 KB / 8 KB | 4 KB | -c wiring -b 115200 |
| Leonardo, Pro Micro | ATmega32U4 | 32 KB / 4 KB (Caterina) | 1 KB | -c avr109 -b 57600 |
Step by step: making a .hex backup of an Arduino with avrdude
avrdude is not a program you have to install separately; it ships with the Arduino IDE. The easiest route is this: in File > Preferences tick Show verbose output during upload and upload any sketch. The output window then prints the full path of avrdude, the avrdude.conf file passed with -C and your board's port on a single line. Copy that line and change only the -U part at the end to turn it into a read command. On Windows the path is usually under AppData\Local\Arduino15\packages\arduino\tools\avrdude, on macOS under ~/Library/Arduino15 and on Linux under ~/.arduino15; the version folder name (for example 6.3.0-arduino17) may differ on your installation. If you run the commands from inside the bin folder, you can pass the configuration file as ../etc/avrdude.conf. If you like the command line, the Arduino CLI guide makes this even easier.
- Close the Serial Monitor and Serial Plotter in the Arduino IDE; avrdude cannot reach the board while the port is open.
- Note your board's port from Tools > Port (something like COM3 on Windows, a name starting with /dev/cu.usbmodem on macOS).
- Open a terminal or command prompt and change to avrdude's bin folder.
- Copy the read command that matches your board from below, replace the port name with yours and run it.
- It takes a few seconds; at the end you should see the line avrdude done. Thank you.
- Open the new .hex file in a text editor: if every line starts with a colon, the backup is sound.
1# Arduino Uno ve yeni bootloader'lı Nano / Uno and Nano (new bootloader)
2avrdude -C ../etc/avrdude.conf -v -p atmega328p -c arduino -P COM3 -b 115200 -U flash:r:yedek_uno.hex:i
3
4# Eski bootloader'lı Nano (ATmega328P Old Bootloader) / Nano with the old bootloader
5avrdude -C ../etc/avrdude.conf -v -p atmega328p -c arduino -P COM3 -b 57600 -U flash:r:yedek_nano.hex:i
6
7# Arduino Mega 2560
8avrdude -C ../etc/avrdude.conf -v -p atmega2560 -c wiring -P COM3 -b 115200 -U flash:r:yedek_mega.hex:i
9
10# Leonardo / Pro Micro: önce reset'e bas, 8 saniye içinde çalıştır / press reset, run within 8 seconds
11avrdude -C ../etc/avrdude.conf -v -p atmega32u4 -c avr109 -P COM7 -b 57600 -U flash:r:yedek_leo.hex:i
12
13# macOS / Linux port örnekleri / port examples: /dev/cu.usbmodem14101, /dev/ttyACM0, /dev/ttyUSB0Here is what the flags mean: -p sets the processor model (atmega328p, atmega2560), -c the protocol spoken by the bootloader on the board, -P the port and -b the bootloader's baud rate. -v gives verbose output and shows the processor signature (1E 95 0F for the Uno). The part that does the real work is -U flash:r:yedek_uno.hex:i: read (r) the flash memory (flash) and write it to yedek_uno.hex in Intel HEX format (i). Put w instead of r in the same expression and it writes the file to the board; put v and it only compares. The readable area of the Uno is 32,768 bytes; 512 of them belong to the bootloader and are included in the read.
Restoring the backup and copying it to another board
The .hex file you made can be loaded one to one onto another board with the same processor. A backup from an Uno can go to another Uno or to a Nano with the new bootloader, because both use a 16 MHz ATmega328P. You cannot write a Mega 2560 backup to an Uno, the processor is different. The -D flag in the write command disables auto erase, just as the Arduino IDE does; the bootloader erases the pages itself. After writing, avrdude reads the file back from the board, compares it and reports verified.
1# Yedeği aynı model karta geri yükle / restore the backup to the same board model
2avrdude -C ../etc/avrdude.conf -v -p atmega328p -c arduino -P COM3 -b 115200 -D -U flash:w:yedek_uno.hex:i
3
4# Mega 2560
5avrdude -C ../etc/avrdude.conf -v -p atmega2560 -c wiring -P COM3 -b 115200 -D -U flash:w:yedek_mega.hex:i
6
7# Yalnız karşılaştır, yazma / verify only, do not write
8avrdude -C ../etc/avrdude.conf -p atmega328p -c arduino -P COM3 -b 115200 -U flash:v:yedek_uno.hex:iReading EEPROM, fuse and lock bytes: the ISP programmer
The Optiboot bootloader on the Uno is designed only to read and write flash; it does not support EEPROM reads and has no access at all to the fuse and lock bytes. For those you need an ISP programmer. The most common one is the inexpensive USBasp; if you do not have one, upload File > Examples > 11.ArduinoISP to a second Arduino and use it as the programmer. In that case connect pins D11, D12 and D13 of the two boards to each other, the programmer's D10 to the target's RESET pin, and share GND and 5 V. If the programmer board is an Uno, put a 10 µF capacitor between its RESET and GND pins; otherwise it resets itself when avrdude connects. With ISP you can even read a board with no bootloader or a corrupted one. The fuse bytes set the processor's clock source, bootloader size and brown-out level. On a stock Uno you will usually see lfuse 0xFF, hfuse 0xDE and efuse 0xFD (0x05 in some avrdude versions). When you move a project to another chip you have to write these values as well; otherwise the code may run but at the wrong clock speed. Wrong fuses can leave a chip unable to run without an external crystal, so always note the values before writing any.
1# USBasp: flash, EEPROM, sigortalar ve kilit baytı / flash, EEPROM, fuses and lock byte
2avrdude -C ../etc/avrdude.conf -p atmega328p -c usbasp -P usb -U flash:r:flash.hex:i -U eeprom:r:eeprom.hex:i -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h -U lock:r:-:h
3
4# ArduinoISP yüklü ikinci bir Arduino ile / with a second Arduino running ArduinoISP
5avrdude -C ../etc/avrdude.conf -p atmega328p -c stk500v1 -P COM4 -b 19200 -U flash:r:flash.hex:i -U eeprom:r:eeprom.hex:i -U lock:r:-:hErasing the code and the EEPROM on an Arduino
The reset button does not erase the code, it only restarts the program. Cutting power does not erase it either, because flash memory keeps its data without power. The practical way to erase the program on an Arduino is to upload a blank sketch: open File > Examples > 01.Basics > BareMinimum; it contains only empty setup() and loop() functions and overwrites the old program when uploaded. Because no pin is set as an output in a blank sketch, connected relays, motors or LEDs are not driven, which makes it a safe starting point before you wire up a new circuit. The ATmega328P flash is rated for at least 10,000 write cycles, so even dozens of uploads a day will not cause trouble for years.
Uploading a new sketch does not erase the EEPROM. If your previous project stored a calibration value, a password or a counter there, the new code may read that old data and behave unexpectedly. The sketch below dumps the whole EEPROM to the Serial Monitor in rows of 16 bytes and, if you want, sets everything back to the factory value 0xFF. Set the Serial Monitor to 115200 baud, send d to list the contents and s to start clearing. Clearing 1024 bytes on an Uno takes about 3.5 seconds, because each EEPROM write takes around 3.3 ms. If you copy the list printed by the d command into a text file before clearing, you also have a backup of the EEPROM; all EEPROM commands are in the Arduino EEPROM guide.
1// Arduino EEPROM okuma ve temizleme / Arduino EEPROM dump and clear
2// Seri Monitör 115200 baud: 'd' = içeriği yazdır, 's' = hepsini 0xFF yap
3// Serial Monitor 115200 baud: 'd' = dump contents, 's' = set everything to 0xFF
4#include <EEPROM.h>
5
6const int BYTES_PER_ROW = 16; // Satır başına bayt / bytes per row
7
8void dumpEeprom() {
9 char buf[8];
10 int usedBytes = 0; // 0xFF olmayan bayt sayısı / non-0xFF byte count
11 for (int addr = 0; addr < (int)EEPROM.length(); addr += BYTES_PER_ROW) {
12 sprintf(buf, "%04X: ", addr); // Satırın başlangıç adresi / row start address
13 Serial.print(buf);
14 for (int i = 0; i < BYTES_PER_ROW; i++) {
15 byte value = EEPROM.read(addr + i);
16 if (value != 0xFF) usedBytes++;
17 sprintf(buf, "%02X ", value);
18 Serial.print(buf);
19 }
20 Serial.println();
21 }
22 Serial.print(F("Dolu bayt / used bytes: "));
23 Serial.println(usedBytes);
24}
25
26void clearEeprom() {
27 for (int addr = 0; addr < (int)EEPROM.length(); addr++) {
28 EEPROM.update(addr, 0xFF); // Yalnız farklıysa yazar, ömrü korur / writes only if different, saves wear
29 }
30 Serial.println(F("EEPROM temizlendi / EEPROM cleared (0xFF)"));
31}
32
33void setup() {
34 Serial.begin(115200);
35 pinMode(LED_BUILTIN, OUTPUT);
36 Serial.print(F("EEPROM boyutu / size: "));
37 Serial.print(EEPROM.length()); // Uno/Nano 1024, Mega 4096
38 Serial.println(F(" bayt / bytes"));
39 Serial.println(F("'d' = yazdir / dump, 's' = temizle / clear"));
40}
41
42void loop() {
43 if (Serial.available()) {
44 char cmd = Serial.read();
45 if (cmd == 'd') dumpEeprom();
46 if (cmd == 's') {
47 digitalWrite(LED_BUILTIN, HIGH); // Temizlik sürerken LED yanar / LED on while clearing
48 clearEeprom();
49 digitalWrite(LED_BUILTIN, LOW);
50 }
51 }
52}| Code part | What does it do? |
|---|---|
| EEPROM.length() | Returns the board's EEPROM size: 1024 bytes on Uno and Nano, 4096 on Mega |
| sprintf(buf, "%04X: ", addr) | Prints the row address as a four-digit hexadecimal number |
| EEPROM.read(addr + i) | Reads one byte; reading does not wear out the EEPROM |
| EEPROM.update(addr, 0xFF) | Skips the write if the byte is already 0xFF, so the 100,000-cycle life is not wasted |
| usedBytes | Counts the bytes that are not 0xFF; 0 means the EEPROM is empty |
Full reset and protecting your code: chip erase and lock bits
The -e (chip erase) command sent through an ISP programmer erases flash, EEPROM and the lock bits together; it is the closest you get to a factory-fresh chip. Careful: the bootloader also lives in flash, so it is erased too and the board no longer accepts code over USB. To bring the board back, choose USBasp or Arduino as ISP under Tools > Programmer and run Burn Bootloader. If you want to protect your code you use the same tool the other way round: to run without a bootloader, upload the code over ISP with Sketch > Upload Using Programmer and then write the lock byte.
| Lock mode | Lock byte (BLB bits open) | Result |
|---|---|---|
| LB mode 1 | 0x3F | No lock: reading and writing allowed (Arduino's value after erase) |
| LB mode 2 | 0x3E | Further programming disabled, reading still possible |
| LB mode 3 | 0x3C | Programming and reading disabled; the data read back is meaningless |
| Arduino default | 0x0F | Protects only the bootloader section, not your code |
1# Tam silme: flash + EEPROM + kilit bitleri, bootloader da silinir / chip erase, the bootloader is removed too
2avrdude -C ../etc/avrdude.conf -p atmega328p -c usbasp -P usb -e
3
4# Programı ISP ile yaz ve kilitle (LB mod 3) / write the program over ISP and lock it (LB mode 3)
5avrdude -C ../etc/avrdude.conf -p atmega328p -c usbasp -P usb -U flash:w:urun.hex:i -U lock:w:0x3C:m
6
7# Kilidi kontrol et: 0x3C görmelisin / check the lock byte: you should see 0x3C
8avrdude -C ../etc/avrdude.conf -p atmega328p -c usbasp -P usb -U lock:r:-:hReading and erasing ESP32 flash with esptool read_flash and erase_flash
On the ESP32 the program lives in an external SPI flash chip next to the processor; on most boards it is 4 MB. Here esptool takes the place of avrdude. If the ESP32 board package is installed, esptool is ready in Arduino15\packages\esp32\tools\esptool_py; if you use Python you can also install it with pip install esptool. First find the flash size with flash_id, then read it from start to end with read_flash. Reading 4 MB takes a few minutes at 115200 baud and drops to about one minute with --baud 921600. If the board does not connect, hold the BOOT button and run the command again.
1# Flash boyutunu öğren / detect the flash size
2esptool.py --chip esp32 --port COM5 flash_id
3
4# 4 MB flash'ın tamamını yedekle / back up the whole 4 MB flash
5esptool.py --chip esp32 --port COM5 --baud 921600 read_flash 0 0x400000 esp32_yedek.bin
6
7# Yedeği geri yaz / write the backup back
8esptool.py --chip esp32 --port COM5 --baud 921600 write_flash 0x0 esp32_yedek.bin
9
10# Flash'ın tamamını sil / erase the whole flash
11esptool.py --chip esp32 --port COM5 erase_flash
12
13# Yalnız NVS bölümünü sil (varsayılan bölüm şeması) / erase only NVS (default partition scheme)
14esptool.py --chip esp32 --port COM5 erase_region 0x9000 0x5000An ESP32 backup contains the bootloader, the partition table, the application, SPIFFS or LittleFS files and the NVS partition. NVS is where the ESP32 keeps saved Wi-Fi networks and the settings you wrote with Preferences, so the backup file carries your Wi-Fi password too. Only restore it to a board with the same flash size. In esptool 5 the commands are written with hyphens, such as read-flash and erase-flash; the old underscore names are still accepted. If you set Tools > Erase All Flash Before Sketch Upload to Enabled in the Arduino IDE, the whole flash is erased automatically on the next upload. If you only want to remove saved Wi-Fi and settings, the short sketch below is enough. To get to know the board in general, see the what is ESP32 guide. On the ESP32 code protection does not come from lock bits but from Flash Encryption and Secure Boot, enabled through eFuses. With Flash Encryption on, read_flash still works, but the file contains encrypted data that is useless on another chip. eFuses are burned once and cannot be undone; a wrong setting can make the board permanently unusable, so try it on a spare board before applying it to a product.
1// ESP32 NVS temizleme: kayıtlı Wi-Fi ve Preferences verisini siler
2// ESP32 NVS erase: removes stored Wi-Fi credentials and Preferences data
3#include <nvs_flash.h>
4
5void setup() {
6 Serial.begin(115200);
7 delay(1000);
8 esp_err_t err = nvs_flash_erase(); // NVS bölümünü tamamen sil / erase the whole NVS partition
9 Serial.printf("nvs_flash_erase: %s\n", esp_err_to_name(err));
10 err = nvs_flash_init(); // Boş NVS'yi yeniden hazırla / re-initialise the empty NVS
11 Serial.printf("nvs_flash_init: %s\n", esp_err_to_name(err));
12 Serial.println("Bitti, simdi yeni sketch yukleyebilirsin / Done, upload your new sketch now");
13}
14
15void loop() {
16 delay(1000); // Tekrar silmemek için burada bekle / wait here, erase only once
17}Board, task and command summary table
| Board | Task | Command or method |
|---|---|---|
| Uno, Nano | Flash backup | avrdude -p atmega328p -c arduino -b 115200 -U flash:r:yedek.hex:i |
| Nano (Old Bootloader) | Flash backup | Same command, -b 57600 |
| Mega 2560 | Flash backup | avrdude -p atmega2560 -c wiring -b 115200 -U flash:r:yedek.hex:i |
| All AVR boards | Restore the backup | -D -U flash:w:yedek.hex:i |
| All AVR boards | Erase the code | Upload a blank sketch (BareMinimum) |
| All AVR boards | Clear the EEPROM | Sketch that sets every byte to 0xFF with EEPROM.update |
| AVR + ISP | Read EEPROM, fuses, lock | -c usbasp -U eeprom:r:eeprom.hex:i -U lock:r:-:h |
| AVR + ISP | Full erase | avrdude -e, then Burn Bootloader |
| AVR + ISP | Lock the code | -U lock:w:0x3C:m (without bootloader) |
| ESP32 | Flash backup | esptool.py read_flash 0 0x400000 yedek.bin |
| ESP32 | Full erase | esptool.py erase_flash |
| ESP32 | Wi-Fi and settings only | erase_region 0x9000 0x5000 or nvs_flash_erase() |
Common errors and how to fix them
| Error message or symptom | Likely cause | Fix |
|---|---|---|
| stk500_recv(): programmer is not responding | Wrong port or the baud rate does not match the bootloader | Try -b 57600 on a Nano, pick the right COM port |
| ser_open(): can't open device | The Serial Monitor or another program is using the port | Close the Serial Monitor in the IDE, unplug and replug the cable |
| Expected signature for ATmega328P is 1E 95 0F | Different chip (ATmega328PB, ATmega168) or bad wiring | Change the -p value to match the chip, check the ISP wires |
| The .hex read back is all FF or garbage | The board is empty or the lock bits are in LB mode 3 | Read the lock byte; if locked, only a chip erase is possible |
| Failed to connect to ESP32 | The board did not enter download mode | Run the command while holding BOOT, use a data cable |
| The restored code does not run | Different processor, clock speed or flash size | Write the backup to the same model with the same fuses |
In short, you cannot get the source code back from an Arduino, but you can back up, copy, erase and lock its machine code. When you try the read command for the first time, upload an unimportant sketch, back it up and restore it; that way you see in advance what will happen with your real project. If you get similar port errors while uploading, see the Arduino upload error guide. The best backup is always the source file itself. If you want to start a new project on a clean board, the BlueGrays app gives you step-by-step circuit diagrams and commented code for LED, button, sensor and ESP32 projects, so your code is always with you.
Frequently asked questions
Can you get the code back from an Arduino?
The source code (.ino) cannot be recovered, because the board only holds compiled machine code. With avrdude you can read that machine code as a .hex file and copy it to another board, but comments and variable names do not come back.
How do I erase the code on an Arduino?
The easiest way is to upload a blank sketch such as BareMinimum; the new program overwrites the old one. EEPROM data is not affected, so you have to set it to 0xFF with a separate clearing sketch. With an ISP programmer, avrdude -e erases everything but removes the bootloader too.
Does the reset button delete the Arduino program?
No. The reset button only restarts the processor and the program runs again from setup(). The code in flash and the data in EEPROM stay exactly as they were.
Can you convert a .hex file back to Arduino code?
You cannot get readable Arduino code. avr-objdump turns a .hex file into assembly instructions and you can see the text constants, but the original variable names, comments and structure are gone. Understanding even a small program this way takes hours.
How do I protect my Arduino code from being copied?
Remove the bootloader, upload the code with an ISP programmer and then set the lock byte to 0x3C (LB mode 3). Flash and EEPROM can then no longer be read from outside; only a chip erase, which also deletes the code, removes the lock. On the ESP32 the equivalent is Flash Encryption and Secure Boot.
How do I back up and erase the code on an ESP32?
With esptool, the command esptool.py read_flash 0 0x400000 yedek.bin backs up the whole 4 MB flash and write_flash 0x0 yedek.bin restores it. erase_flash wipes everything; to remove only saved Wi-Fi and settings, erasing the NVS partition is enough.
Projects to build after reading this
See all projects
LED Blink
The very first hello of electronics: switching a single LED on and off. The Arduino keeps the LED on for one second and off for one second, so you can easily follow every blink.

Traffic Light
A tiny copy of the traffic light you see on the road every day. Red stays on for two seconds, yellow flashes for a moment, green stays on for two seconds, then it goes back to red through yellow…

LED Control with a Push Button (Push)
Just like a doorbell: the light stays on as long as you press, and goes off when you let go.

HC-SR04 Distance Measurement (Serial Display)
The HC-SR04 sensor works just like a bat finding its way in the dark: it sends out a sound you cannot hear, listens for the echo and measures how far away the object in front of it is.

Temperature and Humidity with DHT11
How warm is your room, and is the air humid or dry? The DHT11 sensor measures temperature and humidity, and the Arduino writes the values on a two-line LCD screen, refreshing every couple of seconds.

Password Lock with 4x4 Keypad and LCD
A small electronic door lock that keeps out anyone who doesn't know the code. You press the hash key and type the code on the keypad; if it is right, the screen says access granted, the green LED…

Smart Electronic Safe
A small safe for your valuables that opens with a code. You type your four-digit code on the keypad; if it is right, the servo motor opens the lock, the green light comes on and the screen shows the…

LED Blink with ESP32
Teaches basic GPIO output usage with ESP32. We achieve a blinking effect by turning an LED on and off from a digital pin. ESP32 version of the classic blink project on Arduino UNO; the basic blink commands in the Arduino environment are the same, but the ESP32's pin layout and 3.3 V GPIO levels differ from those of the Arduino UNO.
More articles
BlueGrays app
Build this circuit step by step on your phone
Wiring diagram, code and parts list on one screen. 500+ Arduino, ESP32, ESP8266, Pico, mBot and micro:bit projects ready to build.
Scan with your phone
Point your camera at the code and your phone's app store opens.
or open the store

