0

I have two temperature sensors, an MCP9808 and a DS18B20, connected to an arduino and I've read that it could be advantageous to set a small delay between temperature readings. What I wonder is where I should set the delay, in the setup function:

tempsensor.begin();                           //initialize communication with mcp9808
sensors.begin();                              //initialize communication with dc18b20
readSensors();     

or the readSensors function?:

temp = tempsensor.readTempC();              //read current temperature and save value inside "temp"
sensors.requestTemperatures();              // Send the command to get temperatures
temp2 = sensors.getTempCByIndex(0);

As you can probably tell I am very new to all of this, intuition tells me that I should set the delay within the readSensors function.

Any alternative suggestions about how I should be readings temperature data from two different sensors connected to one arduino would be appreciated.

EDIT
after re-reading question and based on first answer I haven't made it unequivocally clear my intention; I want to delay the time between the mcp9808 and the ds18b20 temperature readings, so that it waits X seconds between each. I have read that this may help to reduce noise.

  • (After your EDIT): These are 2 different devices. It is unlikely one affects the other. That said, reading them back to back should be feasible. The interval between reading the same device may be of concern. How fast you can do that may be in the specification sheets I linked to in my answer. As for reducing noise? As I said, these devices likely already implement some averaging. If you do not mind the added delay, you could add more averaging in your code to further "smooth" out the temperature response. Ask if you need me to add example of this in my answer. – st2000 Oct 1 '17 at 20:00
  • 1
    MCP9808 uses I2C; DS18B20 uses One Wire. Both are digital protocol that run on different pins and different wires: no cross talk and being digital, no noise. Or you read a temperature or you don't; there is no in-between. – user31481 Oct 1 '17 at 20:48
  • your proposed delay only makes sense using analog sensors powered solely by the MCU and being read solely by the MCU, and depending on the MCU. Consider a nodeMCU that immediately sends a server it's reading; the power spike from the wifi transmit could jitter the ADC's ref voltage and affect the next reading. that should not be an issue in your setup. – dandavis Oct 1 '17 at 22:47
1

The essence of your question is actually about managing processor to processor communications. As the temperature sensors you have chosen appear as peripheral processors which communicate to your processor over SMBus (MCP9808) or 1-Wire bus (DS18B20).

Both temperature sensors actually abstract you from the details of (likely) thermistor Analog to Digital Conversion (ADC) sampling and (likely) exponential averaging over multiple samples in order to reduce noise by providing a digital communication interface. As you can see, there may be many steps taken before the temperature data is available over the digital communication interface. Each step takes a finite amount of time.

In almost all cases there is a microprocessor busy at work inside the temperature probe. How well that processor's code is written dictates how much you can (skip reading the MCP9808 and DS18B20 specification sheet(s) and) "abuse" the protocol.

If you ask for the data faster then the temperature probe can obtain a new sample, in the best case, it is likely you will be supplied with duplicate values or values clearly designated as errors. In the worst case, you may get back incorrect values indistinguishable from normal values. Read the specification sheets and consider the source of the parts.

All this said, it may be that the 2 libraries you have chosen to use are not compatible. Yes, one library is using the SMBus / I2C approach and the other is using the 1-Wire bus approach. However, if both libraries use any single Arduino resource (for example a hardware timer or a hardware interrupt) at the same time, they may interfere with one another.

To test this, comment out the instantiation and code for one of the approaches and see if the other begins working as you expected in the first place. Then repeat the experiment commenting out the other.

In the end, it is almost always better to use as few libraries as possible when assembling software for an Arduino. There may be some effort to make some libraries work together. But it is impractical to test all library combinations. For this specific case, using several temperature devices of the same type (all using the same serial bus) and assigning different addresses to each would allow you to restrict your self to only including / using one library.

  • I'm waiting 30 seconds between requests. the reason for my concern is that the temperature readings for both sensors are almost perfectly correlated, on an absolute basis they're different but on a relative basis they follow one another so well I'm worried that they're in some way "working together" (as silly as this sounds). You mention the steps taken before the temperature reading is available, is there a way I can listen for this reading before moving on to the sensor reading of the ds18b20, or is this performed by default? – luke_mclachlan Oct 2 '17 at 8:23
  • 1
    It is almost certain 30 seconds is long enough to wait. Likely you need not worry until you get down to fractions of a second. I work a lot with temperature sensors. Often, when the ambient temperature is about 20C (~70F), I can cause a noticeable temperature change after a few seconds by just pinching my fingers around the probe or chip. Alternatively, you can use a hair-dryer or a can of freeze-spray. It is a good test, what you are doing, to validate the sensors independently. As for when the data is actually ready, maybe there is something in the specification sheet that could help. – st2000 Oct 2 '17 at 12:30
  • Ah, we all forgot to consider that you are using 2 different libraries. And that they may not be compatible. If you can not get "different" temperatures out of your 2 devices, consider my amended answer. – st2000 Oct 2 '17 at 13:43
1

I've read that it could be advantageous to set a small delay between temperature readings

Although temperature is typically a measure that changes slowly in time, there is nothing in programming/device side preventing you from making readings in short intervals; unless this added time is necessary to prevent issues on other tasks, but so it should be not related to that.

  • Apologies please read my edit, hopefully clear now. – luke_mclachlan Oct 1 '17 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.