Communication between Arduino Mega and Raspberry pi using GPIO tx/rx pins

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!

Mega is 5V board. are pins of RPi 5V tolerant?

Nope, thats where the logic level converter comes into play, it lowers it down to 3.3V

  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
}

23:41:37.131 -> 0

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

Mega:-
5V -> HV (
GND -> GND
RX1 -> HV3
TX1 -> HV4

RPi
3V3 -> LV
GND -> GND
TX -> LV3
RX -> LV4

#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();
  }
}

You want me to try with baud rate as 115200?

this like a standard today

I see, well I changed the baud rate too.
And still no luck. I dont see nothing on my serial monitor

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

15 RX -> LV3
14 TX -> LV4

Updated Wiring, but won't this essentially connect TX to TX and RX to RX?

this serial connection is named UART. read about.

Okay, ill check it out! Still no luck output.. Do you require any other particulars?

how you power mega? from USB of RPi ?

Mega is powered from USB by my laptop.

Ill try buying a new logic level converter tmrw and let you all know

actually you need drop voltage only on wire Mega > RPi and it may be just voltage divider

are you sure it is comunication to mega ?

actually you need drop voltage only on wire Mega > RPi and it may be just voltage divider

Could you elaborate on what you mean? Id like to use the logic level shifter, due to its convenience.