help with float and int

Hi friends

I have a project that uses the arduino uno, a relay module, AD8495 K type amplifier and a lcd.

I worked for some time with lm35 sensor and it works fair....now I would replace it with AD8495 for use k type thermocouple.

My problem is during the calibration I use the 5 / 1023 and get the voltages, I take 3 points of temp and calculate the calibration rule and it gives a good R value, but after input the calib equation, the readings are very far from the real value.

Then I upload a simple program with INT function to read the A0 and after the same process I get about 1 degree of error.

I suspect the FLOAT function, but I use it to get more decimal places.

Please help me to correct this code seems the var "temp" and "tempPin" can be resumed to one

The original code is

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27 // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

//source: http://www.electroschematics.com/9540/arduino-fan-speed-controlled-temperature/

int row = 16; //  <<----- My LCD was 16x2
int column = 2; //  <<----- My LCD was 16x2
LiquidCrystal_I2C   lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin, BACKLIGHT_PIN, POSITIVE);


float tempPin = A0;   // the output pin of K thermocouple
float temp;
int led = 8;        // led pin

float templaw=5;   // the law ramp 

void setup() {
    lcd.begin (row,column); // initialize lcd and turn on backlight
  lcd.home ();                   // go home  
  pinMode(led, OUTPUT);
  pinMode(tempPin, INPUT);
  Serial.begin(9600);
    
}


 
 void loop() {  
   temp = readTemp();     // get the temperature
  if (temp < templaw - 1) {   
   templaw = templaw + 0; 
   } else {
   templaw = templaw + 0.083;  //1seg em 1seg verifica
    
   }
 
    
   
   if(temp > templaw) {        // if temp is higher than tempMax
     digitalWrite(led, LOW);  // turn on led 
   } else {                    // else turn of led
     digitalWrite(led, HIGH); 
   }
   
   Serial.print("TEMP: ");
   Serial.println(temp, 4);      // display the temperature
   lcd.setCursor(1, 0);                                            // Set LCD cursor position (column, row)
   lcd.print("TEMP: ");                                             // Print text to LCD
   lcd.print (temp, 4);
   lcd.setCursor(1, 1);                                            // Set LCD cursor position (column,row) 
   lcd.print("TEMPLAW: ");                                                     // Print text to LCD
   lcd.print (templaw);  
   
   Serial.print("TEMPLAW: ");
   Serial.println(templaw); 
   
   
  
   
   
   delay(1000);
  
}

float readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return (temp * (5.0 / 1023.0)) * 196.8 - 248.6;//calib dez2018
   
}

The simple analog read program is (i only use the A0)

/*
   ReadAnalogVoltage
   Reads an analog input on pin 0 to 3, converts it to voltage, 
   and prints the result to the serial monitor.cpalha 04dez2017
   */

// the setup routine runs once when you press reset:

void setup() {
   // initialize serial communication at 9600 bits per second:
   Serial.begin(9600);
}

// the loop routine runs over and over again forever:

void loop() {
   // read the input on analog pin 0:
   int sensorValue0 = analogRead(A0);
    int sensorValue1 = analogRead(A1);
     int sensorValue2 = analogRead(A2);
      int sensorValue3 = analogRead(A3);
   
   
   
   // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
   float voltage0 = sensorValue0 * (5.0 / 1023.0) * 192.2 - 251.42;//calib ago2018
  float voltage1 = sensorValue1 * (5.0 / 1023.0);
 float voltage2 = sensorValue2 * (5.0 / 1023.0);
float voltage3 = sensorValue3 * (5.0 / 1023.0); 
   
   
   
   // print out the value you read:

 Serial.print(voltage0, 1);
 Serial.print(" ");
  Serial.print(voltage1, 4);
   Serial.print(" ");
   Serial.print(voltage2, 4);
    Serial.print(" ");
   Serial.println(voltage3, 4);
   
   delay(10000);
}

float tempPin = A0;  // the output pin of K thermocoupleWhy is this pin number a float ?

UKHeliBob:
float tempPin = A0;  // the output pin of K thermocoupleWhy is this pin number a float ?

. Because it isn't connected to anything? :smiley:

The pin number is the Analog 0
It is connected because I have readings

for example

With my this code:

54.81c --- 1.5640volt
17.21------1.3685volt
6.01c----1.3099volt

With other code (INT function)

41.71c----1.4761v
18.21c----1.3539v
6.91c----1.3001v

best
cpalha

