Problems while Raspberry Pi 3B+ is sending data via UART to Pro Mini

Hello,

I'm trying to solve my problem with lot of topics in this and other forums.

I hope anyone yould give me the missing and solving tip.

I'm sending data from the RPi with UART to my Arduino Pro Mini.

The test on the RPI (connecting TX to RX) is successfull and shows the echo.

Just for testing i have written the following code in python on RPi:

import serial

ser = serial.Serial('/dev/ttyAMA0', 9600)
ser.write(b'1')
#print(ser.read())
ser.close

This works.

But I am not able to get the data from UART on my arduino.

I tried lot of code parts i've found. Nothing helps.
Tried it with codes like this:

int incomingByte = 0; // for incoming serial data

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }
}

I connected the RX/TX and TX/RX.
Builded it up like:
Arduino Pro Mini is connected to my PC with a TTL Converter.
Arduino Pro Mini connected to the RPI with RX/TX, 3,3v and ground.

Hopefully anyone finds a mistake or can help me out with some tips.

best regards,
oskar

You can connect Python to Arduinos a lot easier and more robustly if you use libraries.

Try using pySerialTransfer (pip installable and cross-platform compatible) and SerialTransfer.h (installable through the Arduino IDE’s Libraries Manager). These compatible libraries reliably and automatically packetize and parse serial data transferred between Python and Arduinos.

Example Code:


Example Python:

from pySerialTransfer import pySerialTransfer as txfer

if __name__ == '__main__':
    try:
        link = txfer.SerialTransfer('COM13')
    
        link.txBuff[0] = 'h'
        link.txBuff[1] = 'i'
        link.txBuff[2] = '\n'
        
        link.send(3)
        
        while not link.available():
            if link.status < 0:
                print('ERROR: {}'.format(link.status))
            
        print('Response received:')
        
        response = ''
        for index in range(link.bytesRead):
            response += chr(link.rxBuff[index])
        
        print(response)
        link.close()
        
    except KeyboardInterrupt:
        link.close()

Example Arduino Code:

#include "SerialTransfer.h"
#include <SoftwareSerial.h>


SoftwareSerial mySerial(2, 3); // RX, TX
SerialTransfer myTransfer;


void setup()
{
  Serial.begin(115200);
  mySerial.begin(9600);
  myTransfer.begin(mySerial);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
 
  myTransfer.sendData(3);
  delay(100);

  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

For more info on how to use either library, please see the readme file in the library’s respective GitHub repository.

Oskar92:
I connected the RX/TX and TX/RX.
Builded it up like:
Arduino Pro Mini is connected to my PC with a TTL Converter.
Arduino Pro Mini connected to the RPI with RX/TX, 3,3v and ground.

Are the PC and the Raspberry Pi connected to the Arduino at the same time?
If so, the PC TTL Tx and the Raspberry Pi Tx will be connected via the traces on the circuit board and will fight each other for control of the Arduino Rx line.

It solved my problem @mikb55. I thank you very much. I didn’t think about that at all. Now i connect and disconnect the TTL TX from Arduino RX everytime and get the data nevertheless in the terminal on my computer.

Thanks to you @Power_broker, too!

I hope my english was okay and you had no problems with it. :smiley: