I would like to use the following sketch for a project, however I would rather use the DS18B20 digital temperature sensor rather than the analog LM35 used in the sketch. What changes would have to be made to the sketch to support this change in hardware.
Any help would be much appreciated as I have very little programing skills.
/* This setup will define a temperature
* cool-down for the fan to cool. Output
* will be to a fan, readings will be
* displayed on a 20x4 LCD.
* The circuit:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital pin 4
* LCD D4 pin to digital pin 2
* LCD D5 pin to digital pin 13
* LCD D6 pin to digital pin 8
* 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 (board pin 3)
* fan output on PWM pin 3
* temp sensor input on A0
*/
// setting up the LCD readout:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 4, 2, 13, 8, 12); //RS,EN,D4,D5,D6,D7
// averaging sensor readings:
const int numReadings = 5; // how many readings to average
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
// intializing analog input/output pins:
int fanOutputPin = 3;
int inputPin = A0;
// defining variables to be used later
int outputValue = 0;
int outputPerc = 0;
float tempC;
float tempF;
// defining temperature markers
int shutdownTempF = 75;
int startTempF = 85;
int maxTempF = 92;
// code to define the "printDouble" variable on the LCD, used to display floating point integer
void printDouble(double val, byte precision) {
lcd.print (int(val));
if( precision > 0) {
lcd.print(".");
unsigned long frac, mult = 1;
byte padding = precision -1;
while(precision--) mult *=10;
if(val >= 0) frac = (val - int(val)) * mult;
else frac = (int(val) - val) * mult;
unsigned long frac1 = frac;
while(frac1 /= 10) padding--;
while(padding--) Serial.print("0");
lcd.print(frac,DEC) ;
}
}
void setup(){
Serial.begin(9600);
lcd.begin(20, 4); // set up the LCD's number of columns and rows:
}
void loop() {
// temp smoothing averaging:
total= total - readings[index]; // subtracts the last reading
readings[index] = analogRead(inputPin); // reads from the sensor
total= total + readings[index]; // adds the reading to the total
index = index + 1; // advance to the next position in the array
if (index >= numReadings) // if we're at the end of the array
index = 0; //wrap around to the beginning
average = total / numReadings; //Averages the readings
// temp sensor conversion
tempC = (5.0 * average * 100.0)/1024.0; // converts input average to C
tempF = ((tempC*9)/5)+32; // converts C into F
// switching output based upon temperature and fan status
if (tempF < shutdownTempF) // if temp is below shutoff point
outputValue = 0; // output will be 0
else if (tempF > shutdownTempF && tempF < startTempF && outputValue == 0) // temp between shutdown and startup
outputValue = 0; // and fan is off, leave it off
else if (tempF > startTempF && tempF < maxTempF) //temp is between startup and maximum
outputValue = map(tempF, shutdownTempF, maxTempF, 50, 255); // start fan and adjust accordingly
else if (tempF > maxTempF) // temp is above maximum
outputValue = 255; // fan speed is maximum
else // if no other condition is acceptable, must be in transition and fan is still running
outputValue = map(tempF, shutdownTempF, maxTempF, 20, 255); //let it run until shutoff temp is reached
analogWrite(fanOutputPin, outputValue); // writes to PWM output
// serial display segment:
outputPerc = map(outputValue, 0, 255, 0, 100); //Translates fan output to percentage
Serial.print("Temperature C = ");
Serial.print(tempC);
Serial.print(" Temperature F = ");
Serial.print(tempF);
Serial.print(" Output = ");
Serial.print(outputValue);
if (outputValue < 1)
{
Serial.println(" FAN OFF");
}
else
{
Serial.print(" Output Percentage = ");
Serial.println(outputPerc);
}
// LCD display segment:
lcd.setCursor(0, 0); // set the cursor to column 0, line 0
lcd.print("Temp=");
printDouble(tempF,1); // displays tempF with 1 digit after decimal
lcd.print((char)223);
lcd.print("F ");
lcd.setCursor(0, 1); // new line
lcd.print("STATUS="); //fan status
if (outputValue < 1)
{
lcd.print("FAN OFF"); // either "OFF'
}
else
{
lcd.print("FAN ON "); // or "ON"
}
lcd.setCursor(0, 2); // new line
lcd.print("FAN POWER = ");
lcd.print(outputPerc); // printing PWM output percentage
lcd.print("% ");
delay(250);
}