Hi,
This is my first post here, I'm normally programming server software and MacOS applications in swift so I'm not that familiar with the world of Arduino.
I'm trying to establish a serial communication between an Arduino pro mini (ATmega328p) and a Raspberry Pi Zero W V1.1. but somehow the connection "breaks" after a few messages - more infos below.
Arduino to RPI connection:
I connected the Arduino to the Raspberry Pi using a TXS0104E logic level shifter - RX to GPIO 14 and TX to GPIO 15.
The Arduino runs at 5v supplied by the pi.
My software:
For testing the connection I wrote some lines in c++ for the Arduino and in python for the pi.
The pi sends a character "h" or "l" to the Arduino and depending on the letter the Arduino turns the led on or off and returns the letter.
Arduino Code:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
blink(1);
}
void loop() {
String value = "";
while (Serial.available() > 0) {
value += (char) Serial.read();
delay(2);
if (value == "h") { digitalWrite(LED_BUILTIN, HIGH); Serial.println("recieved: " + value); }
else if (value == "l") { digitalWrite(LED_BUILTIN, LOW); Serial.println("recieved: " + value); }
else{}
}
}
void blink(int count) {
for (int i = 0; i <= count; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(200);
}
}
Raspberry Pi Code:
#!/usr/bin/env python
import time
import serial
ser = serial.Serial(
port="/dev/ttyAMA0",
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1,
)
while 1:
ser.write("h".encode())
x = ser.readline()
print(x)
time.sleep(1)
ser.write("l".encode())
x = ser.readline()
print(x)
time.sleep(1)
The issue:
After a few "messages" the connection becomes unstable and this happens:
case 1:
recieved: l // led off
recieved: h // led on
recieved: l
recieved: h
recieved: l
recieved: h
recieved: h
recieved: l
recieved: h
recieved: l
recieved: l
recieved: l
recieved: h
[ ... ]
recieved: h
< at this point the Pi is still transmitting but not receiving anything and the LED is not turning on/off >
[ ... ]
< the Arduino "resets" but the problem remains the same> // it resets but not the way as if you would press the reset button, the leds are blinking etc. but it doesn't affect the running program at all.
[ ... ]
< nothing happens anymore >
case 2:
recieved: l
recieved: h
recieved: l
recieved: h
recieved: h
< Arduino resets >
recieved: l
recieved: h
recieved: l
recieved: h
recieved: l
< Arduino resets >
[ ... ]
My attempts to fix the issue:
- connect the Arduino via USB cable --> the same problem
- connect the Arduino via USB cable & use an other pi program --> the same problem
- use an other pi program (written in swift) --> the same problem
- connect the Arduino to my MacBook ---> no problems
--> might this be a pi related issue?
- testing the same setup with an Arduino Uno --> no problems
- testing the Arduino Uno with the MacBook --> no problems
- testing the same setup with an Arduino Mega --> no problems
- testing the same setup with an other ATmega328p (chip only) --> the same problem
While testing I also noticed, that I couldn't connect to the Arduino pro mini when using an other Baud than 9600.
Of course I could continue using another Arduino/Atmel chip then the ATmega328p but due to power limitations I'd like to keep the chip as small and powerful as possible.
I know that the ATmega328p is quite often used in IoT devices like smart switches etc. and for (3D printer) controller, so I thought it might be a quite "battle" proved chip and I'm really wondering why I can't establish an usable UART connection with the pi.
Is there anything special to consider/configure when I connect it to a Pi?
I'm happy about any solution approaches.
thanks,