Relay refusing to stay low

I’ve been working on this project off and on for quite some time, and I had it nailed down a while back, but now a new annoying thing has cropped up. So an Arduino(actually a 328P) waits for an IR signal, and sends the relay high. Simple enough. Then it watches GPIO8 on the Pi. When it goes low, the relay waits 2 seconds, and then goes low. Also, simple enough it would seem.

The problem is, it works perfectly, UNTIL I connect the hot wire through the output side of the relay. Once the Pi’s power source runs through the relay, when the Pi pin goes low(after shutdown), the relay goes low, and a moment later goes high again. I’ve tried enabling the internal pullup, and also pulling the pin low with a resistor between the pin and ground. Same behavior every time. Here is the code I am using. How is the relay going high without it being expressed in the code?

#include <IRremote.h>
#include <IRremoteInt.h>

// initialize components and variables
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
const long StoredCode = 0x20DFA25D;
const int relayPin =  5;  // Relay pin
const int checkPin =  7; 
int outputState = 0;
long lastgpioCheck = 0;
long gpiocheckDelay = 2000;

void setup() 
{ 
  Serial.begin(9600);
  irrecv.enableIRIn();
  pinMode(relayPin, OUTPUT);
  pinMode(checkPin, INPUT);
  digitalWrite(relayPin, LOW);
}

void loop() 
{
  // start pi power on check
  if ((irrecv.decode(&results)) && (outputState == 0)) { 
    if(StoredCode == (results.value)) {
      Serial.println("Infrared Signal recieved. Switching relay on.");
      digitalWrite(relayPin, HIGH);
      outputState = 1; 
    }
    irrecv.resume(); 
  } 
  // end pi power on check
  
  // start pi power off check
  if ((digitalRead(checkPin) == 0) && (outputState == 1)) 
  {
    if  ( (millis() - lastgpioCheck) > gpiocheckDelay ) // Only run after gpio LOW for 2000ms
    {
      Serial.println("Raspberry Pi powered down. Switching relay off.");
      digitalWrite(relayPin, LOW);
      outputState = 0;
      lastgpioCheck = millis(); // Reset count
    }
  }
  else
  {
    lastgpioCheck = millis();
  }
  // end pi power off check

}

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thankyou....Tom..... :)

Here's something put together quickly in Gimp. If it's nonsense I can make a better one in a bit. Slightly busy at the moment. To keep things simple:

1: Atmega328P,(although the same thing happens with actual Arduino Uno) 2. Arduino 5V relay 3. Raspberry Pi, powered via GPIO pins(although same happens powered over uUSB) 4. IR Reciever

Red lines are hot, black lines are ground.

Arduino pin 5 to relay signal Arduino pin 7 to Pi pin GPIO8 Arduino pin 11 to IR Reciever Out

|500x314

Note the connection of the relay output before the Pi input. If I unscrew those wires and twist them together, the relay goes up and down exactly as it should. It's only when the relay is actually controlling the Pi's power input that it acts wrong.

Could be the phantom power between Arduino and RP. Try a 10k resistor in series with the data line (pin7 to GPIO8). Leo..

Wawa: Could be the phantom power between Arduino and RP. Try a 10k resistor in series with the data line (pin7 to GPIO8). Leo..

No good. Same thing. Perhaps this will help... I upped the time for it to watch the pin before going LOW to 5 seconds. This made no difference. It goes high, and then low the next moment, so it's not getting the signal to go low from my code.

EDIT: I just removed the entire second half of the code, leaving only the section using the IR to turn the relay high. Still does it. Goes high for a second, and right back to low.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a picture of your project.

Tom..... :)

I would leave the 10k resistor in the circuit. Connecting a live MCU directly to a powered down MCU is never a good idea. Leo..

TomGeorge: Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a picture of your project.

Tom..... :)

There's one on post 2, or is it no good?

I just tested with this code:

const int relayPin = 2; 
void setup() 
{ 
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);
}

void loop()
{
      digitalWrite(relayPin, HIGH);
}

The relay just pulses on and off very quickly. I put a serial print line in setup to make sure it's not resetting. Oddly, if the Pi has a separate power source and ground, it works exactly right.

Question. Could this just be the nature of using a relay to switch power on the same source as the relay? Should I really be using a FET, considering it's only switching 5v 2A?

