Go Down

Topic: Is it possible to ignore a floating input? (Read 1 time) previous topic - next topic

turbo-brown

Hello all!

Long term lurker, first time poster here!

I'm working on a little box of tricks to control a boiler for my brewery here.

I'm using TMP35 sensors for temperature reading, and I have a number of these sensors in thermowells which can be put into various bits of beer making kit.

The sensors are plugged into my box of tricks using simple audio jacks.

The box has sockets for two of these sensors and an LCD which displays the current temperature of the sensors plugged in.......you can see where this is going!

I will usually have one sensor plugged in, but more often than not, the other socket is empty.

My question is, is there a way to ignore floating analogue input readings?

I had a go at using the internal pullup resistors, but this gives a great big offset to my readings, and when there's no sensor plugged in, of course just gives a great big temperature reading.

I'd really like to be able to just ignore a floating input, and tell the display to print "---" or something of the sort.

Apologies for the rambling post, and many thanks in advance for any help!

Code below in full:



turbo-brown

Code: [Select]


// include the library code for LCD, courtesy of the examples in the Arduino IDE:
#include <LiquidCrystal.h>

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



// Define the number of samples to keep track of.  The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.  Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings1 = 15;
const int numReadings2 = 15;

const int heater_1 = 13; //SSR for heater 1 is controlled by pin 13
const int heater_2 = 10; //SSR for heater 2 is controlled by pin 12
const int tmp35_1 = A0; //temperature sensor 1 TMP35 connected to this pin
const int tmp35_2 = A1; //temperature sensor 2 TMP35 connected to this pin
const int setPot = A5; //pot to set the temperature is on this pin
const int deadband = 2; //a constant to hold the deadband for use elsewhere in the program
const int brixButton = 8; //press this button to convert from Brix to SG
const int killButton = 9; //press this button to kill the heaters temporarily

//variables to deal with averaging of temp sensors
int readings1[numReadings1];      // the readings from the analog input
int index1 = 0;                  // the index of the current reading
int total1 = 0;                  // the running total
int average1 = 0;                // the average
int readings2[numReadings2];      // the readings from the analog input
int index2 = 0;                  // the index of the current reading
int total2 = 0;                  // the running total
int average2 = 0;                // the average

int rawTemp_1 = 0; //value to read from A0 (temperature sensor 1)
int rawTemp_2 = 0; //value to read from A1 (temperature sensor 2)
int degreesC_1 = 0; //holds converted value to give a degrees C reading on the LCD
int degreesC_2 = 0; //holds converted value to give a degrees C reading on the LCD
int setPotVal = 0; //variable to store the value of the setPot
int brixVal = 0; //variable to read the brixButton
int killVal = 0; //variable to read killButton
float SG = 0; //variable to calculate the SG from an dialled in Brix value
float Brix = 0; //variable to help converting from Brix to SG

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

//
  pinMode (tmp35_1, INPUT);
  pinMode (tmp35_2, INPUT);
  pinMode (setPot, INPUT);
  pinMode (brixButton, INPUT);
  pinMode (killButton, INPUT);  

  pinMode (heater_1, OUTPUT);
  pinMode (heater_2, OUTPUT);  

  for (int thisReading1 = 0; thisReading1 < numReadings1; thisReading1++)
    readings1[thisReading1] = 0;    

  for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++)
    readings2[thisReading2] = 0;      
}

void loop() {

//*********************************************Reads temperatures and setpoints*******************

  readVals(); //go into a loop which reads the various values for temperature and setpoint etc. Avoids having to copy this everywhere

//***************************Deals with comparing the setpoint to the actual temp and acts accordingly****************************

  while(setPotVal >= 126){fullOn();} //while the set point is over 125degrees, turn both elements on
  while(setPotVal < 60){fullOff();} //while the set point is over 125degrees, turn both elements on
  while(brixVal==1){brixCalc();} //while the brixButton is pressed, go to the brix to SG calculator
  while(killVal==1){fullOff();} //while the killButton is pressed, turn off all heaters

  if(degreesC_1 < (setPotVal-deadband)){
    digitalWrite(heater_1, HIGH);}
  else if(degreesC_1 > setPotVal) {digitalWrite (heater_1, LOW);}



//**********************************************Controls the display******************************  
  lcd.clear(); //clear the screen
  lcd.print("Set Temp=");  //just prints this text

  lcd.print(setPotVal); //prints the setPotVal which is mapped from the setPot in degreesC on temp sensor 1  
  lcd.write(223); //degrees symbol
  lcd.print("C");  //just prints this text  

  showTemps(); //calls a loop which shows the incoming temperatures  
  
  delay(100);
}

void fullOn(){

//***********************************Turn heaters both on***************************************
  digitalWrite(heater_1, HIGH);
  digitalWrite(heater_2, HIGH);  
  
//***********************************Read the setpot and the temp sensors*********************************
  readVals(); //call the readVals loop

//**********************************Operate the display************************************
  
  lcd.clear(); //clear the screen
  lcd.print("Heaters On");  //just prints this text
  
  showTemps(); //calls a loop which shows the incoming temperatures  
  delay(100);

//*******************************deals with reading the buttons even when the heaters are full on or off**********
  while(brixVal==1){brixCalc();} //while the brixButton is pressed, go to the brix to SG calculator
  while(killVal==1){fullOff();} //while the killButton is pressed, turn off all heaters
  }
  

