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.