Temprature sensor gives constant value of 449.51 Celsius

Hello there ,

I'm working on a project ( it is a work in progress).
Where i am using a temperature sensor. The problem is it was working fine but now it keeps giving me he same value. It's was working correct before , so it is probably something small. But i can't find it...

the temperature is calculated in Celsius. And the average value is displayed on the monitor.
I think the mistake is in this part of the code

{
 
    sensorInput = analogRead(A0);
  temp = (double)sensorInput / 1024; // sensor waarde 
  temp = temp * 5;
  temp = temp - 0.5;
  temp = temp *100;
  AvrNum = temp; // waarden van de tempratuur worden opgeslagen
  counter++; // telt + 1 bij de counter op bij elke loop
  Serial.print(AvrNum);

  if (counter == 10) { // als er 100 outputs waarden zijn gelezen
    long AvrCap= AvrNum/10; // het gedeelt door 100 om een constantere gemiddelde waarde produceren
    Serial.print("Tempratuur: ");
    Serial.println(AvrCap);
    counter = 0;
    AvrNum = 0;
    delay(500);
  }

But here is the rest

#include <LiquidCrystal.h>
LiquidCrystal lcd(5, 4, 3, 2, A4, A5);
int LCDRow = 0;


int sensorPin = A2;
int sensorInput;
double temp;
int reading = 0;
String readString;
int n = 0 ;
int number = 0;
int inputVariable;
long AvrNum;
int counter;


//voor de keypad

#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {A0, A1, 11, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

String inputString;
long inputInt;

void setup()
{
  Serial.begin(9600);
  pinMode(A2, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(13,OUTPUT);
  inputString.reserve(125); // hoogste getal dat kan worden ingevoerd
  AvrNum = 0;
  counter = 0;
   lcd.begin(16, 2);
   lcd.setCursor(LCDRow, 0);
}

void loop()

{
 
    sensorInput = analogRead(A0);
  temp = (double)sensorInput / 1024; // sensor waarde 
  temp = temp * 5;
  temp = temp - 0.5;
  temp = temp *100;
  AvrNum = temp; // waarden van de tempratuur worden opgeslagen
  counter++; // telt + 1 bij de counter op bij elke loop
  Serial.print(AvrNum);

  if (counter == 10) { // als er 100 outputs waarden zijn gelezen
    long AvrCap= AvrNum/10; // het gedeelt door 100 om een constantere gemiddelde waarde produceren
    Serial.print("Tempratuur: ");
    Serial.println(AvrCap);
    counter = 0;
    AvrNum = 0;
    delay(500);
  }
  
   if (temp >n ) { // als de tempratuur hoger is dan de ingestelde temperatuur
    Serial.println("n lager dan temp"); // print dat n lager is dan de temperatuur
    digitalWrite( 13, HIGH); // Zet de lamp (of zaag) uit
  }
  else { // anders dus als de tempratuur lager is dan de ingestelde temperatuur
    Serial.println("n hoger dan temp"); //print dat n hoger is dan de temperatuur
    digitalWrite( 13, LOW); // zet lamp (of zaag) aan
  }
  
  char customKey = customKeypad.getKey();
  
  if (customKey){
    Serial.println(customKey);
    
    if (customKey >= '0' && customKey <= '9') { // als waarde keypad tussen 9 en 0 ligt
      inputString += customKey; // 2 strings samenvoegen 
       Serial.print("Ingestelde tempratuur: "); //controle van de ingestelde tempratuur
      Serial.println(inputString);
    } 
    if (inputString.length() > 3){
      inputString= "";
    }
    else if (customKey == '#')  //als waarde # is  
    {
      if (inputString.length() >0) // en als de lengte van de string hoger dan 0 is 
      {
        int number = inputString.toInt(); // zet de strin om in een geheel getal
        inputVariable = number; // de totale waarde van de string wordt omgezet in een input waarde
        n = inputVariable; // input waarde wordt ingevoerd in n
      } 
      else (inputString.length() >3);{ // als de lengte van de string langer is dan 3 karakters, wordt de string gereset op 0
        inputString= "";
        
      }
      if (n > 125) { // Als de ingevoerde waarde hoger is dan de maximale waarde 125 graden. wordt 125 graden ingevuld.
        n=125;
      }
    } if (customKey == '*') { // als * wordt ingetoetst
      n=0; // wordt n gereset op 0
      inputString = ""; // de string waarde wordt op 0 gezet.
      Serial.print("Reset");
      Serial.println(inputString); // controle als de string daadwerkelijk 0 is
   
  }
 
  }
}

Please KNOW, that there is still a load of different shit in this code that i need to clean up. Or code better.
BUT I AM A NOOB so please be kind :smiley:

Setup
Thinkercad setup

Your post was MOVED to its current location as it is more suitable.

1 Like

I was going to suggest keeping paper away from the area, but then I noticed the Celsius, so you're way past that particular threshold :slight_smile:

1 Like

Sounds like a bad wire or connection.

What temperature sensor? How do you have it connected? What is the calculation after the reading doing?

a7

1 Like

Post the circuit diagram here. I, and a lot of other members, are not going to create a TinkerCad account just to see your circuit.

1 Like

How are things connected? Breadboard?

That's not doing what you think.


so this how i connected everything in thinkercad.
so it was working before i put the lcd in the mix. ( eventually i want to display the given value of the keypad to be shown there , and the current temperature reading)

i am using a TMP36 sensor. (if there better sensor to use let me know!)

ignore the reset button i was just trying things i had seen on the internet.

I would print out the "raw" A/D output. aka sensorInput. Others may be able to look at the code and be sure it is not the issue, however I'm old school, first look at the raw data. In this case I'll assume it stuck at 1023 but would eliminate any software issue if you could know that.

1 Like

so yes the "raw" data is getting stuck at 1023 indeed. even though i am changing the temperature.

the calculation after the reading, is to convert the data in to Celsius. (idk if that is what you meant thoughh ,english is my second language. )

you are right! any ideas how to keep that from happening?

So that's

F = ((C * 5) - 0.5) * 100

or

F = 500 * C + 50

even figuring in the fact that the sensor is 10 mV / degree C, your calculation is not correct.

So when you find the broken or missing or misplaced wire (my bet) and start getting numbers that look like they change with temperature appropriately, come back to the conversion and fix it.

F = 9/5 C + 32

but remember to scale the sensor reading so you have C, not off by a factor of 100.

It looks more like you stole borrowed a conversion to scale and shift the sensor reading - so still in centigrade.

Other sensors in that family go into negative territory so report a shifted reading.

I like that sensor FWIW.

a7

1 Like

Is this question about playing in tinkercad or a real hardware built system?
Working code never breaks down as hardware does. Onle code changes make code fail. Then step back to the last working version of code.

There's no use in naming your pins (e.g. sensorPin = A2) and then using a pin directly in the code (e.g. analogRead(A0)).

Why not use analogRead( sensorPin )?

2 Likes

Or if you don’t, at least use the pin that you have connected to your sensor.

Squinting at the Fritxthing looks like you’ve used A2. That would explain some things.

a7

1 Like

This means the analog input is a higher voltage than the analog reference.

What reference are you using?

  1. default = The supply voltage
  2. 1.1 v internal

soo i think i found it was reading the wrong pin. it needs to be A2.

`sensorInput = analogRead(A2);`

But can a analog input recieve a variable signal? If so is it better to switch to a digital pin ?

Hahahaa yes i indeed borrowed it from a example of the internet(like a lot of this code, trying to learn it haha). It looked if it was correct .
But i can play around with this , and i will comeback to you!

Yes , you are correct Thanks for the heads up :smiley:

Good point! that makes more sense ,i will change it