DS18B20 works on UNO but shows -127 on MKR 1010

Hi, may someone please help me understand where my error is connecting the DS18B20 waterproof sensor to the Arduino Opla through the MKR Wifi 1010?

I have correct temperature readings from the sensor using UNO but when I connect it to MKR 1010 and run a modified sketch (originally the Smart Garden project) to also take the DS18B20 input, I can't get the temperature, just reading -127 (the error code?).

The sensor cable is properly connected through a PlugableTerminal to MKR's VCC (3.3V), GND and PIN 6.

I went through many related posts but couldn't pin the issue, also just starting with Arduino IoT, I would really appreciate any suggestions, thank you!

#include <OneWire.h>
#include <DallasTemperature.h>

#include "thingProperties.h"
#include <Arduino_MKRIoTCarrier.h>
MKRIoTCarrier carrier;

#define ONE_WIRE_BUS 6

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

int moistPin;
 
String waterPumpState;
String coolingFanState;
String lightState;
 
uint32_t lightsOn = carrier.leds.Color(82, 118, 115);
uint32_t lightsOff = carrier.leds.Color(0, 0, 0);
 
void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  sensors.begin();
  sensors.setResolution(12);
  
  // Defined in thingProperties.h
  initProperties();
 
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  //Get Cloud Info/errors , 0 (only errors) up to 4
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
 
  //Wait to get cloud connection to init the carrier
  while (ArduinoCloud.connected() != 1) {
    ArduinoCloud.update();
    delay(500);
  }
 
  delay(500);
  CARRIER_CASE = false;
  carrier.begin();
  moistPin = carrier.getBoardRevision() == 1 ? A5 : A0; //assign A0 or A5 based on HW revision
  carrier.display.setRotation(0);
  delay(1500);
}
 
void loop() {
  //Update the Cloud
  ArduinoCloud.update();
  sensors.requestTemperatures(); 
  
  //read temperature and humidity
  temperature = carrier.Env.readTemperature();
  humidity = carrier.Env.readHumidity();
  water = waterTemperature();
  
  //read raw moisture value
  int raw_moisture = analogRead(moistPin);
 
  //map raw moisture to a scale of 0 - 100
  moisture = map(raw_moisture, 0, 1023, 0, 100);
 
  //read ambient light
  while (!carrier.Light.colorAvailable()) {
    delay(5);
  }
  int none; //We dont need RGB colors
  carrier.Light.readColor(none, none, none, light);

  Serial.print("Celsius temperature: ");
  Serial.print(sensors.getTempCByIndex(0)); 
  Serial.print(" - Fahrenheit temperature: ");
  Serial.println(sensors.getTempFByIndex(0));
  //water = sensors.getTempCByIndex(0); 

  delay(100);
}

float waterTemperature() {
  sensors.requestTemperatures();
  float waterTemp = sensors.getTempCByIndex(0);
  return waterTemp;
}

void onWaterpumpChange() {
  if (waterpump == true) {
    carrier.Relay2.open();
    waterPumpState = "PUMP: ON";
  } else {
    carrier.Relay2.close();
    waterPumpState = "PUMP: OFF";
  }
  updateScreen();
}
 
void onCoolingFanChange() {
  if (cooling_fan == true) {
    carrier.Relay1.open();
    //coolingFanState = "FAN: ON";
    coolingFanState = "TEMP: C";
  } else {
    carrier.Relay1.close();
    //coolingFanState = "FAN: OFF";
    coolingFanState = "TEMP: F";
  }
  updateScreen();
}
 
void onArtificialLightChange() {
  if (artificial_light == true) {
    carrier.leds.fill(lightsOn, 0, 5);
    carrier.leds.show();
    lightState = "BLACK: 100C";
  } else {
    carrier.leds.fill(lightsOff, 0, 5);
    carrier.leds.show();
    lightState = "GREEN: 70C";
  }
  updateScreen();
}
 
//Update displayed Info
void updateScreen() {
  carrier.display.fillScreen(ST77XX_BLACK);
  carrier.display.setTextColor(ST77XX_WHITE);
  carrier.display.setTextSize(3);
 
  carrier.display.setCursor(40, 50);
  carrier.display.print(water);
  carrier.display.setCursor(40, 90);
  carrier.display.print(coolingFanState);
  carrier.display.setCursor(40, 130);
  carrier.display.print(lightState);
}

Here is also the thingProperties.h:

// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char SSID[]     = SECRET_SSID;    // Network SSID (name)
const char PASS[]     = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)

void onArtificialLightChange();
void onCoolingFanChange();
void onWaterpumpChange();

float humidity;
float temperature;
float water;
int light;
int moisture;
bool artificial_light;
bool cooling_fan;
bool waterpump;

void initProperties(){

  ArduinoCloud.addProperty(humidity, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(temperature, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(water, READ, 2 * SECONDS, NULL);
  ArduinoCloud.addProperty(light, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(moisture, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(artificial_light, READWRITE, ON_CHANGE, onArtificialLightChange);
  ArduinoCloud.addProperty(cooling_fan, READWRITE, ON_CHANGE, onCoolingFanChange);
  ArduinoCloud.addProperty(waterpump, READWRITE, ON_CHANGE, onWaterpumpChange);



}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

Hello

Could try the example sketch directly on your Arduino without using IoT just to isolate the problem?
Also, share your schematic pls

You don't mention it, but I assume that you have the pull up resistor present on the DS18B20 DQ line?

I have read that some (fake) Ds18B20 don't work well on 3.3volt power.
You can power the sensor from 5volt on a 3.3volt-logic board, as long as you pull up the data pin to 3.3volt.
Leo..

I also had troubles using the (fake) DS18B20 sensors with my 3.3v MKR boards. Particularly with cable lengths longer than a couple feet.
Heres a few things that helped it work for me.(applies to any dallas network)

#1 Adjust the pull up resistance. I believe 3k3 worked in my case.

#2 Add a 100nF capacitor between Vcc and grd at the sensor.

#3 Add a 100 ohm resistor in series between the arduino input pin and the DS18B20 output.Place it closest to the arduino.

On one project because I had multiple sensors on a long cable( >15 meters )
I had to buy genuine DS18B20's to get it to work reliably.

Dear all, thanks to your helpful suggestions, I disconnected the MKR 1010 from OplĂ  to test my original DS18B30 on pin 6 and it worked with 3.3V. So I looked at the MKR and OplĂ  pinouts (that I should have started with) and it seems pin 6 was taken with all the sensor inputs from OplĂ . I switched to pin 2 and voilĂ ... it reads temperature properly!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.