void fullOff(){
  
//***********************************Turn heaters both off***************************************
  digitalWrite(heater_1, LOW);
  digitalWrite(heater_2, LOW);
  
//***********************************Read the setpot and the temp sensors*********************************
  readVals(); //call the readVals loop
  
//**********************************Operate the display************************************
  
  lcd.clear(); //clear the screen
  lcd.print("Heaters Off");  //just prints this text

  showTemps(); //calls a loop which shows the incoming temperatures
  delay(100);

//*******************************deals with reading the buttons even when the heaters are full on or off**********
  while(brixVal==1){brixCalc();} //while the brixButton is pressed, go to the brix to SG calculator
  while(killVal==1){fullOff();} //while the killButton is pressed, turn off all heaters
  
}


turbo-brown

#2
Aug 16, 2015, 09:15 pm Last Edit: Aug 16, 2015, 09:16 pm by turbo-brown
Code: [Select]
void readVals(){
//*********************Reads temp sensor 1*******************************
  rawTemp_1 = analogRead(tmp35_1); //reads the analogue voltage from the TMP35 sensor

//*********************Reads temp sensor 2*******************************
  rawTemp_2 = analogRead(tmp35_2); //reads the analogue voltage from the TMP35 sensor

//*********************Reads and maps the setpoint*******************************
  setPotVal = map(analogRead(setPot), 0, 1024, 50, 130); //reads the value on the setPot and maps it over our temperature range. Mapped past 125 to give a "full on" option

//******************Reads the brixButton**********************************
  brixVal = digitalRead(brixButton); 

//******************Reads the killButton**********************************
  killVal = digitalRead(killButton);   

//*********************************************Does the averaging*****************

  averageTemp1();//go to loop which gives us the average of temp sensor 1
  averageTemp2();//go to loop which gives us the average of temp sensor 2

}

void averageTemp1(){
    // subtract the last reading:
  total1= total1 - readings1[index1];         
  // read from the sensor: 
  readings1[index1] = rawTemp_1;
  // add the reading to the total:
  total1= total1 + readings1[index1];       
  // advance to the next position in the array: 
  index1 = index1 + 1;                   

  // if we're at the end of the array...
  if (index1 >= numReadings1)             
    // ...wrap around to the beginning:
    index1 = 0;                           

  // calculate the average:
  average1 = total1 / numReadings1;

  degreesC_1 = (average1 / 2.048); //looking at the chart for the TMP35, 100C = 1v
 
}


void averageTemp2(){
    // subtract the last reading:
  total2= total2 - readings2[index2];         
  // read from the sensor: 
  readings2[index2] = rawTemp_2;
  // add the reading to the total:
  total2= total2 + readings2[index2];       
  // advance to the next position in the array: 
  index2 = index2 + 1;                   

  // if we're at the end of the array...
  if (index2 >= numReadings2)             
    // ...wrap around to the beginning:
    index2 = 0;                           

  // calculate the average:
  average2 = total2 / numReadings2;

  degreesC_2 = (average2 / 2.048); //looking at the chart for the TMP35, 100C = 1v
 
}

void showTemps(){

//******************Sets cursor to 2nd line and shows temp 1 and temp 2 
  lcd.setCursor(0, 1);  //sets the cursor
  lcd.print("T1=");  //just prints this text
  lcd.setCursor(3, 1);  //sets the cursor 
  lcd.print(degreesC_1); //prints the temperature in degreesC on temp sensor 1 
  lcd.write(223); //degrees symbol
  lcd.print("C");  //just prints this text

  lcd.setCursor(8, 1);  //sets the cursor
  lcd.print("T2=");  //just prints this text
  lcd.setCursor(11, 1);  //sets the cursor 
  lcd.print(degreesC_2); //prints the temperature in degreesC on temp sensor 1 
  lcd.write(223); //degrees symbol
  lcd.print("C");  //just prints this text
}

void brixCalc(){

  readBrixVals(); //call the readVals loop
  lcd.clear(); //clear the screen
  lcd.print("Brix Value=");  //just prints this text
  lcd.print(setPotVal); //prints the setPotVal which is now mapped to represent 0-30% Brix
  lcd.print("%"); //percent symbol

  lcd.setCursor(0, 1);  //sets the cursor
  lcd.print("SG Value=");
  lcd.print(SG,3);

  delay(100);
 
}

void readBrixVals(){

//*********************Reads and maps the setpoint*******************************
  setPotVal = map(analogRead(setPot), 0, 1024, 0, 31); //reads the value on the setPot and maps it over our temperature range. Mapped past 125 to give a "full on" option
  Brix=((setPotVal)/1.04); //see http://byo.com/hops/item/1313-refractometers for explanation of this constant 1.04
  SG = (1.000019 + (0.003865613*Brix) + (0.00001296425*Brix) + (0.00000005701128*Brix)); //see http://byo.com/hops/item/1313-refractometers for this formula
 

//******************Reads the brixButton**********************************
  brixVal = digitalRead(brixButton);  //if we don't do this, we can't get back to the main loop when the button is released

}


Sorry, code in two halves!

Wawa

The LM35 can handle some resistance to ground without the reading being affected.

Try a 10k pull-down resistor from analogue in to ground.
Then the temp will read zero degrees when the sensor is unplugged.
Leo..

Archibald

Use internal pull-up resistor as you describe and add code so when a "great big temperature reading" is detected print "---".

Wawa

Use internal pull-up resistor as you describe and add code so when a "great big temperature reading" is detected print "---".
Dangerous.
A pull-up resistor could affect readings.
The datasheet only shows loads to ground, not to supply.
A simple 10k resistor to ground could avoid problems.
Leo..

olf2012

The LM35 is specified for a load current of 20µA. I think this is to prevent self-heating. So you should use at least 100k as pull-down

turbo-brown

Many thanks indeed for your help everyone!

Popped a 10k pull down resistor on each temp input. Doesn't appear to have altered the readings, need to calibrate the sensors to make sure I've not scaled them wrongly!

Thanks again,
Kind regards,
Alex

Go Up