DS18B20 Sensor

DS18B20 sensors won't work with Arduino Mega

They work fine with a Arduino UNO.

Please, post your codes with code tags </>.

It outputs 0 found on buss and displays 32°

//YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
//-----( Import needed libraries )-----*/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define ONE_WIRE_BUS_PIN 2

/-----( Declare objects )-----/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);

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

/-----( Declare Variables )-----/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress Probe01 = { 0x28, 0xFF, 0x65, 0x6D, 0x22, 0x17, 0x03, 0x3D };
DeviceAddress Probe02 = { 0x28, 0xFF, 0x63, 0x35, 0x22, 0x17, 0x03, 0x65 };
DeviceAddress Probe03 = { 0x28, 0xFF, 0x65, 0x72, 0x22, 0x17, 0x03, 0x9F };
DeviceAddress Probe04 = { 0x28, 0xFF, 0x0B, 0x33, 0x22, 0x17, 0x03, 0x43 };
DeviceAddress Probe05 = { 0x28, 0xFF, 0xD2, 0x30, 0x22, 0x17, 0x03, 0x66 };
DeviceAddress Probe06 = { 0x28, 0xFF, 0xFA, 0x71, 0x22, 0x17, 0x03, 0xCF };
DeviceAddress Probe07 = { 0x28, 0xFF, 0xB5, 0x6D, 0x22, 0x17, 0x03, 0x63 };
DeviceAddress Probe08 = { 0x28, 0xFF, 0x16, 0x46, 0x22, 0x17, 0x03, 0xFA };
//DeviceAddress Probe09 = { 0x28, 0xFF, 0x96, 0x41, 0x22, 0x17, 0x03, 0xB7 };
//DeviceAddress Probe10 = { 0x28, 0xFF, 0xD7, 0x40, 0x22, 0x17, 0x03, 0x1C };
DeviceAddress Probe22 = { 0x28, 0xFF, 0x76, 0x8D, 0x90, 0x17, 0x05, 0x68 };

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
int tempC = 0;

void setup() /****** SETUP: RUNS ONCE ******/
// start serial port to show results
lcd.begin (20, 4);
lcd.backlight(); //Turn on the backlight
Serial.print("Initializing Temperature Control Library Version ");

// Initialize the Temperature measurement library

// set the resolution to 10 bit (Can be 9 to 12 bits … lower is faster)
sensors.setResolution(Probe01, 10);
sensors.setResolution(Probe02, 10);
sensors.setResolution(Probe03, 10);
sensors.setResolution(Probe04, 10);
sensors.setResolution(Probe05, 10);
sensors.setResolution(Probe06, 10);
sensors.setResolution(Probe07, 10);
sensors.setResolution(Probe08, 10);

int Temp01 = 0;

void loop() /****** LOOP: RUNS CONSTANTLY ******/
Serial.print("Number of Devices found on bus = ");
Serial.print("Getting temperatures… ");

// Command all devices on bus to read temperature

Serial.print("Probe 01 ");
lcd.setCursor(0, 0); // Set curson to space 0 line 1
lcd.print("1 "); // Write T1 value to LCD

Serial.print("Probe 02 ");
lcd.setCursor(0, 1); // Set curson to space 0 line 2
lcd.print("2 "); // Write T1 value to LCD

Serial.print("Probe 03 ");
lcd.setCursor(0, 2); // Set curson to space 0 line 1
lcd.print("3 "); // Write T1 value to LCDSerial.println();

Serial.print("Probe 04 ");
lcd.setCursor(0, 3); // Set curson to space 0 line 1
lcd.print("4 "); // Write T1 value to LCD

Serial.print("Probe 05 ");
lcd.setCursor(11, 0); // Set curson to space 0 line 1
lcd.print("5 "); // Write T1 value to LCD

Serial.print("Probe 06 ");
lcd.setCursor(11, 1); // Set curson to space 0 line 1
lcd.print("6 "); // Write T1 value to LCD

Serial.print("Probe 07 ");
lcd.setCursor(11, 2); // Set curson to space 0 line 1
lcd.print("7 "); // Write T1 value to LCD

Serial.print("Probe 08 ");
lcd.setCursor(11, 3); // Set curson to space 0 line 1
lcd.print("8 "); // Write T1 value to LCD

//–(end main loop )—

/-----( Declare User-written Functions )-----/
void printTemperature(DeviceAddress deviceAddress)

float tempC = sensors.getTempC(deviceAddress);
int x = 0;

if (tempC == -127.00)
Serial.print("Error getting temperature ");