No, the pin number should not be a float.
It should be an integer, preferably of type byte or uint8_t

AWOL:
No, the pin number should not be a float.
It should be an integer, preferably of type byte or uint8_t

of type 'const byte' or 'const uint8_t'

Pins don't typically change :slight_smile:

So I need to replace the FLOAT for INT?

And in the code (as I am a begginner in arduino) seems the "temp" and "tempPin" could be the same, for example always "temp"

cpalha:
So I need to replace the FLOAT for INT?

And in the code (as I am a begginner in arduino) seems the "temp" and "tempPin" could be the same, for example always "temp"

better replace float for 'const byte' in declaring pins , you could save much SRAM for that

Like that

float tempPin = A0;

replace for

const byte tempPin = A0;

Hi

Why the setup for analog read could not be as the simple program to read Analog out??

float temp;
int tempPin = 0;

void setup()

{

Serial.begin(9600);

}

void loop()

{

temp = analogRead(tempPin);

temp = temp * 0.48828125;

Serial.print("TEMPRATURE = ");

Serial.print(temp);

Serial.print("*C");

Serial.println();

delay(1000);

}

Why the setup for analog read could not be as the simple program to read Analog out??

It's your program. Make it as simple or as complicated as you want

According to the datasheet, the AD8495 outputs 5mV per °C, so:

tempC = analogRead(A0) / 1.023;
Serial.println(tempC);

Should work.

UKHeliBob:
It's your program. Make it as simple or as complicated as you want

Hi friend if I had knowldge to do that I was not here to ask for help. This program results from other examples that I found. and worked for years with LM35, now I need to improove it with thermocouple.

thanks
cpalha

I am having a mighty hard time following your descriptions of what you're seeing.

Make sure you're not getting integer-division'ed.

10/3 = 3 (integer)
10.0/3 = 3.33 (float, type of second operand is promoted according to C type promotion rules)
10/3.0 = 3.33
float x = 10/3; //x is still 3, because it didn't get turned into a float until you stored the result.

also:
int x = 9.99; //x is 9. Converting floats to int truncates, not rounds.

Well, I don't understand your "calibration", but since the AD8495 outputs half the mV per degree as the LM35, try multiplying the ADC result by 2 in your original program.

float readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin) * 2; //<<<<<<<<
  return (temp * (5.0 / 1023.0)) * 196.8 - 248.6;//calib dez2018
   
}

If that doesn't work, you may have the thermocouple connected backward, try reversing the wires.

I only need to get the reading for the A0 with about 2 decimal places...

cpalha:
I only need to get the reading for the A0 with about 2 decimal places...

That's too bad that analogRead returns an integer.

AWOL:
That's too bad that analogRead returns an integer.

Can you please explain better??? Do you mean an integer without decimal places??? I need precision, because I will work with temperatures from 0ºc to 100ºC. And the changes are in milivolts...

Do you mean an integer without decimal places?

Is there any other kind ?

You could get about 5 times better resolution using the Arduino's 1.1V internal reference, but would limit your maximum temp to about 220°C. See what kind of results you get with this test sketch.

/*
// thermometer, no floats, no delays

const byte sampleBin = 8, // number of samples for smoothing
           aInPin = A0;

const int calValue = 0; // adjust for calibration 

const int kAref = 1100, // analog ref voltage * 1000
                        // measured with accurate DMM
          kSampleBin = sampleBin * 1000,
          tEnd = 5000; // update time in mS
int tempC,
    tempF;
    
uint32_t total,  // sum of samples
         tStart; // timer start
void setup()
{
  Serial.begin(9600);
  analogReference(INTERNAL); // use 1.1V internal ref
  analogRead(aInPin);
  total = analogRead(aInPin) * sampleBin;
}
void loop()
{
  if(millis() - tStart > tEnd)
  {
    tStart = millis(); // reset timer 
    total -= (total / sampleBin); // make room for new reading
    total += analogRead(aInPin); // add new reading
    tempC = total * kAref / kSampleBin * 2 + calValue;
    tempF = (tempC * 18 + 3200) / 10;
    Serial.print(analogRead(aInPin));
    Serial.print("\t");
    Serial.print(total); // sum of samples
    Serial.print("\t");
    prntTemp(tempC);
    prntTemp(tempF);
    Serial.println();
  }
}
    
void prntTemp(int temp){
  Serial.print(temp / 10); // whole degrees
  Serial.print(".");
  Serial.print(temp % 10); // tenths
  Serial.print("\t");
}