How to find what sketch is loaded?

I'm an Arduino novice. Started with a UNO R3 months ago and had lots of help here. Other things intervened and so just getting back into it and having to relearn some basics.

I have half a dozen ATM328 chips onto which I uploaded various sketches during that initial period. Some were moved to run independently on a breadboard. My note-making wasn't as thorough as I intend it to be in future, so I'm unsure what sketches are on what chips. Apart from recognising the sketch when run (like my trivial edit of the default 'Blink'), is there some way to discover the loaded sketch please?

Terry, UK

Normally it is not easy. It requires pretty advanced technique.
Sketch has only what is programmed, except some additional stuff needed for Arduino.
If you are using serial line you can send some message at startup like prog. name and its version.
More advanced is to have some kind of signature, let say a few bytes at fixed address in flash, usually at the end. This type is used e.g. by the OptiBoot bootloader. I think, last two bytes in boot region mean its version. However, you have to know how to read out this information.
There are surely other options. The only limitation is your imagination and knowledge.

Serial.println( FILE );

Place in setup() after Serial.begin(). You can make this the default by editing the BareMinimum example sketch. The IDE uses this as the template for file/new. The DATE & TIME macros are also available.

Thanks both.

@tf68: I'll try that on my next session and onwards. So no way to tell what sketch an existing UNO board is running?

Terrypin:
So no way to tell what sketch an existing UNO board is running?

You would need to compile all of your sketches, saving the hex file from each, then read back what is stored on each UNO and compare it against each hex file until you found a match.

@david_2018: Thanks, that's an interesting technique but I'm uncertain about a couple of points:

  1. Which of these nine (!) files is "...the hex file" ?

  1. How do I "...read back what is stored on each UNO"?

david_2018:
You would need to compile all of your sketches, saving the hex file from each, then read back what is stored on each UNO and compare it against each hex file until you found a match.

Nice and simple. However, not so easy in real life... (especially for beginner)
Did you try it ever?

  • HEX file can have different format so even if the information is the same the file is not; better to compare BIN form (google for hex2bin)
  • downloaded HEX can contain a stuff from the memory behind the sketch; you have to know how to distinguish the end of actual sketch but of course you can compare just number of bytes according to stored sketches.
  1. How do I "...read back what is stored on each UNO"?

Avrdude tool from CMD. The avrdude help, UG or there is a hint:

  1. Switch on detailed log in preferences to see what command for upload is used.
  2. Replace write switch parameter in this command -U with the read one like -U flash:r:...

Budvar10:
Did you try it ever?

Never tried with a sketch, did try it a bit when experimenting with reading back the contents of a board using command line instructions to an arduino-as-isp.

Thanks for all the suggestions. But in view of the apparent complexity I'll forget about identifying the existing chips and make a point of more methodical documentation in future.

Yes, probably this is the best way for the beginning.