[SOLVED] How to print the degree symbol ? (extended ASCII)

Hi
Serial.print(int(temp_air));
Serial.print(" °C. Humidity: ");
Give me
Temperature: 17 °C...

How can i get it ? i tried Serial.println(&deg); or &#176
thanks

It's a non-ASCII character so you can't print it as a normal character. You can add a custom character / image but that depends on the LCD (library) used.

septillion:
It's a non-ASCII character so you can't print it as a normal character. You can add a custom character / image but that depends on the LCD (library) used.

I believe he's using Serial....

try like this...

Serial.print(char(176));

You're right :o Just answered some threads about LCD's so in my head I assumed this was a LCD as well.

For a single char it's easier to use Serial.write(176); No need for a case then.

Inline you can do

  Serial.print(int(temp_air));
  Serial.print(" \xB0""C. Humidity: ");

Yes, the "" is a bit ugly but you need it otherwise the C is part of the hex...

1 Like

The HD44780 lcd controller has a built in character set. See table 4 of the data sheet

lcd.print(223) or lcd.print(0xDF) will print a degree symbol.

1 Like

It is an ASCII character. But the Arduino IDE is Unicode. You can type a º in your code and upload it to the Arduino - it will work perfectly. If you save that file, then it gets converted to Unicode, which uses two bytes to represent that character. Reloading it will show the º in the editor but it's really 2 bytes. The first byte is the funny A.

The best way to do it is to give the degree symbol a name by creating a character constant using the char(167) operator. (Or is it a cast? I can never get the terminology right.)

MorganS:
The best way to do it is to give the degree symbol a name by creating a character constant using the char(167) operator. (Or is it a cast? I can never get the terminology right.)

Did you mean 176?

Serial.print(char(176)); works nicely... thanks very much ... nuff answer in few time this forum is great...

@Catledog : thanks for complete explanation bro i will for sure keep this array for my lcd... first i try my project direct in serial to get hand on hardware and coding.... and also my lcd burn !!! but that another story...
But How did you find 223? or xB0 ? i see only 1 and 0 in the array .. did you make a translation from binary to hexa?

Blessed love.

But How did you find 223? or xB0 ? i see only 1 and 0 in the array .. did you make a translation from binary to hexa?

The table is presenting all the 8 bit binary values for the characters in the controller memory. The high four bits are given across the top, and the low four bits are given down the left edge.

First find the symbol you want in the table. The degree mark is on the bottom row and two columns from the right edge. Read up the column to find the high four bit binary value of 1101. Read across to find the low four bit binary row value of 1111.

So the binary value for the degree symbol is B11011111 which is expressed in Hex by 0xDF and in decimal by 223. You can put any of these forms into lcd.print() and you will the degree mark.

1 Like

MorganS:
It is an ASCII character.

No it's not :wink: It's an extended ASCII character. 0:-)

But thanks for the explanation!

MorganS:
(Or is it a cast? I can never get the terminology right.)

I would say it's a cast. A char holding 176 is indeed a way but that would make it harder to store it as a string (in PROGMEM). So you can inline it (and put it all in PROGMEM) like

Serial.print(F(" \xB0""C. Humidity: "));
//or for the LCD
lcd.print(F(" \xDF""C. Humidity: "));

\xnn with nn the hex value will inline a hex char. The "" is unfortunately needed to stop the compiler from parsing the next char ('C' here) as part of the hex code. Not needed if the next char isn't a valid hex value (for example "\xDF!") but I think it's a save bed to always add it to be sure.

thanks for precision.. u a master man... read the 1 and 0 ? i'm impress..
Bless.
I put it as resolved and change the title a little ...

cattledog:
lcd.print(223) or lcd.print(0xDF) will print a degree symbol.

Only works for me with lcd.write but that's no problem.

edit: perhaps that's a library thing? Mine's on an i2c backpack. Anyhoo, it works.

Only works for me with lcd.write but that's no problem.

Correct. I was wrong. It's either lcd.write(223) or lcd.print((char)223);

I got confused when I looked at an old sketch with a concatenated string from sprintf that I printed with lcd.print(), but the 223 was placed in with a %c.

cattledog:
It's either lcd.write(223) or lcd.print((char)223);

Thanks: I just verified both of those work for me.

For some reason I no longer get the degree symbol printed correctly on Serial monitor using char(176).

Here is my simple test sketch:

double TbmpC = 24.1;

void setup() {
  Serial.begin(115200);
  Serial.println("*** start SETUP ***\n");
  Serial.print("Temperature: "); 
  Serial.print(TbmpC); 
  Serial.print(char(176)); 
  //Serial.print("\xB0"); 
  Serial.println("C");
  Serial.println("\n*** end SETUP ***");
  }

void loop() {
  delay(1000);
}

And the attached snippet image shows wrong symbol for degrees in Serial Monitor. It appears as a non-descript square symbol instead.

Suggestions appreciated …

degreeSymbolError.JPG

IDE version?

1.8.2

Just upgraded to IDE 1.8.3

still see same issue, although appearing as a backward quesiton mark now - see attached

degreeSymbolError2.JPG

IDE 1.8.1 / Win7-64 / Mega

float TbmpC = 24.1;

void setup() {
  Serial.begin(250000);
  Serial.print("Temperature: ");
  Serial.print(TbmpC, 1);
  Serial.print(char(176));
  Serial.println("C");
}
void loop() {}
Temperature: 24.1°C

yep I used to see similar printed as per @Whandall

I'm using 1.8.3 on Win10-64 and Mega2560

still not solved :frowning:

PS: @Whandall its curious you set 250000 as baud rate. I would have thought 115200 x 2 = 230400 more appropriate?