Go Down

Topic: Temp sensor LED  (Read 175 times) previous topic - next topic

darinsquared

I wrote the attached sketch.  When I run the program 'firstled' blinks.  I heated up the sensor with a hair dryer but it does not cause the remaining led's to light up.

Do I have an issue with not converting temperature to Celsius?  do I have an error in my code?

By the way this is my first attempt at Arduino programming.  Maybe I should have started with the blink test.

outsider

#1
Dec 16, 2016, 11:14 pm Last Edit: Dec 16, 2016, 11:46 pm by outsider
First replace:
Code: [Select]
float temperatureInCelsius = sensors.getTempCByIndex(SENSOR_INDEX);
With:
Code: [Select]
tempCDS18B20 = sensors.getTempCByIndex(SENSOR_INDEX);
See if that works.
OOPs, int tempCDS18B20; should be float tempCDS18B20;

darinsquared

First replace:
Code: [Select]
float temperatureInCelsius = sensors.getTempCByIndex(SENSOR_INDEX);
With:
Code: [Select]
tempCDS18B20 = sensors.getTempCByIndex(SENSOR_INDEX);
See if that works.
OOPs, int tempCDS18B20; should be float tempCDS18B20;

I tried that and no difference.

 I do not think it is reading the temp (DS18B20).  I changed the 'if' statement to <= 0 and the first 'else' statement to >=1...  this should make the second LED light up instead of first  blinking ,but it does not, the first still blinks.  Do I need to have a Celsius conversion formula in my sketch?  (I assumed there is one in <DallasTemp.h>).  I checked the other LED's by adding digitalWrite to void setup and they turn on during setup.  I checked for continuity between sensor wires > breadboard> arduino and there is no problem.  I did find I get a voltage reading [at sensor] between the signal wire and data wire.

darinsquared

here is the updated sketch.  I did a print.ln and all I get is -271 (I assume K) as you see in the sketch I replaced the temps with K equivalents.

I based my wiring from this: http://www.theorycircuit.com/temperature-level-bar-graph-using-lm35-with-arduino/

I omitted the buzzer and added blinks. using 4K7 resistor between data and ground. I used a new Ds18B20 with waterproof end i.e. pre-made. 

I tried heating up the sensor and it did not register a change.  I will re check wiring for 10th time.

outsider

WAIT! STOP! DS18B20 and LM35 are totally different! Post a diagram of your wiring.

darinsquared

WAIT! STOP! DS18B20 and LM35 are totally different! Post a diagram of your wiring.
Yes, I know.   that was just a reference for how it was setup for the lights. I did the resistors as per DS18B20.

MarkT

#6
Dec 18, 2016, 10:05 pm Last Edit: Dec 18, 2016, 10:10 pm by MarkT
getTempCByIndex returns a float, not an int

Assign that return value to a float variable, then in your if-else-if chain use the same variable!

You first version assigned the result to a different variable from the one you tested,
the later one assigned it to an int which you then tested against over-large values.

If you do
#include <DallasTemperature.h>
Then be sure to actually look at that include file yourself to see what each function does and what
its arguments are and return type is!

And please indent your code properly, and please make it readable:

Hard to read:
Code: [Select]
    else if(tempCDS18B20>=17.5&&tempCDS18B20<20)

Easy to read:
Code: [Select]
    else if (temp >= 17.5 && temp < 20)

Lack of whitespace makes it hard to see silly errors, and make sense of the code, over complex
variable name is just visual noise - the variable is a temperature, whether it came from a DS18B20 or an LM35 or an XYZ3823823dcSDCds7383 is irrelevant to code that is comparing temperatures.

Break the code down into small functions with concise but meaningful names and it will become
much easier to read and maintain too.  Any function over 20 lines is a clue you are not structuring
the code enough.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy