I've been trying to create a shutdown for my Raspberry Pi. On the Pi, the UART TX pin is HIGH, until a software shutdown is performed, at which point that pin goes LOW. My code has 2 parts:
Wait for an IR signal to switch a relay on controlling the Pi.
Wait for the UART TX Pin on the Pi to go LOW, and then switch the relay off.
The first part works perfectly, every time. The second part, doesn't seem to be doing anything at all.
#include <IRremote.h>
#include <IRremoteInt.h>
// initialize components and variables
int RECV_PIN = 3; // IR receiver pin
int gpioPin = 7; // Pi UART pin
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long StoredCode = 0x3CE8AD6;
int relayPin = 5; // Relay pin
int outputState = 0;
long lastgpioCheck = 0;
long gpiocheckDelay = 1000;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
digitalWrite(relayPin, HIGH);
pinMode(relayPin, OUTPUT);
pinMode(gpioPin, INPUT);
}
void loop()
{
// start power on signal check
if ((irrecv.decode(&results)) && (outputState == 0))
{
if(StoredCode == (results.value))
{
Serial.print("Proper IR signal received. Switching relay on.");
digitalWrite(relayPin, LOW);
outputState = 1;
}
irrecv.resume();
}
// end power on signal check
// start pi power off check
if ((digitalRead(gpioPin) == LOW) && (outputState == 1))
{
if ( (millis() - lastgpioCheck) > gpiocheckDelay ) // Only run after gpio LOW for 1000ms
{
Serial.print("Raspberry Pi powered down. Switching relay off.");
digitalWrite(relayPin, HIGH);
outputState = 0;
lastgpioCheck = millis(); // Reset count
}
}
else
{
lastgpioCheck = millis();
}
// end pi power off check
}
If the digitalWrites look backward, it's apparently because this relay is "active low". At least that's what I read. The last relay I used wasn't like this.
So is there some glaring mistake that I am just missing?
It doesn't seem to be the same problem. This code worked perfectly before, but I made a stupid mistake and the relay got shorted and killed it. I ordered another relay, hooked it up, and now this. I have removed sections of code over and over until I've seen that the arduino basically isn't seeing when the pin goes low.
Is it possible that when the first relay got fried it messed up the arduino only partially?
Well it looks like you are trying to drive the relay directly from an arduino. Is that the case?
If so the there are very few relays you can drive directly without damaging the arduino.
On the Pi, the UART TX pin is HIGH, until a software shutdown is performed, at which point that pin goes LOW.
This is where you are going wrong. An unpowered output pin does not go low, it presents a diode drop to ground. In other words your input is floating and can read anything. That is why you think the read is being ignored.
Grumpy_Mike:
Well it looks like you are trying to drive the relay directly from an arduino. Is that the case?
If so the there are very few relays you can drive directly without damaging the arduino.
On the Pi, the UART TX pin is HIGH, until a software shutdown is performed, at which point that pin goes LOW.
This is where you are going wrong. An unpowered output pin does not go low, it presents a diode drop to ground. In other words your input is floating and can read anything. That is why you think the read is being ignored.
No the only direct connection between the arduino and the relay is pin 5 to control it.
I think I see what you are saying... It was my understanding that when a pin is set as input, it will read LOW with less than 2 volts(or something like that) applied to it, or HIGH if more than that is applied to it. That's what I was hoping for. When the Pi's UART TX pin is high it is outputting a steady 3.3V, and that drops to nothing when it goes LOW. I guess that is wrong though. Any way I can make this work?
Put the output from the Pi through a transistor. Base through a resistor to the Pi, emitter to ground, collector to the arduino's input.
Then enable the internal pull up resistors.