# Water pump lm35

Good evening, can someone give me a code that lets you run a 3v water pump when the lm35 reaches the desired temperature? thank you

``````if (readLM35 () >= threshold)
{
run3VwaterPump ();
}
``````

Not likely. You write the code and we help to get it working. If you really want someone to write the code for you, post in the Gigs and Collabarations section. Be prepared to pay and provide a detailed program specification and specifications of the hardware.

I have the following code, the temperature is always changing between negative and positive values which causes a pump to always turn on and off, I may have a code error, can someone help me?

``````float temperatura = 0.0;
int waterpump = 8;
void setup(){

Serial.begin (9600);

pinMode(8,OUTPUT);
}

void loop(){

Serial.println (temperatura);
delay (2000);

if (temperatura < 14){
digitalWrite(8, LOW);

}else
digitalWrite(8,HIGH);

}
``````

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

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

Thanks.. Tom... You may have a type casting issue in your equation.

try

Then comes the question, why multiply by 0.5 and divide by 0.01? x0.5/0.01 is the same as x50.0.

``````analogRead(0); //Discard this reading
temperatura = (float)analogRead(0) * 0.244f; //Optimized math
``````

Something to do with initialization of ADC.

Initialization as in the first time. Your suggestion does it everytime.

What kind of temperature sensor are you using?
Looks like you need some hysteresis (dead band), try:

``````if (temperatura < 14.0)
digitalWrite(8, LOW);
else if(temperatura > 15.0)
digitalWrite(8,HIGH);
``````

Hi,

``````temperatura = (0.5 * analogRead(0)*5/(1023))/0.01;
``````

Should be;

``````temperatura = (0.5 * analogRead(A0)*5/(1023))/0.01;
``````

At the moment you are trying to read one of the program comms pins.
Tom.. temperatura = (0.5 * analogRead(0)*5/(1023))/0.01; Post#3

temperatura = (0.5 * analogRead(0)*5/(1023))/0.01; Post#5

try

temperatura = (float)analogRead(0) * 0.244f; Post#6

temperatura = (0.5 * analogRead(A0)*5/(1023))/0.01; Post#11

1. The response equation of LM35: T0C = 100*SignalOutFromSensor
Assuming VREF = 5V (DEFAULT)
==> T0C = 100
float T = 100
(5.0/1024)*analogRead(A0); //tested; OK! shows: 25.37 C

//------ from Post#3---------------------------------------
float temperatura = (0.5 * analogRead(0)*5/(1023))/0.01; //tested: not OK! shows: 14.91 C

//----- from Post#5-----------------------------------------
temperatura = (0.5 * (float)analogRead(0)*5.0/1024.0)/0.01; //tested: not OK! shows: 14.14 C

//-----from Post#6-------------------------------------------
temperatura = (float)analogRead(0) * 0.244f; //tested: not OK! shows: 14.13 C

//----- from Post#11------------------------------------------
temperatura = (0.5 * analogRead(A0)*5/(1023))/0.01; //tested: not OK! shows: 14.66 C

//-------------------------------------------------------------------------------------------

The equations are failing; because, they are not logically formed.
For input signal 5V (when VREF = DEFAULT), the ADC value is 1023 (or 1024)
For input signal VDT (SignalOutFromSensor), the ADC value is (1023/5)*SignalOutFromSensor

Therefore, the response equation of LM35 becomes as:
T0C = 100SignalOutFromSensor
==> T0C = 100

We want to present the temperature as integer, the declaration is:
unsigned int T = 100*(5/1023)*analogRead(A0); //no fractional part will be shown

We want to present th etemperature as flloat, the declrarion is:
float T = (float)100*(5/1023)*analogRead(A0); //this is preferred to the next one.

OR

float T = 100*(5.0/1023)*analogRead(A0); //it is enough to have one factor is float. But, which one?

2. Interestingly, if we change the order of factors of the temperature equation of this Post, the response goes crazy! (I saw a post in the Forum having a change in the order of the placement of the factors.)

float T = 100*(5.0/1024)*analogRead(A0); //tested; OK! shows: 25.45 C

==> float T = 100*(analogRead(A0)/1024)*5.0; //tested: not OK! shows: 0.00

//-------------------------------------------------------------------------------------- ``````void setup()
{
Serial.begin(9600);
analogReference(DEFAULT);
}

