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.

Llaves
  • 261
  • 3
  • 10
  • 1
    what is your question? – jsotola Mar 08 '22 at 03: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. – the busybee Mar 08 '22 at 06:46
  • 1
    Looks like an issue worth reporting to whoever maintains the Arduino extension for VSCode. – Edgar Bonet Mar 08 '22 at 08:15
  • Originally developed by Microsoft, the Arduino extension for vscode reportedly went open source circa 2017. It looks like the repository on GitHub takes bug reports https://github.com/Microsoft/vscode-arduino/issues – RowanP Mar 08 '22 at 13:09
  • There is discussion in some of the error reports at the GitHub repository regarding ongoing instability with the VSCode serial monitor and some ‘fairly significant’ code changes they’re working through. – RowanP Mar 08 '22 at 13:18
  • 1
    @EdgarBonet - I've submitted an issue to the github repo for the project. – Llaves Mar 08 '22 at 15:30
  • 1
    @RowanP - I've submitted an issue to the github repo for the project – Llaves Mar 08 '22 at 15:32

1 Answers1

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

RowanP
  • 819
  • 4
  • 21