reliably power cycle arduino nano
[Arduino] / power_cycle / power_cycle.ino
1 /*
2  * power cycle switch
3  * 
4  * relay is connected across 5V relay through normally closed pins so that failure of arduino doesn't kill power to switch.
5  * to activate relay on this board, signal pin has to be pulled to ground.and coil draw is 76 mA when active
6  * board has pull up on input pin to it's vcc
7 */
8
9 #define RELAY_PIN 2
10
11 void setup() {
12   Serial.begin(115200);
13
14   pinMode(LED_BUILTIN, OUTPUT);
15
16   pinMode(RELAY_PIN, INPUT); // don't modify pin state
17   Serial.print("Relay pin on reset: ");
18   Serial.println(digitalRead(RELAY_PIN));
19 }
20
21 void loop() {
22   if ( Serial.available() ) {
23     char c = Serial.read();
24     if ( c == '0' ) {
25       Serial.print("L");
26       pinMode(RELAY_PIN, OUTPUT);
27       digitalWrite(RELAY_PIN, LOW); // activate relay
28
29       digitalWrite(LED_BUILTIN, HIGH); // led on
30     } else if ( c == '1' ) {
31       Serial.print("H");
32       pinMode(RELAY_PIN, INPUT);
33
34       digitalWrite(LED_BUILTIN, LOW); // led off
35     } else {
36       Serial.print(c);
37     }
38   }
39 }