With "relay" I suppose you mean "relay module", not a relay coil connected directly to a pin. Maybe we could see a picture of your setup. Leo..

Wawa: With "relay" I suppose you mean "relay module", not a relay coil connected directly to a pin. Maybe we could see a picture of your setup. Leo..

I can take a picture, but honestly it's a rats nest of way too long wires, and a few that are different colors from having male jumpers spliced to female jumpers. Would be very hard to make anything out. Yes it is one of those "fancy" relay modules, with hot, ground, and signal input.

The picture and description above is exactly how it is put together, not accounting for the capacitors on the uC input, the cap and resistor on the IR, and now the resistor on the data line between uC and Pi.

|500x375

Hi, Nowhere like a circuit diagram. Include pins numbers and what is the relay? How are you powering the project?

Tom.... :)

TomGeorge: Hi, Nowhere like a circuit diagram. Include pins numbers and what is the relay? How are you powering the project?

Tom.... :)

Picture uploaded. I'll see if I can work on an actual circuit diagram shortly. It's powered from a 5v 8A PSU.

EDIT: |500x456

I see a resistor in the wire to the relay board. Why? There is already one on that board. I don't see a 10k in the data line between MCUs. Nor a 10k reset pull-up resistor. Leo..

Wawa: I see a resistor in the wire to the relay board. Why? There is already one on that board. I don't see a 10k in the data line between MCUs. Nor a 10k reset pull-up resistor. Leo..

Damnit, that's what I get for trying to rebreadboard at 2am. The resistors are actually in the right spot when I was working on it. I always need to use the reset pullup resistor, every time? (Assuming this is uC specific since I've never needed to on a full arduino)

Hi,

In the picture the arduino is the chip on the protoboard is it?

Where is the crystal and caps to run it?
How did you program it and how do you know it is working.
Can I suggest in the 328 you program one of the pins to flash an LED to show that the sketch is running.

Forget the fritzy, it in no way resembles the picture of your project.

Actual circuit, yes please, all components including power supply.

Tom… :slight_smile:
Reverse engineering to produce a circuit may find your problem.

TomGeorge: Hi,

In the picture the arduino is the chip on the protoboard is it?

Where is the crystal and caps to run it? How did you program it and how do you know it is working. Can I suggest in the 328 you program one of the pins to flash an LED to show that the sketch is running.

Forget the fritzy, it in no way resembles the picture of your project.

Actual circuit, yes please, all components including power supply.

Tom.... :) Reverse engineering to produce a circuit may find your problem.

The picture is crap. The fritzing is exactly how it is connected right now, and it is still doing it. I know it is working because it responds when I use the remote on the IR reciever. I have the 328P set up for a minimal circuit using internal, so it doesn't need the crystal, but at the moment I'm back to using the UNO to try to figure this out.

Like I said earlier, I literally stripped it down to nothing but arduino, Pi, and relay module, with the only code being to send the relay HIGH. With the Pi not connected to the relay module, it would go high and stay high. Once the Pi was connected to the relay, it would go high for one second, and back low, over and over.

Hi, In the picture, the black wire from the PI, is that the GND wire? It doesn't look like its connected to the GND strip on the protoboard.

Tom.... :)

TomGeorge: Hi, In the picture, the black wire from the PI, is that the GND wire? It doesn't look like its connected to the GND strip on the protoboard.

Tom.... :)

The picture is really no good. I rebreadboarded it quickly to take a picture, and connected a wire wrong. The Fritzing is exactly how I have it connected right now.

At this point I'm thinking there is some strange interaction with the relay. The script works perfectly, until I run the Pi's input through the relay. I have a couple P-channel mosfets on the way. I'm going to see if they play nicer. They would be more convenient anyway.

Hi, What does the monitor print when you have this problem?

Tom..... :)

Hi, Just looking at the sketch,

StoredCode == (results.value)

Does the results.value display the LAST received IR code, and does it stay that code until another code is transmitted.

Can you try sending the ON code, then send another invalid code before the PI output drops.

If results.value is the last code received, it will always be the valid code, so when the relay state changes from 1 back to 0 the

 if ((irrecv.decode(&results)) && (outputState == 0))
  {
    if (StoredCode == (results.value))

set of Ifs will be valid again.

Just a thought.

Tom..... :)