Reset via arduino -> arduino serial connection

Hi All,

I am going to have two Arduinos, a mega and Nano connected by serial connection but not accessible (basically part of a model railway set up) The mega i will have remote reset via a momentary push switch, but is there a way to send a 'reset command' from the mega to the nano?

The connection is fine and the serial commands are being sent, it just trying to find away of remote reset via software, rather than adding a switch that will be a pain to get to.

Many thanks in advance.

Instead of resetting it's a neater approach to just have a way of setting the Arduino to it's reset state :slight_smile:

septillion:
Instead of resetting it's a neater approach to just have a way of setting the Arduino to it's reset state :slight_smile:

Sorry, having a senior moment, what do you mean?

Instead of a hardware reset, just restore everything as if a reset happened. Aka, reset variables to there initial sate etc. That way you keep it nice and controlled.

The real question is why you think you need to reset the Nano. Once we know what, exactly, you want to reset, we can offer useful suggests on how to do that.

How ARE the Nano and Mega connected? If they are wired, running another wire for a reset switch seems like the simplest solution.

Connect a wire from the reset pin to any digital pin.

In setup make sure that pin is initialized as an INPUT with PULLUP.

When you want to reset the arduino just change that pin to an OUTPUT and do digitalWrite(nPin, LOW)

Wulla - you can reset your arduino at will.

Thank you all

PaulS:
The real question is why you think you need to reset the Nano. Once we know what, exactly, you want to reset, we can offer useful suggests on how to do that.

How ARE the Nano and Mega connected? If they are wired, running another wire for a reset switch seems like the simplest solution.

If i reset the mega, then it is waiting for a signal from the Nano that it is ready, it does this by sending a single character to the mega.

I could by pass this by sending a character at the end of each loop, but wasn't sure if this was best use of resources?

septillion:
Instead of a hardware reset, just restore everything as if a reset happened. Aka, reset variables to there initial sate etc. That way you keep it nice and controlled.

Is there a way to rerun the setup sequence?

boylesg:
Connect a wire from the reset pin to any digital pin.

In setup make sure that pin is initialized as an INPUT with PULLUP.

When you want to reset the arduino just change that pin to an OUTPUT and do digitalWrite(nPin, LOW)

Wulla - you can reset your arduino at will.

I had read that this was not a good way to do it due to the length of the pulse?

boylesg:
In setup make sure that pin is initialized as an INPUT with PULLUP.

the reset pin has it’s own pull-up. no need to pull-up the control pin. INPUT state is fine

setup();

:wink:

I could by pass this by sending a character at the end of each loop, but wasn't sure if this was best use of resources?

If you mean "at the end of loop()...", say that. Also, clarify if that is at the end of loop() on the Nano or on the Mega. Sending a character, or some other signal, is called a heartbeat, and is quite frequently done, to confirm to one device that the other is still alive.

Is there a way to rerun the setup sequence?

You need clarify what this means. You can call setup() any time you like. The setup() function does not do anything to get the hardware ready. It can get the hardware into a known state (input vs. output, on vs. off, etc.)

The init() function gets the hardware ready, but if the Nano is going to detect that the Mega wants it to reset, the hardware must already be ready, so calling init() again seems useless.

You STILL have not told us how the Arduinos are connected or WHY you want to reset the Nano.

I had read that this was not a good way to do it due to the length of the pulse?

Is this a question or a statement? The length of what pulse?

What do you think the reset switch on the Arduino does?

PaulS:
If you mean “at the end of loop()…”, say that. Also, clarify if that is at the end of loop() on the Nano or on the Mega. Sending a character, or some other signal, is called a heartbeat, and is quite frequently done, to confirm to one device that the other is still alive.
You need clarify what this means. You can call setup() any time you like. The setup() function does not do anything to get the hardware ready. It can get the hardware into a known state (input vs. output, on vs. off, etc.)

The init() function gets the hardware ready, but if the Nano is going to detect that the Mega wants it to reset, the hardware must already be ready, so calling init() again seems useless.

You STILL have not told us how the Arduinos are connected or WHY you want to reset the Nano.
Is this a question or a statement? The length of what pulse?

What do you think the reset switch on the Arduino does?

