Peltier/ Temp sensor

Hello :slight_smile:

We’re trying to make a temp sensor (LM35) read the temprature from a peltier. The temperatures Serial monitor are printing out doesn’t seem logic. Can you help us find anyithing wrong in this cod?
Thanks!

int peltier = 3;
int tempPin = A0;
//int power = 0;
//int LED = 13;

float temp;
int median[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
float onskadtemp = -1;
char number;
int p = 100;
int u;
int t=0;
float x;

float large;

void setup() {
 Serial.begin(9600);
 pinMode(tempPin, INPUT);
 pinMode(peltier, OUTPUT);

}
float Max
(float q, float w) {
 float result = 0;


 if (q > w) {
   result = q;
 }
 else if (w > q)
 {
   result = w;
 }
 return result;
}







void loop() {
 
 if ( Serial.available() > 0) {
   number = Serial.read();
   onskadtemp = number - '0';

 }


 if (0 < onskadtemp) {
   if (onskadtemp < 9) {



     temp = analogRead(tempPin);
   //  if (x<30){
     //  temp=x;
     temp = temp * 04,8828125;
     temp = temp / 100;
     temp=temp+10;
  //   }
  
 
delay(1);
t++;

     
       for (int i=0;i<10;i++)
       {
       median[i]=temp;
       large=Max(large,median[i]);
       }

       
     
     if (t==1000){
     Serial.print("TEMPRATUREN = ");
     Serial.print (temp);
     Serial.println ("*C");
     t=0;
     }

   




     if ( temp <= onskadtemp) {
       delay(1000);
     }
     else if (onskadtemp < temp) {
        u = p * (onskadtemp - temp);
         analogWrite(peltier, u);
        /* temp = analogRead(tempPin);
         temp = temp * 04, 8828125;
         temp = temp / 10;
         
       */
       /*
       analogWrite(peltier, HIGH);
       delay(5000);
       analogWrite(peltier, LOW);
       */
     }
   }
 }

}

Please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

You can edit your post to fix it up.

Post a copy of the serial terminal readings so we can see what you are getting.

Weedpharma

Julmat: The temperatures Serial monitor are printing out doesn't seem logic.

So I guess what you see is not what you expect.

  • What do you see?
  • What did you expect?
  • What is the difference? Answering these 3 questions will help everybody, especially yourself.

This is wrong:

temp = temp * 04,8828125;

That comma should be a point. Right now, you just multiply by 4.

This is wrong:

median=temp;

median is an array of ints. temp is a float.

You never do anything interesting with 'median' and 'large'. You assign values to them but you don't use them.

Thanks for your replies!

To anser your questions:

  • We can see that Serial monitor is printing out temperatures around 15*C. Thats not the temperatur in the room, so this seems wrong. (And it doesnt change if we place the temp sensor on the peltier either).

  • We want to use the Seriel monitor to select the temperatur we want, for example; if we write "7" in Serial Monitor, we would like the peltier to cool down to 7*C degrees.

Hope you understand what we mean! Many thanks/ Julia and Matilda

Please remove all the parts you have commented out. It make it hard to understand what you are doing.

Then use the auto format under the Tools menu.

Weedpharma

Julmat: Thanks for your replies!

To anser your questions:

  • We can see that Serial monitor is printing out temperatures around 15*C. Thats not the temperatur in the room, so this seems wrong. (And it doesnt change if we place the temp sensor on the peltier either).

Make a small sketch to only read the temperature sensor. Get that working with help here. Once you see what is different (to make it work right) you should at least have a handle on the problem.

Julmat: Serial monitor is printing out temperatures around 15*C. Thats not the temperatur in the room, so this seems wrong.

What would be your approach to troubleshoot this?

  • You read some value,
  • You do some calculation,
  • The result is wrong.

So either the value you read is wrong, or your calculation is wrong. To figure out which one it is, you have to test them separately.

Can you test that you read the correct sensor values, without doing your calculation? Can you test your calculation with values of your choosing, not from the sensor?

That's why I say make a small sketch to just do the one thing.

It's going to be a LOT quicker and easier. Big code gets less help here for good reason.

We are two students who are trying to program a Peliter and a temperarure sensor. When we write the temperature we want in Serial monitor, for example 7, the peltier starts to cool. And when the temperatur sensor read the correct temp we a the led to shine. Now that doesn’t work, but it’s probably just something wrong with the code.
Please can you help us? :slight_smile:

int peltier = 3;
int tempPin = A0;
int led = 13;
float voltvarde;
float advarde;
float temp;
int onskadtemp = -1;
char number;
int p = 100;
int u;
int t = 0;
int x;


void setup() {
  Serial.begin(9600);
  pinMode(tempPin, INPUT);
  pinMode(peltier, OUTPUT);
  pinMode(led, OUTPUT);
}


void loop() {

  if ( Serial.available() > 0) {
    number = Serial.read();
    onskadtemp = number - '0';
  }

  if (0 < onskadtemp) {
    if (onskadtemp <= 9) {


      advarde = (float)analogRead(tempPin);
      voltvarde=advarde*5.0/1023;
      temp = voltvarde/0.01;
      
      Serial.print("TEMPRATUREN = ");
      Serial.print(temp);
      Serial.println ("*C");
      delay(1000);
     

      if ( temp <= onskadtemp) {
  digitalWrite(led, HIGH);   
  delay(1000);

      }
      else if (onskadtemp < temp) {
        u = p * (onskadtemp - temp);
        analogWrite(peltier, u);
        analogWrite(peltier, 255);
        
      }
    }
  }

}

This is the fourth time that I have seen what is basically the same question.

Julmat:
Now that doesn’t work, but it’s probably just something wrong with the code.

Now that you have changed the calculation, do you now get the correct temperature values in your serial monitor?

      else if (onskadtemp < temp) {
        u = p * (onskadtemp - temp);

Here, onskadtemp < temp. So that means that (onskadtemp - temp) is a negative number. I don’t think that is what you want.

      advarde = (float)analogRead(tempPin);

Casting an int to a float, so the result can be stored in a float is pointless.

     else if (onskadtemp < temp) {
        u = p * (onskadtemp - temp);
        analogWrite(peltier, u);
        analogWrite(peltier, 255);
       
      }

What is the purpose of the 2nd and 3rd lines? If you are going to slam the pin to full on, it is pointless to set it to some possibly impossible value first.

Julmat: Please can you help us? :)

FIRST verify that your sensor is working properly, wired properly. Just read and check the raw data.

Next, don't convert temperature analog to volts, use the value 0 to 1023 to work with. Simpler, more accurate, faster.

Convert serial input to 0 to 1023 range, multiply by 10 so 9 entered becomes 90 to compare to 0 to 1023.

Much easier and faster to use but do check that 9C analog reads 90! I only went by your code.

Hello girls, Can you tell me this,

Have you written a program just to read the temperature of the LM35 sensor and send it to the serial monitor?

You need to program your project in stages, then put them together. So first lets get a program going that reads the sensor and puts its result on the monitor.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

We are all trying to help... Tom.... :)

From the old Real Programmer's Joke Sheet (I first saw in 85, it was old then),

How many programmers does it take to change a lightbulb?

Can't be done, it's a HARDWARE problem!