I built a small circuit with a Temperature/Humidity sensor and let the retrieved values be displayed in the serial Monitor. My problem is that the degrees symbol (°, \u00B0) is displayed correctly only sometimes.
I have also tried using the extended ascii table, in which the degrees sign has the value 176 assigning it to an unsigned char. This way only the number is printed though.
Is there a reliable way to display this character?
This is likely a problem in the code, probably overrunning an array. Common problem with unicode is not allowing enough space to store the character since it takes multiple bytes per character.
I've had that problem before too. You could try increasing the serial baud rate, that worked for me. Don't forget to increase this in the serial monitor as well, otherwise there will be even more errors. Unfortunately I can't tell you why this works.
Serial.print("\xC2\xB0"); //unicode utf-8
Serial.println("°"); //note - cannot use single quotes
You cannot embed \x00 in a text literal, because 0x00 is used to indicate the end of the text, and the serial monitor does not appear to accept 176 for the degree symbol (should work on an LCD though, depending on the character set being used).
It always just works sometimes, same issue as in the first screenshot. And increasing the baud rate works 99% of the time, but I have no idea why. Anyone have an idea?
Seems odd that baud rate would have any affect on this, increased baud rate just reduces the possibility that your code will fill the serial transmit buffer and have to wait for space to become available before returning from the print() function.
Which version of the IDE are you using? Looking at some of the previous discussions on printing the degree symbol, I see comments about some versions of the serial monitor supporting extended ascii and others using uft-8.
The Arduino IDE 2.0 - 2.0.3 serial monitor had a timing-dependent bug while displaying UTF-8 multibyte sequences: if it happened to do a display in the middle of a receiving a UTF-8 byte sequence (between the leading byte and the last continuation byte), then the unicode character is displayed as �� in the serial monitor. But if the entire UTF-8 byte sequence for that character is received before it happened to display, then it shows up correctly.
At low baud rates (like 1200 baud) with long unicode sequences (like the smiley / code point U+1F600) this happened every time. At high baud rates (like 115200 baud) with short sequences (like the degree sign ° above) it happened rarely.