Lcd vs serial monitor

I have a arduino UNO and a dfrobot keypad LCD shield. When I tested my lm35 temp sensor whit serialread I get good results. When I change de code to lcd.print everyting goes well until I reach 31 degrees C then I get strange things on the LCD. If i use lcd.print(A0) he gives me the same Numbers as serial monitor. It's like the translation of the equation to the LCD goes wrong

Strange problem,

Please post your code so we can check it

/*-----( Import needed libraries )-----*/
#include <LiquidCrystal.h>

/*-----( Declare objects )-----*/
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //These are the pins used on this shield

int up = 11;
int down = 12;
const int NO = 13;

int plus = 0;// variable for reading the pin status
int minus = 0;
int counter = 20;
int currentState = 0;
int previousState = 0;

void setup()   /*----( SETUP: RUNS ONCE )----*/
  lcd.begin(16, 2);              // start the lcd object

  pinMode(up, INPUT); // declare pushbutton as input
  pinMode(down, INPUT);
  pinMode(NO, OUTPUT);

}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/

  // read the input on analog pin 0:
  int Tval = analogRead(A1);

  plus = digitalRead(up); // read input value
  minus = digitalRead(down);

  if (plus == HIGH) { 
    currentState = 1; 
  }  // check if the plus is HIGH (button released)

  else { 
    currentState = 0; 
  }               //  plus
  if(currentState != previousState){
    if(currentState == 1){ 
      counter = counter + 1;



  if (minus == HIGH) { 
    currentState = 1; 
  }  // check if the minus is HIGH (button released)

  else { 
    currentState = 0; 
  }                //minus
  if(currentState != previousState){
    if(currentState == 1){ 
      counter = counter -1;

  previousState = currentState;

  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float temp = (5 * Tval * 100) / 1024;

  // print out the value you read: 
  lcd.print("C  ");

  float contact = (counter - temp);

  if (contact >=1)
    digitalWrite(NO, HIGH); 
  else   {  
    digitalWrite(NO, LOW);  



when i try the thermometer code separate everything works fine. when i put it in my thermostat code i get strange readings at 32° C it becomes unreadable on the lcd. With the serial monitor it shows minus degrees. Can anyone explain what is wrong with the code.
I have every function of the code working fine but when i combine everything it goes wrong


the formula (5 * Tval * 100) are all ints and the math will overflow (and becomes negative or so) when Tval > 65.
When tVal ==65 the formula resolves to 32 ...

so please change

float temp = (5 * Tval * 100) / 1024;


float temp = Tval * 0.48828125;  // do all the math in one step

lcd.print("°C "); // ALT-0176 special char

please press CTRL-T befor posting code, remove unneeded comments (only explain WHY is enough)
makes the code better readable.

If i use lcd.print(A0) he gives me the same Numbers as serial monitor

Are you reading A0 or A1 ?

// read the input on analog pin 0:
int Tval = analogRead(A1);


the reading is correct now, but the °C thing did not work i get a other symbol

the sensor is A1 because its a keypad lcd shield that uses the A0 for the keypad. Almost al my code is copy/pasted so there can be some errors copied in to.

My next step is to change the digital buttons whit the buttons on the keypad. If anyone has tips please let me know.


Try lcd.write(0xDF); for the ° sign. Or create your own custom character.

that worked, everything works now. I just found a bug. when the relay switches on, the voltage drops and the reading drops also, the result is that the relay goes on an of until the differential is more than de voltage drop.

Give the relay a power supply of its own.

You can do several things.
Do what robtillaart said.
after the relay is turned on, delay in the code, before reading the input again.
Put more capacitors in.

I don't think you have shown us the schematic for the relay. Maybe a bit of reworking it may help. What is the amps needed to run the relay, etc.

according to the datasheet its 90mA so that is not to much. the voltage drop is 0,2V so that has an effect on the lm35 I presume, c have to create a potential free contact so it kan turn on my heating system. perhaps I can use somting else?

90mA is more than 2X the maximum amp. an arduino output can source, you need a transistor to drive the relay

You ask:

My next step is to change the digital buttons whit the buttons on the keypad. If anyone has tips please let me know.

I don’t know for sure which “LCD keypad shield” you have, but I think the most common one is like mine. If you give us the url for yours we can be more accurate.
On mine, I use this sketch to read the keys.

void keypressed(){
  int value = analogRead(A0)/12;
  if(value<70){keyvalue=60;}else{keyvalue=80; return;}
  lcd.setCursor(15,1); // set cusor position to row 1, col 1
  //lcd.print(keyvalue);   // value on A0
  if(keyvalue==85) return;
  if(keyvalue==10) {
  if(keyvalue==25) {
  if(keyvalue==39) {
  if(keyvalue==0) {
  if(keyvalue==60) {
  if(keyvalue==25)   {  adjustTime(-62); delay(100);}
  if(keyvalue==10)   {  adjustTime(1); delay(100);}

If you're trying to do precise measurements on the analog inputs, don't use the 5V supply as the reference voltage. It's nowhere near stable enough if you are powering the Arduino from USB, or if you have heavy switched loads (such as your relay) powered from 5V. Instead, connect the AREF pin to the 3.3V pin and put an analogReference(EXTERNAL) call in setup, before you do any analogRead calls. The maths will have to change because the 5 in your original formula now needs to be 3.3. A side-effect is that your temperature resolution will be improved from about 0.5C to about 0.32C.

btw I recommend using the LM34 sensor rather than the LM35, because it gives you more than twice the resolution and can measure temperatures below freezing. You can easily convert its output to Celsuis in the Arduino software.

Since the LM35 produces an output of 1V or less for temperatures below 100C, another possibility is to use the internal voltage reference for the ADC. However this varies between chips (it can be anywhere between 1.0 and 1.2V), so you would have to calibrate it.

And as alnath says, you need to drive your relay through a transistor.

Maybe a SSR would be better than a relay.

I am new whit these things transistors and ssr and aref. al these thing I have to search the web how the things work.


I use a funduino keyes relayboard. it uses transistors. The board is powerd by the arduino 5V and works fine whitout the lcd I think I will start over whit these one and see if i can work around it