°C --> How to insert this character in 16x2 LCD

I'm playing some sketch of LCD examples using Arduino Uno board.
Instead of using temperature sensor, I used potentiometer.

I tried to insert the degree sign 377 in OCT plus C (°C), but compilation error always happened.
Here the code. TIA.

/*  

comment here

 */
 
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int analogPin = 3;     // potentiometer wiper (middle terminal) connected to analog pin 3
                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read

void setup()
{
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
}

void loop()
{ 
  lcd.setCursor(0, 0);
  lcd.print("Temperature: ");
  lcd.setCursor(0, 1);
  val = analogRead(analogPin);    // read the input pin
  lcd.print(val);                 // debug value
  delay(1000);
  lcd.clear();
}

The degree symbol is 0xdf or 223 decimal or 337 octal vs 377 octal on my lcd.

To print the degree symbol you would use the standard C escape sequence of \xxx
to represent the character
where xxx is the octal character value.

So to print degree C on my display it would be:

lcd.print("\337C");

If 377 is correct for yours then:

lcd.print("\377C");

--- bill

3 Likes

lcd.write(B11011111);

If you want to save one space, you can try combining the degree and the C in one space by defining a custom character. Details:

thanks bill. got it.
these two codes are working.

I using this LCD.

lcd.print((char)178);     // degree symbol
lcd.print("C");     // C character
lcd.print("\262C");
2 Likes

Thanks liudr.
B11011111 binary or 337 Octal is equivalent to greek alpha.

My LCD has note regarding to degree sign.
"Different LCD displays have different char code for degree.
If you see greek alpha letter try typing 178 instead of 223."

Might be useful or at least fun to try some LCD character arts:

http://arduino.cc/forum/index.php/topic,54411.0.html

Would be awesome to do this on a larger display:

Give this code a try see if it works, it is more generic, regardless of the LCD being used and its character map:

const uint8_t charBitmap[][8] = {
   { 0x6, 0x9, 0x9, 0x6, 0x0, 0, 0, 0 }
};

int charBitmapSize = (sizeof(charBitmap ) / sizeof (charBitmap[0]));

  // Load custom character set into CGRAM
   for ( i = 0; i < charBitmapSize; i++ )
   {
      myLCD.createChar ( i, (uint8_t *)charBitmap[i] );  // myLCD is an initialized LCD that you should have done earlier 
   }

   myLCD.print ( "\x01" );
   myLCD.print ("C");

It loads a custom character to location 0 of the LCD GCRAM.

fm:
Give this code a try see if it works, it is more generic, regardless of the LCD being used and its character map:

fm,
Shame, Shame.
It is quite cruel to provide people examples of code that not only do not compile but has bugs
and does not even use the proper API parameters, especially when presenting code to people
that may not have the programming capabilities to be able to correct the code.

Kiks,
If you want to define a custom character so that the code will work
on any hd44780 lcd, here is some sample code that is merged into your example.
It takes the bitmap provided by fm, but then uses the proper calls to send it to the display
as character 1.
Character 1 is used rather than 0 so that it can be used inside a string and can even
be combined with the C in the same string.
Once you send the custom bitmap to the display you can use it just like any other character.

/*
 */
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int analogPin = 3;     // potentiometer wiper (middle terminal) connected to analog pin 3
                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read

uint8_t DegreeBitmap[]= { 0x6, 0x9, 0x9, 0x6, 0x0, 0, 0, 0 };

void setup()
{
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);

  // install the custom degree symbol as character #1
  lcd.createChar ( 1, DegreeBitmap ); 
}

void loop()
{ 
  lcd.setCursor(0, 0);
  lcd.print("Temperature: ");
  lcd.setCursor(0, 1);
  val = analogRead(analogPin);    // read the input pin
  lcd.print(val);                 // debug value
  lcd.print("\001C");             // print char 1 (which is the degree) followed by C
// you can also print the degree with the lines below instead
//  lcd.write(1);  // will print just the degree symbol
//  lcd.print("\001");  // will also print just the degree symbol
  delay(1000);
  lcd.clear();
}

Whoopsie, it was jus a quick hint to allow for a merge on the above code. It's 12:30 in the morning and I am bit jetlaged. Sorry folks. It was a quick snippet of one of my demo apps, but you are right there Bill I will try to be more thorough on coming posts.

Thanks liudr, bperrybap & fm! :slight_smile:
bitmap is also pertains to dot character patterns.

Using the same code, I tried to create additional features that will allow to put the temperature set point and temperature guardband.
I modified the 16x2 LCD and 3x4 Keypad connection and its works.
I'm new in MCU. :slight_smile:

I tried to play the code using this but didn't met my expected output.

    if(key)  // same as if(key != NO_KEY)
    {
      switch (key)
      {
        case '*':
          lcd.setCursor(5, 1);
          lcd.print("Menu");
        case '#':
          lcd.setCursor(5, 1);
          lcd.print("Cancel");
        default:
          lcd.setCursor(10, 1);
          lcd.print(key);
      }
    }

Expected output:

Default display

Temperature:
105°C

