Arduino to PC OK, Arduino to RPI NOK

Hey all,

Not sure if I'm posting this in the right section but I think it is.

I'm trying to make communication between my raspberry pi and arduino

Whenever I upload my sketch with my computer all my 4 buttons and 4 relays work just fine.

BUT

When I unplug my usb cable from PC to Raspberry PI 2 my relays starts flickering.

Any ideas?

Basicly I copied the sketch from another user on this forum topic #3 with the same connections and resistors(10K)
I just added some lines for communication over serial

This is my sketch:

//Buttons
int button1 = 7;
int button2 = 6;
int button3 = 4;
int button4 = 2;


//Relays
int rl1 = 13;
int rl2 = 12;
int rl3 = 11;
int rl4 = 8;


//States for Relay and Button (1)
int state1 = HIGH;      // the current state of the output pin
int reading1;           // the current reading from the input pin
int previous1 = LOW;    // the previous reading from the input pin

//States for Relay and Button (2)
int state2 = HIGH;      // the current state of the output pin
int reading2;           // the current reading from the input pin
int previous2 = LOW;    // the previous reading from the input pin

//States for Relay and Button (3)
int state3 = HIGH;      // the current state of the output pin
int reading3;           // the current reading from the input pin
int previous3 = LOW;    // the previous reading from the input pin

//States for Relay and Button (4)
int state4 = HIGH;      // the current state of the output pin
int reading4;           // the current reading from the input pin
int previous4 = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time1 = 0;          // the last time the output pin was toggled
long time2 = 0;
long time3 = 0;
long time4 = 0;

long debounce1 = 200;   // the debounce time, increase if the output flickers
long debounce2 = 200;
long debounce3 = 200;
long debounce4 = 200;


String inkomend;  // String for incoming message via serial

void setup()
{
 Serial.begin(9600);   // serial communication
 delay(2000);
 
 pinMode(button1, INPUT);
 pinMode(button2, INPUT);
 pinMode(button3, INPUT);
 pinMode(button4, INPUT);

 pinMode(rl1, OUTPUT);
 pinMode(rl2, OUTPUT);
 pinMode(rl3, OUTPUT);
 pinMode(rl4, OUTPUT);

}

void loop() {


 // If incoming message from RPI over serial equals to
  if (Serial.available()) {
   inkomend = Serial.readStringUntil('\n');
   
   if(inkomend.equals("Relay1 OFF")){
   state1 = LOW;
   }
   else if(inkomend.equals("Relay1 ON")){
   state1 = HIGH;
   }
   else if(inkomend.equals("Relay2 OFF")){
   state1 = LOW;
   }
   else if(inkomend.equals("Relay2 ON")){
   state1 = HIGH;
   }
   else if(inkomend.equals("Relay3 OFF")){
   state1 = LOW;
   }
   else if(inkomend.equals("Relay3 ON")){
   state1 = HIGH;
   }
   else if(inkomend.equals("Relay4 OFF")){
   state1 = LOW;
   }
   else if(inkomend.equals("Relay4 ON")){
   state1 = HIGH;
   }
   else{
   Serial.println("Invalid command by raspberry");
   }
  }

 reading1 = digitalRead(button1);
 reading2 = digitalRead(button2);
 reading3 = digitalRead(button3);
 reading4 = digitalRead(button4);


 // if the input just went from LOW and HIGH and we've waited long enough
 // to ignore any noise on the circuit, toggle the output pin and remember
 // the time
 //Condition Relay 1
 if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
   if (state1 == HIGH) {
     Serial.println("Relay1 OFF door button1");   // Write serial relay 1 is off
     state1 = LOW;
   }
   else {
     Serial.println("Relay1 ON door button1");   // Write serial relay 1 is on
     state1 = HIGH;
   }

   time1 = millis();    
 }

 //Condition Relay 2
   if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce2) {
   if (state2 == HIGH) {
     Serial.println("Relay2 OFF door button2");   // Write serial relay 2 is off
     state2 = LOW;
   }
   else {
     Serial.println("Relay2 ON door button2");   // // Write serial relay 2 is on
     state2 = HIGH;
   }

   time2 = millis();    
 }

 //Condition Relay 3
   if (reading3 == HIGH && previous3 == LOW && millis() - time3 > debounce3) {
   if (state3 == HIGH) {
     Serial.println("Relay3 OFF door button3");   // Write serial relay 3 is off
     state3 = LOW;
   }
   else {
     Serial.println("Relay3 ON door button3");   // Write serial relay 3 is on
     state3 = HIGH;
   }

   time3 = millis();    
 }

 //Condition Relay 4
   if (reading4 == HIGH && previous4 == LOW && millis() - time4 > debounce4) {
   if (state4 == HIGH) {
     Serial.println("Relay4 OFF door button4");   // Write serial relay 4 is off
     state4 = LOW;
   }
   else {
     Serial.println("Relay4 ON door button4");   // Write serial relay 4 is on
     state4 = HIGH;
   }

   time4 = millis();    
 }

 digitalWrite(rl1, state1);
 digitalWrite(rl2, state2);
 digitalWrite(rl3, state3);
 digitalWrite(rl4, state4);

 previous1 = reading1;
 previous2 = reading2;
 previous3 = reading3;
 previous4 = reading4;
}

