I’m trying to establish UART communication between a Raspberry Pi 4 and an Arduino UNO via TX/RX pins. The goal is to send a signal from the Arduino when a button is pressed and receive it on the Raspberry Pi to control an LED.
I Connected the grounds of both devices, and the TX with RX of the other device, and pluged the Arduino uno to my PC to pwor it and to upload the code from arduino IDE
Here’s the arduino code
int buttonPin = 8;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
Serial.println("1");
} else {
Serial.println("0");
}
delay(100);
}
And here is the raspberry code
import RPi.GPIO as GPIO
import serial
ser = serial.Serial('/dev/ttyS0', 9600)
ledPin = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(ledPin, GPIO.OUT)
while True:
if ser.in_waiting > 0:
data = ser.readline().decode('utf-8').strip()
print(f"Received: {data}")
if data == "1":
GPIO.output(ledPin, GPIO.HIGH)
elif data == "0":
GPIO.output(ledPin, GPIO.LOW)
I’ve already tried everything, I Disabled the login shell over serial and enabled UART hardware On raspi-config
The Arduino works perfectly; it sends “1” when the button is pressed and “0” when released, as verified through the serial monitor.
I also tested the Raspberry Pi UART using minicom, but nothing is displayed—neither the data from the Arduino nor even the echo when typing.
Murata san is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.