Temperature display on LCD help please

Hi all. I have been working towards my first (hopefully permanent) Arduino project :slight_smile: I want to display some temperatures on an LCD and possibly add humidity later.

I have been working on some code (with some help from DC42 many thanks for that)

I have noticed how much the readings fluctuate - noise I guess and I would like to eradicate some of this. I get a swing of about 1.5 degrees c most of the time. I thought about averaging the readings but can’t get the code to compile and I’m stuck on that :frowning:

Or Would I be better of scrapping using thermistors and dividers and trying to use digital readers?

The code below is what I have so far. I’m also dimming the LCD back light by reading a LDR which is working nicely.

/*
 
 
 The circuit:
 * LDR connected to analog pin 0.
 * LED / transistor connected from digital pin 3 to ground
 
 * LCD RS pin to digital pin 7
 * LCD EN pin to digital pin 8
 * LCD D4 pin to digital pin 9
 * LCD D5 pin to digital pin 10
 * LCD D6 pin to digital pin 11
 * LCD D7 pin to digital pin 12
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 * LCD LED+ (15) pin transistor
 * LCD LED- (16) to ground 
 
 * Thermisor divider 1 output to analog pin 1
 * Thermisor divider 2 output to analog pin 2
 
 
 */

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the lDR is attached to
const int analogOutPin = 3; // Analog output pin that the LED is attached to

const int minLight = 100;        // at or below this light level, use minimum backlight intensity
const int maxLight = 900;       // at or above this light level, use maximum backlight intensity
const int minBacklight = 5;      // lowest backlight intensity to use
const int maxBacklight = 255;  // highest backlight intensity to use

// include the library code:
#include <LiquidCrystal.h>
#undef float
#define NUMSAMPLES 5
int samples[NUMSAMPLES];
#include <math.h>

double average(int RawADC) {
  double Temp;
  Temp = log(((10240000/RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
  Temp = Temp - 273.15;            // Convert Kelvin to Celcius

  return Temp;
}


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);


int sensorValue = 0;        // value read from the LDR
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  lcd.begin(16, 2);
  delay(100);
  pinMode(13, OUTPUT);  

}

void loop() {
  // read the analog in value:
  analogRead(analogInPin);
  delay(200);
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(constrain(sensorValue, minLight, maxLight), minLight, maxLight, minBacklight, maxBacklight ); 
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);  

 uint8_t i;
  float average;

  // take N samples in a row, with a slight delay
  for (i=0; i< NUMSAMPLES; i++) {
   samples[i] = analogRead(1)), 1);
   delay(10);
  }

  // average all the samples out
  average = 0;
  for (i=0; i< NUMSAMPLES; i++) {
     average += samples[i];
  }
  average /= NUMSAMPLES;




 
 
 
 
 
 
 
  //Temperature 1 print
  lcd.clear();

  lcd.setCursor(0,0);
  lcd.print ("Out: ");
  lcd.print(average(analogRead(1)), 1);


  lcd.print((char)223);
  lcd.print ("c");


  //Serial.print ("Temp1=");
  //Serial.println(int(Thermister(analogRead(1))));  // display temp

  // Temperature 2 print
  //lcd.setCursor(0,1);
  //lcd.print(int(Thermister(analogRead(2, 1))));


  //lcd.print((char)223);
  //lcd.print ("c");

  //Serial.print ("Temp2=");
  //Serial.println(int(Thermister(analogRead(2))));  // display temp


  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(1000);                     
}

refactored your code so it compiles …(IDE 0.22) - fixed some minor typos too

/*
 The circuit:
 * LDR connected to analog pin 0.
 * LED / transistor connected from digital pin 3 to ground
 
 * LCD RS pin to digital pin 7
 * LCD EN pin to digital pin 8
 * LCD D4 pin to digital pin 9
 * LCD D5 pin to digital pin 10
 * LCD D6 pin to digital pin 11
 * LCD D7 pin to digital pin 12
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 * LCD LED+ (15) pin transistor
 * LCD LED- (16) to ground 
 
 * Thermisor divider 1 output to analog pin 1
 * Thermisor divider 2 output to analog pin 2
 */

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;      // Analog input pin that the lDR is attached to
const int analogOutPin = 3;      // Analog output pin that the LED is attached to

const int minLight = 100;        // at or below this light level, use minimum backlight intensity
const int maxLight = 900;        // at or above this light level, use maximum backlight intensity
const int minBacklight = 5;      // lowest backlight intensity to use
const int maxBacklight = 255;    // highest backlight intensity to use

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

#define NUMSAMPLES 32

double convert(double RawADC) 
{
  double Temp;
  Temp = log(((10240000/RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
  Temp = Temp - 273.15;            // Convert Kelvin to Celcius

  return Temp;
}

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);


int sensorValue = 0;        // value read from the LDR
int outputValue = 0;        // value output to the PWM (analog out)

void setup() 
{
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  lcd.begin(16, 2);
  delay(100);
  pinMode(3, OUTPUT);  
}

void loop() 
{
  // LDR PART
  int sum = 0;
  for (int i=0; i<16; i++) // do some averaging too
  {
    sum += analogRead(analogInPin);
  }
  sensorValue = sum/16;
  
  // map it to the range[0..255] of the analog out
  outputValue = sensorValue/4;   // far easier than map function
  
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);  
  
  delay(1000);

  // TEMPERATURE PART
  uint8_t i;
  float average = 0;
  for (i=0; i< NUMSAMPLES; i++) 
  {
   average += analogRead(1);
   delay(10);
  }
  average /= NUMSAMPLES;
  double temperature = convert(average);
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Out: ");
  lcd.print(temperature);

  lcd.print((char)223);
  lcd.print ("c");

  delay(1000);                     
}

Wow I can't thank you enough that works perfectly :)

I have adapted it for two temp inputs now! I love my arduino 8)

Next step is to add humidity sensors so I'm looking forward to getting them

Thanks again robtillaart, you have done a great job on the code