Based on:


And some previous experience with Arduinos. What I would like to accomplish here is that I have a 8 channel relay board with the relays in different states. The Arduino is connected to a RaspberryPI via USB.

The first problem is well known the automatic reset of the Arduino which can be taken care of with a resistor or desoldering a pin on the Mega board so you can reconnect your Arduino as many time as you wish (while running on an external +5V power source) to the Raspberry and it will never reset.

However I also remember that doing this to the boards will also make them impossible to program because they cannot reset at the end when the sketch is uploaded.

I have think of a workaround:

1, Create a programming command on the arduino which is when it's received from the RPI it will connect that cut off pin with a relay which will make resetting possible again

2, Save all relay states to the EEPROM

3, After reset the Arduino always checks this programming flag in it's EEPROM and if it's present it either does not touch the relays or set them to the same position ON/OFF depending on the value it read back from the EEPROM

The last guy claims that: When the Arduino is in reset, all pins act like they aren't connected to anything. If you have an output which must be in a specific state during that period, you must add an external resistor to pull it up or down as required.

Is there any other solution for this problem? Like using a newer Arduino board which can keep pin states over reprogramming?



You have basically 2 distinct problems here. The problem to somewhat automatically enable/disable the reset from USB and the problem to keep the relays at their set states over reprogramming.

  • Enable/disable reset: The easiest way is to use a jumper, that you disconnect everytime you want to program the board. If you want to do it remotely, you already described a valid way, though I think, that you don't need a relay for that. A transistor or MOSFET should be sufficient for it.

  • Keep the relay states:

    Like using a newer Arduino board which can keep pin states over reprogramming?

    I don't think, that something like that exists. It does not make sense for general purpose microcontrollers to do that; they all start from a clean state. So you need to save the state of the relays outside of the Arduino. What type of device you want to use for that is up to you. Majenko mentioned flipflops, which gives you the most basic functionality for this. You could also use devices, which are interfaces serially, like shift registers or even port expanders. Using flipflops will be the easiest way, since the programming does not change much. There are many examples how to use each of these devices with an Arduino on the web.


I had to solve a problem similar to this for an ESP32 based system. The solution I came up with was to use external D-type latches (flipflops) to store the state independently of the Arduino.

The Arduino has to actively load a new relay state into a latch in order for the relay to change state, and that state is retained regardless of what the Arduino does.

The outputs of the latches can also be connected to other input pins so the Arduino can find the current state of the relays at boot time.


simulate this circuit – Schematic created using CircuitLab

The Arduino keeps track of what the outputs should be (reading the current state at boot) and then changes a state when needed. It then loads all the states onto the output pins and pulses the LOAD pin (CLK on the chip) high to load those states into the latches.

I used the 74HC374 chip for this since it contains 8 latches tied to a single CLK pin.


I use for this purpose (model railroading switching turnouts/signals with relays and coils) a simple and pin saving solution.
An I2C bus shield/module with 16 I/O ports (and its daisy chainable - up to 64 modules theoretically, 16 practically due to address encoding pins)
The 16-channel PCA9685/PCF8574 I/O extender module cost around 4-5 euro and requires beside GND/VCC only two data pins (SDA & SCL) to control the module. The I2C header has extra 10K pullup resistors and onboard of the module is a 25 MHz typical internal oscillator.

So after reseting, I first read the EEPROM/SPIFFS/SD-values and based on that stored set I work on. The Module(s) has(ve) a seperate power source which is not affected by the reset of the Arduino controlling them. The operating range of 2.3V to 5.5V allows the module to be powered from a range of power supplies and also used with 3.3V devices.
The green terminal block in the picture is for use with servos - the extra power supply attaches to the I2C header (but common ground with Arduino).

Of course you have to use a lib (or like me write the functions you need) but once your into it its easy to use and also usable for other scenarios (LEDs, PWM generator, servos).
enter image description here

The PCA9685 is a 'free running' PWM controller. Tell it what to do, and it will keep doing that until you tell it to do something else. If you wanted to set the relays to a specific state and keep them there, you could disconnect the Arduino until you wanted the relays to do something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.