5

I've got this simple program for testing DS18B20 temperature sensors.

#include "OneWire.h"
#include "DallasTemperature.h"

#define ONE_WIRE_BUS D3

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup()
{
  Serial.begin(19200);
  sensors.begin();
  delay(1000);  //time for serial to connect
  Serial.print(sensors.getDeviceCount());
  Serial.println(" sensors found");
  Serial.print(sensors.getDS18Count());
  Serial.println(" DS18 sensors found");
}

void loop()
{
  sensors.requestTemperatures(); // Send the command to get temperatures

  Serial.print("Temperature for Device 1 is: ");
  Serial.println(sensors.getTempFByIndex(0));

  Serial.print("Temperature for Device 2 is: ");
  Serial.println(sensors.getTempFByIndex(1));


  delay(5000);

}

I'm compiling for the Wemos D1 mini. When I compile on the Arduino IDE, the code runs as expected. It reports two devices, then gives the temperature reading from each device. When I compile using VSCode and the Arduino extension, the code compiles and uploads successfully, the program reports 2 devices, just like the Arduino IDE compilation does, but then it prints -196.60 for the temperature reading, which is the "no device" value.

Same breadboard, same host computer - the only difference is the IDE. I'd much prefer to stick with VSCode, but if the resulting executable is wrong, then it's a non-starter.

Why the different results? More importantly, how do I get the VSCode to produce the same (correct) output as the Arduino IDE?

UPDATE: Apparently the issue has something to do with the serial monitor. The project I'm actually trying to build has an OLED display for the temperature. The temperature is correct if the VSCode serial monitor is closed. Open the monitor and the incorrect values appears. Close the monitor and the correct values appear again.

UPDATE 2: The problem remains over a range of serial port speeds, from 19200 to 115200. I didn't try outside that range.

UPDATE 3: Code compiled using VSCode but run observing through the Arduino IDE serial monitor has the correct behavior - ie, shows the correct temperatures.

7
  • 1
    what is your question?
    – jsotola
    Commented Mar 8, 2022 at 3:38
  • What do you want to know? -- "Why is this?" can be quite difficult to answer, while "How to avoid?" is already answered by yourself. -- Since you have not shown the source with the display code, we cannot say anything on that case. Please edit your question. Commented Mar 8, 2022 at 6:46
  • 1
    Looks like an issue worth reporting to whoever maintains the Arduino extension for VSCode. Commented Mar 8, 2022 at 8:15
  • 1
    @EdgarBonet - I've submitted an issue to the github repo for the project.
    – Llaves
    Commented Mar 8, 2022 at 15:30
  • 1
    @RowanP - I've submitted an issue to the github repo for the project
    – Llaves
    Commented Mar 8, 2022 at 15:32

1 Answer 1

3

Further testing by OP isolated a bug in the Arduino Extension for VSCode which was logged as an issue at https://github.com/microsoft/vscode-arduino/issues/1467

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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