//Serial.print(" F = ");

DS18B20 sensors won't work with Arduino Mega

They work fine with a Arduino UNO.

Nonsense. They work fine with both - no change to code or wiring required.

The reason why yours don't work is because you have changed the code or the wiring. "Change of wiring" also includes incompetent connection. You will get better returns if you explain the real problem more clearly.

Here's how to make them work:


I have been trying to get the DS18B20 to work with a Arduino Mega 2560. I had it working with a Arduino UNO but now it won't even work with an Arduino UNO. I tried in on 2 different UNO's and one MEGA with different breadboards.I used a Ohm meter to check the wiring. and it all checked out. I even hardwired it and still get:

"Sensor Resolution: 0"
"Temp C: -127.0000 Temp F: -196.6000 "

which is a bad connection or bad sensor.

I have all the sensor addresses, and I checked them twice.

Tried several different sensors and get the same thing.

I had 8 DS18B20 on a buss and it worked great, even sent the temperatures to a LCD screen.

I even reloaded Arduino 1.8.5 and the OneWire.h and DallasTemperature.h libraries.

I'm using ground for the left terminal DQ to A2 with a 4.7k Ohm resistor to +5 in the center and +5 to the right, looking at the flat side.

I ran the blink Sketch and it ran fine.

I even ran the sketch to test for addresses and get.

"Looking for 1-Wire devices..."

Running out of options to try.

Is there a way I can test the sensors to see if they are bad?

Thank you,

I had it working with a Arduino UNO but now it won't even work with an Arduino UNO.

So what changed?

"Temp C: -127.0000 "
which is a bad connection or bad sensor.

This is correct. More likely the first option, in my experience.

I even ran the sketch to test for addresses and get.

"Looking for 1-Wire devices…"

Did it give any addresses? Or is it telling you it is done looking for addresses and didn’t find any because there are no sensors connected?

Is there a way I can test the sensors to see if they are bad?

I rather suspect you have done that. If you are getting consistent “-127”, and it appears that you are getting that on two Unos and a Mega, i.e. the second line of your original post is no longer true, and you have checked each wire and each solder point is kosher, I doubt anybody on this forum can say anything other than that your sensors are buggered.

Let us start again:

A: Checking that DS18B20 is Present

1. Remove everything from the breadboard.

2. Build the following connection among UNO, SM1, and DS18B20.

3. Upload the following sketch to detect the presence of the sensor by reading its unique 64-bit ROM/Address Code:

//12-bit default resolution; external power supply
#define OneWireBusPin 3
OneWire ds(OneWireBusPin);  //3
byte addr[8];         //to hold 64-bit ROM Codes of DS18B20 sensor

void setup()

void loop()
  ds.reset();         //sensor is reset
  ds.search(addr);    //collect 64-bit (8-byte) ROM/Addres code Sensor (DS1)
  Serial.print("Address of DS18B20: ");
  for (int i = 0; i < 8; i++)
    Serial.print(addr[i], HEX);   //show on SM

4. Check that the following message/similar message (every sensor has its own 64-bit ROM/Address Code) has appeared on SM.

5. Repeat all the above steps (1 - 4) using MEGA and check that the following ROM/Address of the sensor has appeared on SM>

I ran the sketch to search for addresses.
Tried again and the same results
I get
"Addresses of DS18B20: 00000000"
Wiring connections are correct, checked them with an Ohm meter. No shorts or opens.

Also there is 4.98 Volts DC at the DS18B20 VDD and GND.
dQ is connected to A3

I ordered some DS18B20 and another UNO.

If you wire them up backwards they will quickly get very hot and shortly afterwards fail (don't ask how I know). When you get the replacement be sure of your wiring and put a finger on the device when you apply power and yank it if things start getting hot.

(don't ask how I know).

I know exactly how you know,
but mine survived and are still in service. They were hot for several seconds and I think it is some tribute to the manufacturers that they stood up to that sort of abuse.

Also there is 4.98 Volts DC at the DS18B20 VDD and GND.

I could mean that you are also measuring 4.98V at the GND-pin of DS18B20 sensor, and it could happen if the GND-pin of the sensor is not connected with the GND-pin of UNO. (floating pin?)

So, the statement should be:
Also there is 4.98 Volts DC at the DS18B20 VDD with respect to its GND pin.

Found the problem. Was using A2 instead of D2. I thought Temperature sensors were analog.

But not all of the sketches will work extracting addresses. So that was part of my problem.

Switched from LM34 to DS18B20.