Hello everyone,
I am working on a project of mine. I am trying to send via serial some data from a raspberry pi script to my arduino UNO. Notice that I have tried the following at my linux computer and it is working. However when I run the python script to the raspberry it is not working. As a result i am sure that the problem is related to the Raspberry PI board.
The basic Idea is to use the RPI and send a number or a string or some bytes (eg. 0 for lights off and 1 for lights on). After receiving those bytes in the arduino, it controls a relay to open or close the circuit. Quite simple. But it is not working. All I have done is to randomly open or close or blink the lights. I have no Idea what arduino is receiving. Any ideas? How should I continue? Do I need hardware interrupts? What exactly is going on my arduino?
ADD: When I serial write to activate relay it is working, I am deactivating and it is working. Then If I try to activate again it activates for 1 sec and deactivates... I mean that it is working for only once and then it just activate the relay for 1 sec only. (in case it helps)
I have connected my arduino and my rpi with a simple USB cable. I am not using TX/Rx. Also, The pins I use ar 2, 3.
Arduino code:
#define relayPin1 2
#define relayPin2 3
void setup() {
Serial.begin (9600);
pinMode(relayPin2, OUTPUT);
pinMode(relayPin1, OUTPUT);
}
void loop() {
if (Serial.available()) {
String x = Serial.readString();
if(x == "7") {
Serial.println('0');
delay(10);
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
}else if(x == "8") {
Serial.println('0');
delay(10);
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, HIGH);
}
delay(10);
}
}
thank you for your time,
Vasilis