How do I reset and clear the sketch in a UNO

If I download a sketch onto my Uno it runs.
When I press the reset button the sketch re-initialises and starts again.
If I plug in the USB cable while holding the reset button down the sketch starts again when the button is released.

I would like to reset the Uno so that it has no program running and is just sitting waiting for a new sketch to be downloaded, how can I do this?

I would like to do it so that when I am finished with a sketch I can delete it. Otherwise if I build a new circuit when I plug the Uno in the old sketch starts running on the new circuit - which I don't like.

Also I am starting to use the Serial Monitor and, though it has not happened yet, I can see that I might write a sketch that messes up the serial link preventing me from doing a download.

Are there any built-in commands that can be sent to the Uno via the Serial Monitor e.g. to stop or delete a sketch?

1 Like

Just upload the following null sketch:

void setup() {}
void loop() {}

That will erase the previous sketch.

1 Like

Just upload the following null sketch:

I had thought about that, perhaps with a delay in the loop function.
It would work for the case where I don't want an old sketch trying to use a new circuit.

However it does not feel like the right solution, and it will not work in a situation where I have a sketch going crazy messing with the Serial and so preventing downloads will it?

radman:
I had thought about that, perhaps with a delay in the loop function.

No need.

However it does not feel like the right solution

In what way is it not right?

and it will not work in a situation where I have a sketch going crazy messing with the Serial and so preventing downloads will it?

You have asked for solutions to two separate problems. @johnwasser provided a solution to one of those problems.

The second problem ("serial flood") is typically solved with timing, luck, and the reset button.

To me it is one question, how to stop the current program from executing, with two reasons for wanting to do it.

The reason the null program felt wrong was it only solved one reason.
Killing the serial flood by crossing fingers also seems a bit hit and miss.

However if you advise those are the best ways available to achieve what I want then that's what I'll do.

Thanks.

radman:
Killing the serial flood by crossing fingers also seems a bit hit and miss.

It is. Fortunately, the problem appears to be very rare.

However if you advise those are the best ways available to achieve what I want then that's what I'll do.

For the "serial flood" problem, there really is not a best way. At this point in time, there are two potential solutions: re-burn the bootloader and timing-luck-reset.

Thanks.

You are welcome.

You could always put something in your void setup() so that the program sat there waiting to see a button press before advancing into void loop ().
Or go into power down sleep mode when your program 'ends', wake up with a hardware interrupt, or a reset.

In the case of serial flooding:

Hold down the Reset button.
Unplug the USB cable.
Plug in the USB cable.
Click on the upload button for the sketch you want to upload.
When you see "Binary sketch size:" in the sketch window, release the Reset button.

1 Like

radman:
I would like to reset the Uno so that it has no program running and is just sitting waiting for a new sketch to be downloaded, how can I do this?

One method I forgot to mention: Use an ISP programmer to burn a fresh bootloader. This will clear the contents of the chip (and EEPROM, too, I think) and then install and protect a new Arduino bootloader. The Arduino then has no sketch to run and will sit waiting for an upload whenever it has power.

(and EEPROM, too, I think)

While AVRDUDE can certainly read/write/erase EEPROM, I don't believe the arduino IDE when burning bootloader does anything to the EEPROM on the chip.

Lefty

Thanks everybody. I'll read up on the bootloader, though I don't think I'll give burning one a try just yet.

AVR ISP programmers are not much money either
I have both of these, haven't used the first one yet.
http://store.nkcelectronics.com/pocket-avr-programmer.html

I have used this one to program my Bobuino's
http://www.mdfly.com/index.php?main_page=product_info&cPath=5&products_id=415

thank you for this help,
i can reset my code in esp8266

void setup() {}
void loop() {}

Im having the same issue kind of with an Arduino Uno, I was learning the board and decided to use the 0 digital serial port to change a sketch for LED's, after the upload I can no longer access the port to upload sketches. So of course I find out afterwards that the RX/TX ports are used for communication. How do I go about clearing the sketch so the serial port shows up again in Arduino. I hate learning things the hard way, but I'm wired to love it for some reason too. lol

@dirtabd

Which board?

In general, disconnect whatever is connected to pins 0 and 1.