"Measuring the temperature"-sample not getting to work correctly

@BDub, thanks for your support. My wiring is:

image (as a new user I can’t include it here)

Hi Guys,

Very new to the electronics side of the picture. I picked up on of these for an outdoor project I’m slowly working on.

I’m getting some odd readings out of it. The analog reading seem to wildly vary between 2000 and 3000 so I get results like

( ( ( (2000 * 3.3) / 4095 ) - 0.5) * 100 ) = 111 degrees.

Definitely doesn’t sound right for cold wet Britain.

I have:

  • a jumper from 3.3* to the power,
  • a jumper from data line to A0
  • a jumper from GND to GND

probably missing a cap or resistor somewhere. Anybody able to tell me what I’m doing wrong?



See above - you are missing a capacitor.

Don’t think it’s just that. Having had a read in a few places, it looks like this uses OneWire.

I used @tidwelltimj’s implementation of that from here

Arduino bildr acticle on using this component
Data sheet

I think I’ll stick in a 4.7k resistor and test using the OneWire lib again. Unless someone thinks otherwise?

@elbeardo yeah it’s a OneWire:tm: component… which actually means digital communication over one wire, not analog voltage on one wire :wink:

Check out this post for how to hook it up and some code that should work for it.

That’s awesome. Thanks @BDub.

Am I right in thinking this is already included in the cloud IDE or do I have to add an #include to grab it? #n00bAlert

Hmm… well right now there is no way to add external libraries in the web IDE, but it’s coming soon I think.

The OneWire files are in this directory… https://github.com/mattande/core-firmware/tree/onewirehw/src
This is basically all of the core firmware with some extra files (libraries) added. Matt is building the firmware locally so he’s able to add all of those files seperately.

You can grab the content from all of the OneWire files and put them into one file with your program in a New App in the web IDE, but it’s a little work… not too bad. It would be like this:

Content of each of these stacked up in one file:
Application.cpp (what you normally write as a sketch in arduino IDE, but we just call it a program in the spark web IDE aka Sparkulator)

If this sounds like jibberish… probably best to wait until library support comes for the Sparkulator :wink:

Hi there,
I have my code working but I am not getting decimals in my result? is there a format number fuction?

temperature = ((((analogRead(A0)*

result: 17 and I would like to have decimals eg. 17,2

thank you for helping

It looks like the example is using an INT right now instead of a DOUBLE, so you won’t get any decimal places. You’d want to change:

int temperature = 0;
Spark.variable("temperature", &temperature, INT);


double temperature = 0;
Spark.variable("temperature", &temperature, DOUBLE);

We were having a problem with Spark.variable for DOUBLEs, but that’s fixed now.


Ok… I’ll say it. If you want consistent accurate temperature measurements I’d go for something like a BMP180 or a DHT22. They provide pretty accurate measurements and have the benefit of measuring pressure or humidity. Shop around you can get them at reasonable prices. You’ll save yourself a lot of time…

Dom, I think you meant to say “TEMPERATURE and humidity”. :wink:

pre-calibrated digital temperature sensors are great, I bought bunch of “DS18b20” OneWire sensors for around the house, and they seem pretty decent.

Well the BMP180 measures temperature and pressure… The DHT22 measures temperature and Humidity… Apologies if I wasn’t very clear.

Hei thank you for the prompt reply but I still have issues:

int temperature = 0;

void setup()
  Spark.variable("temperature", &temperature, DOUBLE);
  pinMode(A0, INPUT);

void loop()
  // Keep reading the temperature so when we make an API
  // call to read its value, we have the latest one
  temperature = ((((analogRead(A0)*3.0)/4095)-0.5)*100);

**which result now is:
** “cmd”: “VarReturn”,
“name”: “temperature”,
“result”: 4.053946672212173e-270,

With the INT instead of DOUBLE the result is:
“cmd”: “VarReturn”,
“name”: “temperature”,
“result”: 17,

Ho to get it right? I would like to have 1 decimal after 17.

thank you

//changed from an 'int'
double temperature = 0;
void setup()
  Spark.variable("temperature", &temperature, DOUBLE);

  pinMode(A0, INPUT);
void loop()
  // Keep reading the temperature so when we make an API
  // call to read its value, we have the latest one
  temperature = ((((analogRead(A0)*3.0)/4095)-0.5)*100);
1 Like

so I was writing the formula and Im sure you meant

temperature = ((((analogRead(A0) * 3.0) / 4095.0) -0.5) * 100.0);


Dave was copy/pasting badly formatted code from above… asterisks’ disappear because the forum thinks you are trying to italicize text

I fixed it.


Thank you! :slight_smile:

(space space space for the forum)