void loop()
{
//Serial.pruntln(T, 2);
//  float temperatura = (0.5 * analogRead(0)*5/(1023))/0.01;
//float temperatura = (0.5 * (float)analogRead(0)*5.0/1024.0)/0.01;
//float temperatura = (float)analogRead(0) * 0.244f;
//float temperatura = (0.5 * analogRead(A0)*5/(1023))/0.01;
//Serial.println(temperatura, 2);
Serial.println(T, 2);
delay(2000);

}
``````

TomGeorge:
Hi,

``````temperatura = (0.5 * analogRead(0)*5/(1023))/0.01;
``````

Should be;

``````temperatura = (0.5 * analogRead(A0)*5/(1023))/0.01;
``````

At the moment you are trying to read one of the program comms pins.
Tom.. Tom - actually not.

On a UNO this is how A0 is defined

``````static const uint8_t A0 = 14;
``````

or on a MEGA`static const uint8_t A0 = 54;`

if you look at the source code for analogRead you'll see that they do some math to bring back the pin number between 0 and n-1 (whatever the number of analog pins you have) and this is also documented as such:

Syntax

Parameters
pin: the number of the analog input pin to read from (0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega)

This is the only function that works from a channel though, if you use the wrongly named `analogWrite()` (as it is used for PWM) then you need to pass the appropriate pin number and 0 will not be A0 in that case.

Agree though it's confusing and best practice is to use A0 IMHO

GolamMostafa:
1. The response equation of LM35: T0C = 100*SignalOutFromSensor
Assuming VREF = 5V (DEFAULT)
==> T0C = 100
float T = 100
(5.0/1024)*analogRead(A0); //tested; OK! shows: 25.37 C

//------ from Post#3---------------------------------------
float temperatura = (0.5 * analogRead(0)*5/(1023))/0.01; //tested: not OK! shows: 14.91 C

//----- from Post#5-----------------------------------------
temperatura = (0.5 * (float)analogRead(0)*5.0/1024.0)/0.01; //tested: not OK! shows: 14.14 C

//-----from Post#6-------------------------------------------
temperatura = (float)analogRead(0) * 0.244f; //tested: not OK! shows: 14.13 C

//----- from Post#11------------------------------------------
temperatura = (0.5 * analogRead(A0)*5/(1023))/0.01; //tested: not OK! shows: 14.66 C

float T = 100*(5.0/1024)*analogRead(A0); //tested; OK! shows: 25.45 C

==> float T = 100*(analogRead(A0)/1024)*5.0; //tested: not OK! shows: 0.00

The equations are failing; because, they are not logically formed.

Please try your explanation again. And I will give you a hint, it has nothing to do with any of this:

GolamMostafa:
For input signal 5V (when VREF = DEFAULT), the ADC value is 1023 (or 1024)
For input signal VDT (SignalOutFromSensor), the ADC value is (1023/5)*SignalOutFromSensor

Therefore, the response equation of LM35 becomes as:
T0C = 100SignalOutFromSensor
==> T0C = 100

We want to present the temperature as integer, the declaration is:
unsigned int T = 100*(5/1023)*analogRead(A0); //no fractional part will be shown

We want to present th etemperature as flloat, the declrarion is:
float T = (float)100*(5/1023)*analogRead(A0); //this is preferred to the next one.

OR

float T = 100*(5.0/1023)*analogRead(A0); //it is enough to have one factor is float. But, which one?

Notice that you have to results listed as OK that are almost 0.1 degrees apart, but yet the two results that are 0.01 degrees apart are not OK. ? ? ?

Post #3 and post#11 are absolutely the same thing. So as to how you got different results is beyond reasoning. The only change is the pin number used in the IDE, but the compiler resolves both back to the same ADC configuration and input.

Post #5 and Post #6 are likely only different due to a truncation issue. Post #6 lists the fact as 0.244 but if you calculate the factors from post #5 it actually computes to be 0.244140625. If you use the whole thing then post #5 and #6 have the same result. The only same result throughout the entire list. Hmmm?

Try to use internal vRef:

``````#define LM35PIN A0

void setup() {
pinMode(LM35PIN, INPUT);
analogReference(INTERNAL);
Serial.begin(9600);
}

void loop() {
for (byte i = 0; i < 5; i++) reading += analogRead(LM35PIN);
//Both above shortened to:
delay(1000);
}
``````

This works with LM35DZ and Uno and yields higher resolution than default vRef.

Why do you bother to set the pinMode of an analogue input?

TolpuddleSartre:
Why do you bother to set the pinMode of an analogue input?
Tom.. 