Hey everyone
Im working on bi-directional communication between raspberry pi and Arduino Mega. So first I conducted a test between uni-directional communication from arduino to rpi which worked okay but with 1 catch, it did not work when I had the conditonal
if (Serial1.available() > 0)
Now a point to note would be that I am using a cable from arduino to my laptop which monitors the serial monitor and the pins 18/19 (TX1/RX1) are used between raspberrypi and arduino and a level shifter is between them, using a multimeter i have verified its working.
The issue is that serial.available is always returning zero when i try to establish communication between rpi and arduino
I had two available ports on my rpi /dev/ttyS0 and /dev/ttyAMA0. I was using S0 earlier but then i disabled bluetooth on my pi and found that S0 wont work so i tried with AMA0 still no luck
I am using GPIO pins because it fits my use case, i dont have much space for this cable for the project im working on
Attaching the code below
RPI :-
import serial
import time
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
while True:
ser.write(b'Hello from Raspberry Pi\n')
print("Message sent")
time.sleep(1)
Arduino :-
void setup() {
Serial.begin(9600); // For debugging with Serial Monitor
Serial1.begin(9600); // For communication with Raspberry Pi
Serial.flush();
Serial.println(Serial.available());
}
void loop() {
if (Serial.available() > 0) {
Serial.flush();
char data = Serial1.read();
Serial.print("Received from Raspberry Pi: ");
Serial.println(data);
}
Serial.println(Serial1.available());
Serial.println(Serial.available());
// Send data to Serial Monitor for debugging
Serial.println("Arduino is alive");
delay(1000);
}
If you have any more questions please ask! Any help is appreciated
Cheers!
if (Serial1.available() > 0) {
char data = Serial1.read();
for convenience you may use lifehack:
#define RPi Serial1
void setup() {
Serial.begin(115200); // For debugging with Serial Monitor
RPi.begin(115200); // For communication with Raspberry Pi
}
This is exactly the issue, the serial is not "available", not sure how I should be debugging this
This is my pin arrangement with my logic level converter
#define RPi Serial1
void setup() {
Serial.begin(115200); // For debugging with Serial Monitor
RPi.begin(115200); // For communication with Raspberry Pi
}
void loop() {
if (RPi.available() > 0) {
Serial.println(RPi.readString());
while(RPi.available() > 0)RPi.read();
Serial.flush();
}
}
Id like to again emphasize on the fact that when i explicity removed the conditional which checked if serial port is available while testing communication from mega to RPi, the messages somehow went through?! but its not happening the other way around