Serial communication Arduino to Raspberry Pi generates some random characters after a few seconds

For my project, I need a Raspberry Pi to communicate with several peripheral components, two of which are Arduinos. The one causing my problem is the Pro Mini 3.3V ATmega328. The Arduino receives input from two sensors and transmits the data to the Raspberry Pi via a serial port. The serial package python code is used on Raspberry which makes a connection every 50ms.

When the raspberry tab is printed, the first few lines are correct, but after about two to three seconds the printed lines are random characters.

My Python code looks like this:

import serial
ser = serial.Serial('/dev/ttyS0', 115200, timeout=1)
if ser.isOpen():
    ser.close()

ser.open()

...

# loop
try:
        ser.write("1")            
        ser_line = ser.readline()
        print ser_line
...

      

Arduino code:

#include <Wire.h>
#include "SparkFunHTU21D.h"
#include <FDC1004.h>

FDC1004 fdc(FDC1004_400HZ);
HTU21D myHumidity;

int capdac = 0;

void setup() {
    Wire.begin();
    Serial.begin(115200);
    myHumidity.begin();
}


void loop() {
    String dataString = "";

    dataString += String(myHumidity.readHumidity());
    dataString += "  ";
    dataString += String(myHumidity.readTemperature());
    dataString += "  ";

    for (uint8_t i = 0; i < 4; i++){        
        uint8_t measurement = 0;
        uint8_t channel = i;

        fdc.configureMeasurementSingle(measurement, channel, capdac);
        fdc.triggerSingleMeasurement(measurement, FDC1004_400HZ);
        //wait for completion
        delay(15);

        uint16_t value[2];
        if (! fdc.readMeasurement(measurement, value)) {
            int16_t msb = (int16_t) value[0];
            int32_t capacitance = ((int32_t) 457) * ((int32_t) msb); 

            capacitance /= 1000; //in femtofarads
            capacitance += ((int32_t) 3028) * ((int32_t) capdac);

            dataString += String(capacitance);
            dataString += "  ";

            int16_t upper_bound = 0x4000;
            int16_t lower_bound = -1 * upper_bound;

            if (msb > upper_bound) {
                if (capdac < FDC1004_CAPDAC_MAX) 
                    capdac++;

            } else if (msb < lower_bound && capdac > 0) {
                capdac--;            
            }
        }
    }

    Serial.println(dataString);    
    delay(20);        // delay in between reads for stability
}

      

The output of this loop looks like this:

print output

So the output loses precision and becomes a random character after about six lines and the output is not recovered. When I print the serial output on the Arduino serial monitor, the output stays correct all the time. After a few tests, I'm running out of ideas. Does anyone have a solution to this problem or is experiencing similar behavior?

+3


source to share





All Articles