Welcome to another Arduino 101 video.
As always, in these videos we do something more than just basic Arduino programming.
Today we will see something that even myself didn’t know was possible.
Instead of writing a code to the Arduino, we will see how to extract an existing code
from the Arduino and save it in a hexadecimal format, which basically means 1s and 0s.
I will show you how a code is stored or burned to an Arduino and what software we need to
extract it back.
Then I will show you how to write back that extracted code
to the Arduino.
Sounds interesting?
So, let’s get started.
What’s up
my friends, welcome back.
This here is an Arduino, obviously.
But despite how we all call it, Arduino is not a microcontroller.
The Arduino is just the name of the development board which is made out of the PCB, the plastic
pins, the voltage regulators, the connectors and everything that you see on this board.
The real microcontroller is this one here, in this case is the ATmega328p and is made
by a company called ATMEL, or nowadays was acquired by Microchip Technology.
So why am I telling you about this?
Well, because we have to understand how a code is uploaded to a microcontroller and
you have to understand that the microcontroller is not the entire board, just this chip here,
and it could be any other chip.
But basic Arduinos are using ATMEGA chips and these have an AVR structure.
You have to understand that inside such an AVR microcontroller we have the main CPU,
some flash memory, EEPROM memory and SRAM, so we don’t need any external memory for
most applications.
Almost all devices, except the smallest TinyAVR chips such as the ATTiny 85 and so on, have
serial interfaces, which can be used to connect larger serial EEPROMs or flash chips in case
you need more memory.
These external memories come in the form of other chips like this one, which has an i2c
communication in this case.
AVR structure is using 32 registers of 8 bits.
The code that we upload to an Arduino, so the program instructions, are stored in non-volatile
flash memory.
Although the MCUs are 8-bit, each instruction takes one or two 16-bit words.
The size of the program memory is usually indicated in the naming of the device itself,
for example the ATmega64x has 64 KB of flash, while the ATmega32x, which is the one the
Arduino UNO is using, has 32 KB.
For these microcontrollers we have no external program memory, that means that all code executed
by the AVR core must be inside of the chip on its flash memory and that’s the part
we need to extract in this tutorial.
Basically, inside of the Arduino we have the program memory, data memory and the EEPROM,
and we will focus on the program memory.
This memory is separated into the boot flash section and the rest of the code.
We don’t care for now about the boot section.
So, our code will be stored here.
This is an Arduino code.
But when this is programmed to the chip, it is not stored in a text format like this.
The compiler is in charge of translating this text into 0s and 1s, and then store those
0s and 1s to the memory.
For example, this is the binary data for this same blink code in a hexadecimal format.
And this is what we will extract today.
That being said, let's see how to do it.
To be able to see the differences later in the video, between what we upload and what
we extract, I’ve prepared two blink codes.
One blinks each 100ms and the other one blinks each 2 seconds.
I upload the 100ms to an Arduino UNO.
As you can see the LED is blinking very fast.
No go ahead and download this software from below, is called AVR DUDESS.
Install it and run it.
To communicate with the Arduino, we can’t use its USB connector.
We need to create an ISP communication.
These are the pins of an ISP communication on pins 10, 11, 12 and 13.
You could use an ISP programmer, but in case you don’t have one, we can use another Arduino
as a controller.
To make the difference I will use a red Arduino for that.
So this will be the master and this will be the Arduino from which we want to extract
the code we’ve uploaded before.
First, get the master Arduino, connect it to your PC and run the Arduino IDE.
Here, we go to file, examples, Arduino ISP and open this example code.
Select Arduino UNO board, compile and upload this code to the master Arduino.
And just like that we make it act as an ISP programmer.
Now make these connections from the master Arduino to the Arduino with the blink code.
These are the ISP communication pins.
Now get back to AVR DUDESS software.
For programmer, obviously, select Arduino.
Make sure you have the master Arduino USB connected to your PC.
In the com list, select the com of the master Arduino, in my case is this one.
For the baud rate I set it to 19 200 but in your case it might be 115 200 since I’ve
seen some models working at a different speed.
On the right side, for MCU select the ATMega328p because the Arduino UNO is using that chip.
Now click detect, and if the ISP connection between the master Arduino and the code Arduino
is good, you should get this message.
Ok, so as you can see we can access the flash memory where the code is, the EEPROM memory
and the fuses configuration.
Let’s go 1 by 1 starting with the fuses.
These fuses are like switches and they could be 0 or 1 and they basically control the configuration
of the microcontroller.
For Arduino we have the low fuse byte, high fuse byte, extended and the LB fuse.
Low fuses will deal with the clock sources and each bit controls something as you can
see here.
The high fuse bits will control these parts such as the reset, watchdog, bootloader memory
and so on.
So if an Arduino code has changed these bits, you might want to also get the values as well
when you get the code.
So, in the AVR software, click the read button next to the fuse bits and get the values.
There is no way of storing this so you must write down these values, maybe open a notepad
and paste the values there.
Ok, now we have the fuses.
Next, let’s read the code.
Mark the read checkbox for the flash memory.
For the format select intel hex for hexadecimal.
Click the 3 dots and select where you want to save the code file.
I give it a name, for example in my case I name it blink 100ms dot hex, and save it in
this folder.
Now that you have the file root, make sure you have all the settings as I have and click
go.
The code is now extracted from the blue Arduino.
The process is done.
I go to that folder and as you can see I have the hexadecimal file of the code.
These are just 1s and 0s but they contain the blink code.
We do the same for the EEPROM memory.
Mark the read checkbox, click the 3 dots and give it a name.
I name it EEPROM 100ms.eep.
Now click go.
The EEPROM data file is now extracted from the blue Arduino.
So now we have the fuses, the flash data with the code and also the EEPROM, but why do we
need the EEPROM?
Well, some codes might use the EEPROM memory as well.
For example, for my soldering iron project I was storing the user settings on to the
EEPROM, so if you want to copy the entire code, you might also want to extract the EEPROM
memory.
Ok so finally, to demonstrate that the code we’ve extracted works, I take the blue Arduino
and using the Arduino IDE I now upload this code with a blink of 2 seconds this time.
As you can see the LED is now blinking a lot slower.
Connect back the blue Arduino to the master Arduino with the ISP connections.
We get back to the AVR software.
Make sure you have these settings.
In the flash part, we now select the write option.
Select the hexadecimal file we have extracted before from your PC folder.
Mark the erase flash and EEPROM check box.
Click go and now code will be uploaded to the blue Arduino.
Once finished, remove the reset wire or disconnect all the SPI pins and reset the Arduino.
There you go, we now have the 100ms blink instead of the 2 seconds one.
We’ve successfully extracted and programmed back a code from an Arduino.
Back to the AVR software.
If you want to fully copy and upload a code, you must re-upload all the parts.
So, get the fuse values from your notepad file.
Place them here and check the set fuses check box.
Then select the hexadecimal file of the code and check the write checkbox.
Then select the EEPROM file and also check the write checkbox.
Make sure all the settings are correct for the MCU type and speed, check the erase flash
and EEPROM checkbox and click program.
Now, all 3 parts are uploaded to the blue Arduino.
Just like that you’ve copied a code from an Arduino and uploaded it back.
Have in mind that you will only have access to the hexadecimal format of the code.
You won’t be able to edit this code in Arduino IDE for example.
You can only copy it and upload it again, without editing the code.
And you can do this with Arduino UNO, an Arduino NANO or even to an ATmega328 chip that is
soldered to a prototyping board.
All you need is access to the ISP pins.
So, guys, I hope you’ve learned something new.
If so, consider giving me a like or comment below.
Thanks again and see you later guys.