One wire sensor data gets weird when I open the Arduino's serial monitor

Hey guys, I am recently using Arduino to test a one wire protocol temperature sensor chip.
I built a breakout board based on this chip and wrote the related Arduino library. The example code is to get the temperature of the sensor via one wire protocol and print it via usb serial port.

  • But when I tested the example, something strange happened.The sensor's temperature data sometimes turns negative in a room temperature environment, but sometimes it does turn normal. And there is no pattern to this at all, it is completely random.Sometimes the same computer, the same Arduino, will have normal data in the morning and then not in the afternoon.A similar thing happens with all five of my temperature sensors.

  • With a deeper investigation, I used a logic analyzer to connect to the gnd/data of the ONE WIRE temperature sensor. something even stranger happened, when I finished uploading the example code, but did not turn on the serial monitor, the temperature data was perfectly fine when I watching it through logic analyzer. But when I open the serial monitor, there is a high probability that the temperature goes negative.
    I have tried to test and reproduce the issue on the uno/mega2560, and I did use a third party serial monitor like putty. The results are all similar: as long as the serial monitor is turned on, the temperature data is likely to be abnormal.

  • I know that the Arduino resets once when the serial monitor is turned on, but I don't think that this is causing any anomalies in the readings of this single bus temperature sensor.

  • I am investigating this matter with the AE of that temperature sensor chip for the past two weeks, but we are clueless so far. I do know that this may not have anything to do with the Arduino, but we've tried many possible solutions and unfortunately haven't fully resolved the matter. Hopefully anyone can make suggestions to point us in the right direction.
    I've attached the library I'm using at the end of the topic, if you need more info I'll provide that as soon as possible as well.

  • The temperature get code I'm using is like the following. In the .ino code, I just Serial.print() the temperature gets from this function. As long as I open the Serial monitor, the temperature will become negative. And the data frame of the wrong data will still pass the CRC.
    I start to think there might be some strange act take by the Arduino when the serial monitor is open causing this issue.

float DFRobot_CT1780::getCelsius(uint8_t *newAddr){
  if(newAddr==NULL){
    return NAN;
  }
  uint8_t data[9];
  reset();
  select(newAddr);
  write(CMD_CONVERT_T);
  delay(750); // Wait for the conversion to complete
  
  reset();
  select(newAddr);
  write(CMD_READ_SCRATCHPAD);
  
  for(int i = 0; i < 9; i++) {
    data[i] = read();
  }
  
  if(isAllEqual(data,9)||crc8(data, 8) != data[8]) {
    return NAN;
  }
  
  // Analytic temperature data
  int16_t rawTemperature = (data[1] << 8) | data[0];
  rawTemperature >>=2;
  if (rawTemperature & 0x2000) {
    rawTemperature |= 0xC000;
  }
  float temp = rawTemperature * 0.25f;// The temperature resolution is 0.25°C
  if(temp>1768 || temp<(-270)){
      return NAN;
  }
  return temp; 
}

Full code/Library:

Any advice is appreciated.

Which temperature sensor are you using? Can you try with DS18B20 sensor?

ds18b20 could work perfectly, I’m using the CT1780:

This is a new temperature sensor which I’m prototyping.
I will upload the data sheet of ct1780 tomorrow

It is actually possible (indeed, very likely) that the problem lies in your code, which you haven't shared.
Please read:

Also, this isn't an IDE problem, so I've suggested a moderator place your thread in a more appropriate location.
Thanks

1 Like

Apologize for posting the topic in the wrong tab. I’m new to the forum.
And regarding the code, I included a link to the GitHub library at the very end of the topic. Since I wasn't sure which part of the code was causing the problem, I posted all the code on GitHub.

You'll find many here who won't bother to go fetch it from Github, myself included. Sorry, but in my opinion, if you want help, you can make it easy for helpers.

1 Like

Yes, you are right. My bad.
I added the read temperature part of code in my topic.(If there is something relate to my code, I think this must be the wrong part) Hope someone could point me the right direction.

You are just using a temperature sensing element (CT1780). You need to add enough hardware with this element to encode/decode one-wire Protocol of OneWire.h unless you are devising a new One-Wire Protocol of your own.

You'll find this annoying, I'm sure, but I'll quote a small portion of the how-to I linked you to earlier, in the hope that you'll take a read:

Code problems

We can only find problems in code we can see. Please supply your complete code in code tags <CODE/> More about posting code and using code tags. If you are using Arduino Cloud then you can post a link to your project (but keep in mind that not everyone uses Arduino Cloud).

Posting a snippet of code is generally useless. The problem is usually in another part of the program.

Before posting code, please use Tools / Auto Format at the top of the IDE. It makes the code much easier to read and you will probably find it very helpful yourself. Once you've done that, use Edit / Copy for Forum.

In short, if you really knew where the problem is, you'd be fixing it. Assuming you know where it is, and only providing that snippet, wastes everyone's time.

That was posted to Github 3 months ago, which appears to be a DFRobot repository of some sort. What does that have to do with your post today?

Do you work for DFRobot?

For help on this forum, please read and follow the directions in the "How to get the best out of this forum" post, linked at the head of every forum category.

Topic, and author, muted.

I think the problem is not that hard to solve but without an annotated schematic showing all connections, power, ground and power sources and the code posted here I cannot be of any help. I am another one that does not go to outside links. If it takes to much time to post it then it definitely will take us to much time to read it elsewhere.

1 Like