Hello. Beginner here looking for info. I’m looking to run a relay module from a nano. Is it possible to have a pins state switch from low to high with switching the nanos power on and off? I have 2 audio signals I want to switch between with cycling of the main power. Im limited and can’t add a switch or other control. Or perhaps there’s another method without using the relay? Any help is appreciated.
yo tambien soy principiante, pero creo que si ya que al apagar y volver a conectar el arduino nano, el codigo sigue guardado, no se esta bien pero espero que te sirva de ayuda
Why not use the power to the Nano, to also drive the relay,
No pins required.
Do you mean that each time you power the Nano, you want the pin to alternate—starting HIGH on the first power-up, then LOW on the next, and continuing to switch between HIGH and LOW with each subsequent power cycle?
To achieve that, the Nano needs to remember its last state across power cycles, which requires some form of persistent memory. The good news is that the Nano has built-in EEPROM that can store this information.
a simple sketch could be
#include <EEPROM.h>
const byte relayPin = 7;
const int eepromAddress = 0;
void setup() {
pinMode(relayPin, OUTPUT);
byte lastState = EEPROM.read(eepromAddress); // read the state we were last time
byte newState = lastState == HIGH ? LOW : HIGH; // take the opposite state
EEPROM.write(eepromAddress, newState); // write it in EEPROM for next time
digitalWrite(relayPin, newState); // set the relay's pin to that state
}
void loop() {}
EEPROM cells can be written up to 100,000 times, so you should be good for a few decades if you switch it on and off a few times a day. When it starts failing, change eepromAddress to 1 and you'll be running for another few decades, then go to 2, 3, 4... (up to 1023 - should be enough for a lifetime). ![]()
Please use the English language in the English sections of the forum.
Translation of the quoted text:
Why can't you use a switch?
The answer is NO.
The problem is that when you switch off the power with a relay, then that relay will not stay on for the minimum reset time.
This is because all the outputs will revert to being inputs and thus the relay will drop out, way before the minimum reset pulse has been produced.
If you want to try and get round this fact, you could try using a latching relay. But I am not sure that this would actually work.
This is because you will probably get what is called a "race" condition. That is you have set the relay to turn off the Nano and when the circuit is powered up again the latching really is set to turn off the power, which it does.
If you use J-M-L's code from post #4 then all of a sudden you do have have a switch/control.
The Nano's reset button becomes your switch, you don't need to turn off the power.
You need a latching relay to do what you want. A regular relay will always go back to it's default power off state when the power goes off, a latching will always stay in it's last state.
I've deleted some of your posts. If you don't agree please state your disagreement in a polite, respectful way that helps the OP.
Thank you.
The setup is enclosed in a radio chassis with no access. The only controls available are the rotary encoder for tuning and the volume potentiometer. The momentary encoder switch also is unavailable.
Exactly, thanks, I’ll give that a go. Much appreciated!
Then Jim, with the greatest respect, I will ask you could you please explain your thinking as to why you reject my reasoning as in post #7.