Please use code tags for attaching code. The symbols are </>, upper left corner in the Quick Reply menu…
I quote “unplug my usb cable from PC to Raspberry PI 2”. What is powering the Raspb then? Flickering lights indicate serious powering problems.

Attaching a drwaing showing the logical build up, connections etc. would be helpful.

Railroader:
Please use code tags for attaching code. The symbols are </>, upper left corner in the Quick Reply menu…
I quote “unplug my usb cable from PC to Raspberry PI 2”. What is powering the Raspb then? Flickering lights indicate serious powering problems.

Attaching a drwaing showing the logical build up, connections etc. would be helpful.

Hey,

Think you misunderstood

I’m trying to activate my relay1 by button 1, relay2 by button 2 and so on…

If the arduino is powered by my computer USB this works just fine

But if i power the arduino by my raspberry 2 USB the relays starts flickering (not pushing buttons)

Greetings

"Power the Arduino by the Raspb"……. How do You do it? What is the capacity of the Rbp to supply current? Have You checked the specs?
I really hope the realays are powered by a supply of their own.

Can You supply an overwiew of Your build up? Arduino here, Raspb there, realys, powers.... I can't get hold of it this way.

Good, the relay have a good powersupply.

I try again. Your "Shakespeare like drawings" are hard to understand.

When You plug the USB to the Raspb relays go bananas. Are they controlled by the Arduino? If so, what is powering the Arduino in this situation?

Railroader:
Good, the relay have a good powersupply.

I try again. Your "Shakespeare like drawings" are hard to understand.

When You plug the USB to the Raspb relays go bananas. Are they controlled by the Arduino? If so, what is powering the Arduino in this situation?

The USB port of my raspberry pi is powering my arduino
And my 4 buttons connected to arduino Should toggle the relays

The RPi cannot tolerate 5V input. What are you using for level shifters?

Also, there may be a power problem. The Arduino cannot power motors or relays.

Thanks jremington. The question just stepped out of my knowledge area.

One answer from Op is missing here. The relays have a separate pwr supply, I was told a short time ago..

I connect arduino to my pc, upload the sketch, test my buttons connected to arduino and the relays Which are isolated but also connected to arduino work fine.

I connect arduino to the USB port of raspberry, the arduino powers on and my relays go on and off all the time without pushing buttons.

So the only difference is USB from pc and USB from raspberry pi.

What causes this?

Pay a lot of attention to the reply from jremington! Arduino versus Raspb…. Different voltage levels, 5 versus 3.3 volt.

I connect arduino to the USB port of raspberry, the arduino powers on and my relays go on and off all the time without pushing buttons.

You are somehow overloading the 5V output of the RPi USB port.

Use a separate power supply for the relays.

What causes this?

Something you are not telling us about.

What is the current capacity of the thing powering the Raspberry Pi?

When you get phantom button pushes it is normally due to a missing ground or a floating input, so check for that. Do you know how to do this?

Have you tried replacing the relays with just LEDs and resistors to make sure it is not a power issue with the Pi.

If everything is fine then fundamentally what you are trying to do is OK and will work. But there is about a dozen ways of screwing it up.

Railroader:
Pay a lot of attention to the reply from jremington! Arduino versus Raspb…. Different voltage levels, 5 versus 3.3 volt.

That should not be an issue with only connecting the Pi through the USB port.

Yes, USB ought to be USB wherever it appears. Standard. Thanks!

Google found this quote for me:

"The USB ports on a Raspberry Pi have a design loading of 100mA each - sufficient to drive "low-power" devices such as mice and keyboards. Devices such as WiFi adapters, USB hard drives, USB pen drives all consume much more current and should be powered from an external hub with its own power supply. While it is possible to plug a 500mA device into a Pi and have it work with a sufficiently powerful supply, reliable operation is not guaranteed.".

Paul

I Made a drawing of situation

I also tried a USB hub with psu but didnt Work either

Thanks for help

Why nou use in built in pullups of the Arduino using “INPUT_PULLUP”? Not the cause of the problem.
Where are the relays connected?
Note the remark by Paul_KD7HB!

Railroader:
Why nou use in built in pullups of the Arduino using "INPUT_PULLUP"? Not the cause of the problem.
Where are the relays connected?
Note the remark by Paul_KD7HB!

What do you Mean where are the relays connected? Did you check my drawings? Everything is connected to the arduino and the relay coils are connected to another power supply

You need to connect the ground of your relay’s power supply to the ground of your Arduino.