0

I'm sending data to my Raspberry Pi 3 from an Arduino Mega via serial communication, but sometimes the Arduino sends a strange character, in most of the time it sends a mirrored question mark. Why does it send this and how can I eliminate it?

Arduino's code:


bool flag_sensor3 = false;                      //Descarte de logs
bool flag_sensor4 = false;                      //Interfolhadeira

int pino_sensor3 = 24;
int pino_sensor4 = 25;
//---------------------------------------------------------------  SETUP --------------------------------------------------------------
void setup()
{
  Serial.begin(9600);
  pinMode(pino_sensor3, INPUT);
  pinMode(pino_sensor4, INPUT);
}
//---------------------------------------------------------------  LOOP --------------------------------------------------------------
void loop()
{
  //-------- LEITURA SENSOR 3 -------------------------
  if (digitalRead(pino_sensor3) == LOW)
  {
    if (flag_sensor3 == false)
    {
      Serial.println("33");
    }
    flag_sensor3 = true;
  }
  else
  {
    flag_sensor3 = false;
  }

  //-------- LEITURA SENSOR 4 -------------------------
  if (digitalRead(pino_sensor4) == LOW)
  {
    if (flag_sensor4 == false)
    {
      Serial.println("34");
    }
    flag_sensor4 = true;
  }
  else
  {
    flag_sensor4 = false;
  }
}
9
  • Wrong baud rate? Poor connections? Dodgy software? We can't guess. You need to provide useful diagnostic information.
    – joan
    Oct 1 '19 at 13:03
  • What do you mean by "mirrored“ question mark? Do you mean Rpi sends a question mark to ask Mega for data, Mega does not return data but just echo a question mark? I remember that for both Arudino Uno/Mega, Rpi send/receive somethings, it is important to flush the port first. For Rpi python serial, the statements I use are the following: (1) serialPort.flushInput(), (2) serialPort.flushOutput(). If you forget that, then what is left in the buffer, like new lines, or other control codes might send with the good data. The question mark might mean some control character unprintable.
    – tlfong01
    Oct 1 '19 at 13:06
  • I've already checked the baud rate, changed the cable. Apparently it's ok, but when I run the programm it returns a arabic question mark in the first line Oct 1 '19 at 13:09
  • Photos? Software? Data read/written?
    – joan
    Oct 1 '19 at 13:12
  • Hi tlfong01, no, I'm just receiving datas with the rpi, not asking arduino for this. I didn't write this character in any part of my code, but yet this character appears in the Monitor Serial. Oct 1 '19 at 13:13
1

sending datas to my raspberry pi3 from an arduino mega

I'm presuming from the rest of the context this data is in text form, eg., if the value 1234 is part of the data, it is sent as a string of four characters, "1234".

In case that's confusing, the alternative to this is sending data in binary form, eg., if 1234 is a 16-bit value as basic integers are on the Arduino, you send the two bytes that contain it. The reason this is significant is that data in ASCII text form only uses byte values from 0 to 127 (meaning it is a 7-bit encoding), whereas data in binary form will use the entire 8-bit range (0-255).

Whether you use binary or text is mostly a matter of personal preference; they have different advantages and disadvantages. If you are using the serial monitor that is part of the Arduino IDE, then using text is best.

sometimes the arduino sends a strange character, in most of the time it sends a mirrored quenstion mark

This is the big clue that you are viewing the data as text. The odd question mark represents an "unprintable character", probably a byte with a value greater than 127,1 point being that this isn't necessarily always the same value even if it appears as the same character; you could see this if you viewed the data with a tool like hexdump.

It's not impossible for these to be spurious artifacts due to, eg., wires that are too long. However, most likely they are due to some minor bug in your Arduino code; in my experience UART communication between a Pi and an Arduino is flawless at least up to 115200 Hz (and probably well beyond that).


  1. Character encodings other than ASCII will use all 256 byte values in order to represent characters beyond the basic latin alphabet. However, unless you are embedding such characters in the source code, you aren't using anything beyond the ASCII set.
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.