Problem with UART communication

Hello there,

I am using the Arduino Pro Mini 3.3V / 8MHz for a project in which I need to transmit some data through the TX pin.

The thing is that when acceding to the transmitted data with the oscilloscope, the data does not correspond at all and also the oscillo is reporting some errors in some of the bytes.

Also, I am using a 2400 Hz baud rate, that is also used when uploading the code since this Arduino version has only one UART port. Could somebody verify if the problem may be on this baud rate? I was thinking that perhaps this version doesn't admit such a low frequency, or maybe neither uploading the code like that.

Thank you in advanced!

So what did you connect to the serial line? How are you measuring? What signals are you expecting and what are you getting?
Just from the top of my head:
Tx/Rx here are TTL, not RS232 which means it's

  1. 0V/3.3V and not +/-12V
  2. It's inverted (on RS232 a negative voltage means 1) which could throw the escilloscope of the track

So please tell a bit more about your setup and how you are measuring. It might also be helpful to post some code (I'm sure you've already created a minimal software to reproduce the error).

best regards, Marco

Hello
Post a circuit diagram with measurement points.

1 Like

Thank you for the kind answers.

Well, my circuit is just an Arduino Pro Mini set up in a circuit like this:

I try to send 19 messages and actually I am using a really simple code.

int msg[19] = {254,254,14,6,100,25,1,110,2,199,42,248,1,24,17,240,0,110,7}; // Message initialization
int aux;
void setup() {
  Serial.begin(2400, SERIAL_8E1);
}

void loop() {
  
  /* Check-sum at byte 19 */
  for (int i = 0; i < 18; i++) {          
    aux = msg[i] - msg[i - 1];
  }
  msg[18] = 256 + (aux % 256);

  /* Sending data */
  for (int j = 0; j < 19; j++) {
    Serial.write(msg[j]);
  }
  delay(160);
}

What happens next is that I receive the data at the oscillo sometimes well and sometimes not. It seems like the data gets corrupted every second and goes back again to normal.

Here you can find the oscillo display:
Here good


Here bad, where first bytes do not correspond to the ones I send in the code.

Finally, this is a picture of the set up, in case you are interested

I'm really concern about the baud rate frequency, could it be that this Arduino version does not support it that low?

Thank you

Hi
From what I see the curves seem quite ok. Also the data looks completely normal to me. What the oscilloscope is decoding exactly fits what you have put in the array (ok, Ive not calculated the last byte in my head but I guess that's ok as well).
So where do you think the data is wrong?

As to why there is a ~70ms break in the transmission: I have no clue! Probably some interrupt handling but 70ms seems awfully long!

Just to note: The PC817A has a transmission ratio of 0.8-1.6 at If = 5mA and Vce = 5V. You might want to further reduce R10 or increase R9 slightly to have some more headroom. Otherwise it might happen, that DATA OSCILLO is not pulled completely to 0V.

best regards, Marco

Thank you so much for your help. In fact I have changed R10 to the half of its value and now it is definitely pulled to 0V.

Well, I am worried about the jump that I have between both oscilloscope screenshots that I posted. Seems like the bytes are changing every second form the one to the other. I would like to have the message that I put in my code with no variation in time... what do you think it could be happening?

Regards!

1 Like

It looks like it may be a problem with the scope trigger. When you get data, they look OK, so the issue is to establish why the first few were dropped.

Do you have another Arduino you could use to receive and echo the data?

1 Like

Seems reasonable. In fact I do have some more boards and I'm gonna try to use another one as receiver.

Thank you for the advice,
Regards!

1 Like
for (int i = 0; i < 18; i++) {          
    aux = msg[i] - msg[i - 1];
  }

You will have problems when i = 0.

1 Like

I will consider that, maybe better to make the sum manually for the first try.

Thanks!

There is a 160mS delay in the code. The code is loading 19 bytes of data into the transmit buffer, which will take about 87mS at 2400 baud. That is being send almost entirely during the delay, which should leave about 73mS delay before the next data.

There should not be too much error with the baud rate, unless the arduino is using the internal oscillator.

1 Like

You could also make the array one longer, set the first byte to zero and work with index 1 as start index. You might also try to use byte instead of int as data type which will save you some memory (int is afaik a signed 16bit integer on arduiono). You can also easily use the same iterator variable for both loops.

Some will say:"do not optimize before it becomes necessary" because every step of optimisation is also a possible way to break something. And they are not wrong. But using the smallest possible data type that does the trick is not optimisation for me but just "how you do it" :slight_smile:
And thats probably a leftover from working with microcontrollers with at most 256 bytes of memory where we had to start to fight for single bits of memory (if you had a 12bit ADC result you could use the leftover 4bit in the second byte for other stuff... which made for "funny" debugging sessions if you somehow accidentally wrote over the wrong data because you got a bitmask wrong).