Temperature reading negative

Found an error which answers the previous post (missing quote "), but still have an error!
LM35 error.txt (30.1 KB)
Looking at other examples this function does precede this function, so is it the content within the braces that is causing the error?
As I said before without a tutorial on how to write sketches to work with Cayenne I am ‘shooting in the dark’.

I approached the problem from a different angle now!
Took the TMP36 sketch and modified it with the calculations to get the values from the LM35 sketch you sent me. No errors but still a negative reading!
Here is a copy of the sketch:
LM35 mod1.txt (2.5 KB)
Any ideas why it’s still negative?

Been trying more experiments.
Now showing a positive number, which is about the temperature I would expect, it varies by 5 to 6 degrees mostly greater than the actual temperature and also alternates between this figure and zero.
I thought that it may have been the millis part that was causing this second value to be displayed, but it makes no difference if I comment it out.
Here is the modified sketch:
LM35 mod2.txt (2.7 KB)

Try this code:

#define CAYENNE_PRINT Serial   // Comment this out to disable prints and save space
#include <CayenneTemperature.h>
#include <CayenneMQTTEthernet.h>   // Change this to use a different communication device. See Communications examples.

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

int val;
int tempPin = 1;

int lastMillis = 0;

#define VIRTUAL_CHANNEL 1

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
}

void loop()
{
  Cayenne.loop();
  val = analogRead(tempPin);
  float mv = ( val / 1024.0) * 5000;
  float cel = mv / 10;
  float farh = (cel * 9) / 5 + 32;
  Serial.print("TEMPRATURE = ");
  Serial.print(cel);
  Serial.print("*C");
  Serial.println();
  if (millis() - lastMillis > 10000)
  {
    lastMillis = millis();
    Cayenne.celsiusWrite(1, cel);
  }
}

Tried the code and it gives a reading of between 154.0 and 160.0.
I have put a voltmeter on the output of the device and I get a reading of approx 300mV.
Don’t know how the Cayenne calculates the temperature from the voltage, but perhaps you know how. Looking at the datasheet the gain is 10mV /deg C. Not sure if there is a constant voltage from which the gain is calculated.
Does this help solve this?

Cayenne does nothing here. The data is calculated in the code which is:

  float mv = ( val / 1024.0) * 5000;
  float cel = mv / 10;
  float farh = (cel * 9) / 5 + 32;
  Serial.print("TEMPRATURE = ");
  Serial.print(cel);
  Serial.print("*C");
  Serial.println();

and this is the code from the tutorial link i shared above for LM35. So if you want just the voltage then pass the variable val in the Cayenne.celsiusWrite

I have tried running the code on Arduino without doing it through Cayenne and I am getting widely varying results.So I am shelving this for now until I can obtain a TMP36!

2 Likes