Arduino pro mini & Raspberry Pi Zero UART connection issues

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,

I got same Problem. Should write Serial.begin (9300); instead of 9600.

You use different coding environment/dialects for the Arduino pro mini and the Raspberry Pi.

The Arduino code doesn't look like the entire code. Please post the entire code. Also, please use the code tags, 6:th symbol, </>, in this window.
For the Arduino using strings can be bad. The garbage collection of given back memory is worse than poor and results in crashes after some time.

The Pi code I'm comfortable to analyze at this hour as it's outside my usual territory.

Ok thanks for the hint, do you know if it runs stable at Baud 9300 or will it shift over time ?

first i had to reduce communication speed from 115200 to 300 baud and it was significant longer connected. then i used logic analyzer to examine the signals and i saw Mini made shorter impulses as RasPi made. i made calculation and 9300 was result.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.