by pressing *
Displaying the Temperature Setpoint and Temperature Guardband.
Cursor at (0,12)

Setpoint : _
Guardband :

by pressing 125 or any value as Temperature Setpoint and followed by # (as Enter)
Cursor at (1,12)

Setpoint : 125
Guardband : _

by pressing 3 or any value as Temperature Guardband and followed by # (as Enter)

Setpoint : 125
Guardband : 3

by pressing 3 or any value as Temperature Guardband and followed by # (as Enter)
Will back to its default display.

Temperature:
105°C

/*  

comment here

 */





#include <Keypad.h>             // include the Keypad code:
#include <LiquidCrystal.h>      // include the library code:
 




const byte ROWS = 4;      // Four rows
const byte COLS = 3;      // Three columns

char keys[ROWS][COLS] = { // Define the Keymap
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = { 12, 11, 10, 9 };                                     // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6 };                                           // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );      // Create the Keypad






LiquidCrystal lcd(5, 4, 3, 2, 1, 0);      // initialize the library with the numbers of the interface pins

int analogPin = 5;      // potentiometer wiper (middle terminal) connected to analog pin 5, outside leads to ground and +5V
int val = 0;            // variable to store the value read
uint8_t DegreeBitmap[]= { 0x6, 0x9, 0x9, 0x6, 0x0, 0, 0, 0 };      // create a dot character pattern for degree symbol 



void setup()
{

  lcd.begin(16, 2);                        // set up the LCD's number of columns and rows:
  lcd.createChar ( 1, DegreeBitmap );      // install the custom degree symbol as character #1
}





void loop()
{
  lcd.setCursor(0, 0);
  lcd.print("Temperature: ");
  lcd.setCursor(0, 1);
  val = analogRead(analogPin);   // read the input pin
  lcd.print(val);                // debug value
  lcd.print("\001C");            // print char 1 (which is the degree) followed by C
  // you can also print the degree with the lines below instead
  // lcd.write(1);               // will print just the degree symbol
  // lcd.print("\001");          // will also print just the degree symbol
  delay(1000);
  lcd.clear();
}

Can you post your complete but not working code? The top snippet is a simple case that is missing break; in every case. The bottom is a complete code by itself but has no button sensing or responses at all. Post what you have and don't worry about the code not working.

Thanks liudr. I'm so sorry, my code above is too messy.

I want to built a circuit that will monitor a certain temperature of a system.
It allows me to enter the set point/reference temperature and temperature guardband.
I'm using a 16x2 LCD and 4x3 keypad.
I will pin 13 as an output that will LED is off when out-of-temperature.

I used potentiometer instead of temperature sensor for playing purposes.
Now, still trying to write a code that will look like this.
Can somebody help me to figure out what correct code to be used.
And Iwant to store the value, so that I can able to do math functions for LED output.

when setting the setpoint and guardband temperature:

//Default display

Temperature
30°C

//Display when pressing *
//cursor blinks at (11,0)

Setpoint : _
Guardband :

//print a key then # as an enter
//cursor blinks at (11,1)

Setpoint : 25
Guardband : _

//print a key then # as an enter
//hold for 3 seconds

Setpoint : 25
Guardband : 3

//after 3 second will back to default display

Temperature
30°C

When displaying the setpoint and guardband temperature:

//Default display

Temperature
30°C

//Display when pressing #
//hold for 3 seconds

Setpoint : 25°C
Guardband : +/- 3

//after 3 second will back to default display

Temperature
30°C

/*  

comment here

 */





#include <Keypad.h>             // include the Keypad code:
#include <LiquidCrystal.h>      // include the library code:
 




const byte ROWS = 4;      // Four rows
const byte COLS = 3;      // Three columns

char keys[ROWS][COLS] = { // Define the Keymap
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = { 12, 11, 10, 9 };                                     // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 8, 7, 6 };                                           // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );      // Create the Keypad






LiquidCrystal lcd(5, 4, 3, 2, 1, 0);      // initialize the library with the numbers of the interface pins

int analogPin = 5;      // potentiometer wiper (middle terminal) connected to analog pin 3, outside leads to ground and +5V
int val = 0;            // variable to store the value read
uint8_t DegreeBitmap[]= { 0x6, 0x9, 0x9, 0x6, 0x0, 0, 0, 0 };      // create a dot character pattern for degree symbol 



void setup()
{

  lcd.begin(16, 2);                        // set up the LCD's number of columns and rows:
  lcd.createChar ( 1, DegreeBitmap );      // install the custom degree symbol as character #1
}





void loop()
{  
  

  
  char key = keypad.getKey();
  

 
  
    if(key)  // same as if(key != NO_KEY)
    {
      switch (key)
      {
        case '*':
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Setpoint: ");
          lcd.setCursor(12, 0);
          lcd.cursor();
          delay(500);
          lcd.print(key);
          lcd.setCursor(0, 1);
          lcd.print("Guardband: ");
          lcd.setCursor(12, 1);
          lcd.print(key);
          break;          
        case '#':
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Cancel");
          break;
      }
    }



    
    
    
}

Nice, thanks for info, useful :slight_smile:
#iamnewbie