My apologies, let me take each of your points:
Yes at the end of loop() on the Nano.

I was unaware that you could call setup() in the same way as any other function, thank you to septillion for explaining this.

I believe i did explain, but maybe not clearly enough:

“If i reset the mega, then it is waiting for a signal from the Nano that it is ready, it does this by sending a single character to the mega” when it has completed the setup() function so once set up does not send this again

Regarding the connection, again maybe not clearly enough:
“I am going to have two Arduinos, a mega and Nano connected by serial connection” using the tx1 and rx1 pins on the mega to the tx and rx pins on the nano using 2pins of a 22way connector.

The reset using digitalwrite had a number of negative comments on forums as i have been researching this.

Again my apologies for not being clear enough

Regarding the connection, again maybe not clearly enough:
"I am going to have two Arduinos, a mega and Nano connected by serial connection" using the tx1 and rx1 pins on the mega to the tx and rx pins on the nano using 2pins of a 22way connector.

That STILL doesn't answer the question. I can have a Mega with an XBee shield connected to the RX and TX pins, and a Nano with XBee shield connected to the RX and TX pins, and send data between them wirelessly. Adding another wire, in this case, isn't sufficient to be able to remotely reset the Nano. You'd need to wires, and that would defeat the purpose of using XBees.

So, wired or wireless is the real question. The "22way connector" suggests wired, but please confirm that.

The reset using digitalwrite had a number of negative comments on forums as i have been researching this.

But, connecting two wires in parallel with the reset switch does not. If you have wired connections between the Mega and the Nano, adding two more wires, to add a remote reset switch is probably the simplest way to remotely reset the Nano.

PaulS:
That STILL doesn't answer the question. I can have a Mega with an XBee shield connected to the RX and TX pins, and a Nano with XBee shield connected to the RX and TX pins, and send data between them wirelessly. Adding another wire, in this case, isn't sufficient to be able to remotely reset the Nano. You'd need to wires, and that would defeat the purpose of using XBees.

So, wired or wireless is the real question. The "22way connector" suggests wired, but please confirm that.
But, connecting two wires in parallel with the reset switch does not. If you have wired connections between the Mega and the Nano, adding two more wires, to add a remote reset switch is probably the simplest way to remotely reset the Nano.

Yes via 2 wires on a 22way connector, that then uses a parallel lead to connect the two boards.

The original response that i was replying to suggested that the connection between the digital pin and reset pin was both to be on the Nano as my original question was a way to send a command via serial wired connection.

a search on this forum suggested that this was not a good idea:

I've read in a AVR datasheet that this method is not recommended as it has fundamental flaws. One of the first things the reset cycle does is set all I/O pins to inputs, therefore removing the reset signal too quickly for a complete reset cycle. If ATMEL says don't try to do it this way, then it probably is a poor choice.

I am already using all 22pins on the serial connector so running a connection back is not possible.

JonMiles:
The reset using digitalwrite had a number of negative comments on forums as i have been researching this.

it is no technical. it is a holy grail to run a microcontroller forever without a reset. reset is sign of weak :slight_smile:

EDIT: it is technical if you read about self reset method using own gpio pin of the mcu, but it is not your case
self reset for avr:

wdt_enable(WDTO_15MS);

It appears that the only purpose of resetting the Nano is to make it say "Hey, I'm here and ready to implement your fondest wishes" again. Just make it say that at the end of loop(), every time, or every 5 seconds, or every week, or whatever interval seems appropriate.

See the blink without delay example. Instead of toggling an LED, you send a heartbeat (message). It doesn't, of course, need to be as long as my tongue-in-cheek suggestion above.

I think the hello wave at the end of each loop() is going to be the easiest to implement, the Nano only controls a few relays.

Thank you all for all your help

Juraj:
self reset for avr:

wdt_enable(WDTO_15MS);

That will cause the Nano (assuming it has the stock bootloader) to go into an endless reset loop:

pert:
That will cause the Nano (assuming it has the stock bootloader) to go into an endless reset loop:
https://github.com/arduino/Arduino/issues/4492

good to know, but just today I flashed ‘Uno’ bootloader to my Nano clone :slight_smile: