I've just been experimenting with some code i downloaded for the DS18B20 temp sensor, and i've been experimenting with having more than 1 sensor on a Pin using a BUS. When i connect the 2nd sensor and reset the board or upload the new code with the 2nd sensor connected it looses the 1st sensor..
This is the code i've been running to auto asign the sensors on the BUS.
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3 // Data wire is plugged into digital pin 3 on the Arduino
#include <LiquidCrystal.h>
//LCD pin to Arduino
const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;
const int pin_BL = 10;
LiquidCrystal lcd( pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);
OneWire oneWire(ONE_WIRE_BUS);// Setup a oneWire instance to communicate with any OneWire device
DallasTemperature sensors(&oneWire); // Pass oneWire reference to DallasTemperature library
int deviceCount = 0;
float tempC1 = 0;
float tempC2 = 0;
float tempC = 0;
void setup() {
sensors.begin();// Start up the library
lcd.begin(16, 2);
lcd.clear();
pinMode(3, OUTPUT);
analogWrite(3, 0);
Serial.begin(9600);
Serial.print("Locating devices on Bus 3...");// locate devices on the bus
Serial.print("Found ");
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount, DEC);
Serial.println(" devices.");
Serial.println("");
}
void loop() {
sensors.requestTemperatures();// Send command to all the sensors for temperature conversion
for (int i = 0; i < deviceCount; i++) // Display temperature from each sensor
{
Serial.print("Sensor ");
Serial.print(i+1);
Serial.print(" : ");
tempC = sensors.getTempCByIndex(i);
Serial.print(tempC);
Serial.print((char)176);//shows degrees character
Serial.print("C | ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
Serial.print((char)176);//shows degrees character
Serial.println("F");
}
Serial.println("");
delay(1000);
}
I then tried to run the following code to get the 64-bit address assigned to them, and now nothing i can't even find the 1st one.
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// variable to hold device addresses
DeviceAddress Thermometer;
int deviceCount = 0;
void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// locate devices on the bus
Serial.println("Locating devices...");
Serial.print("Found ");
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount, DEC);
Serial.println(" devices.");
Serial.println("");
Serial.println("Printing addresses...");
for (int i = 0; i < deviceCount; i++)
{
Serial.print("Sensor ");
Serial.print(i+1);
Serial.print(" : ");
sensors.getAddress(Thermometer, i);
printAddress(Thermometer);
}
}
void loop(void)
{}
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
Serial.print("0x");
if (deviceAddress[i] < 0x10) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
if (i < 7) Serial.print(", ");
}
Serial.println("");
}
I've disconnected everything from the breadboard and started again in case something had come loose but still nothing. Any ideas?
I've just tested all the leads on the bread board in case I had a break and they are all good. I then tried each sensor on it's own and they work fine and display the temp on the serial monitor, but as soon as I connect the 2nd one and restart the board it comes up with
"Locating devices on Bus 3...Found 0 devices"
Is there a pullup resistor on the data line? 4.7K is the usually recommended value. See here.
I connect 3 DS18B20 sensors to my Uno and with the first code I get:
Locating devices on Bus 3...Found 3 devices.
Sensor 1 : 26.62⸮C | 79.93⸮F
Sensor 2 : 26.25⸮C | 79.25⸮F
Sensor 3 : 26.44⸮C | 79.59⸮F
Sensor 1 : 26.62⸮C | 79.93⸮F
Sensor 2 : 26.25⸮C | 79.25⸮F
Sensor 3 : 26.44⸮C | 79.59⸮F
#define ONE_WIRE_BUS 3 // Data wire is plugged into digital pin 3 on the Arduino
pinMode(3, OUTPUT);
analogWrite(3, 0);
It probably is not a good idea to be messing with the pin to which the sensors are connected.
Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.
Yes there is a 4.7K resistor across the data to +5V, both of the sensors share it. I did try 1 on each just in case that was the problem, but made no difference. I've put the code back to pin 2 for the data wire, and swapped the date connection to pin 2 on the Arduino and it still makes no difference. Can i just connect the sensors to different data pins and read them separately I've got a few spare pins?
Ok i've run the following code
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// variable to hold device addresses
DeviceAddress Thermometer;
int deviceCount = 0;
void setup(void)
{
// start serial port
Serial.begin(300);
// Start up the library
sensors.begin();
// locate devices on the bus
Serial.println("Locating devices...");
Serial.print("Found ");
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount, DEC);
Serial.println(" devices.");
Serial.println("");
Serial.println("Printing addresses...");
for (int i = 0; i < deviceCount; i++)
{
Serial.print("Sensor ");
Serial.print(i+1);
Serial.print(" : ");
sensors.getAddress(Thermometer, i);
printAddress(Thermometer);
}
}
void loop(void)
{}
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
Serial.print("0x");
if (deviceAddress[i] < 0x10) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
if (i < 7) Serial.print(", ");
}
Serial.println("");
}
and got the following address for the sensors on the bus, by connecting them only 1 at a time.
Sensor 1 : 0x28, 0xC6, 0x80, 0x29, 0x32, 0x19, 0x01, 0x8A
Sensor 1 : 0x28, 0x2B, 0x2E, 0x0F, 0x32, 0x19, 0x01, 0x01
Interestingly if i connect the 3rd one on it's own, which is on a 5m cable, it comes back as "0 devices found"
I tried lowering the baud rate down to 300 as i read an article about cable lengths causing issues on bus address systems with high baud rates but made no difference. I think they were talking about 100's of meters.
So i'm still stuck with only 1 sensor working at a time. I was hoping to build a temperature differential controller to control a pump and valve, this needs a minimum of 2, but ideally 3 sensors.
I can't find any guides or other info on the internet tried goggling if other's have had similar issues, but nothing?
Help!
Ok so I've disconnected everything again and followed a YouTube video using a different code and i get the exact same results 1 sensor works fine but as soon as i connect the 2nd one i get no deviced found. this is the code i used
include <OneWire.h>
#include <DallasTemperature.h>
// All DS18B20 Sensors are connected to pin 8 on the Arduino
#define ONE_WIRE_BUS 8
// Creating a oneWire instance(object)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire object reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int numberOfDevices; //To store number of sensor connected
DeviceAddress tempDeviceAddress; // Variable to store a single sensor address
/*----------------------------------------------------------------*/
void setup(void) {
Serial.begin(9600);
sensors.begin();
// Get the number of sensors connected to the the wire( digital pin 4)
numberOfDevices = sensors.getDeviceCount();
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// Loop through each sensor and print out address
for(int i=0; i<numberOfDevices; i++) {
// Search the data wire for address and store the address in "tempDeviceAddress" variable
if(sensors.getAddress(tempDeviceAddress, i)) {
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
} else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}//Enf of for loop
}//End of void setup()
/*----------------------------------------------------------------*/
void loop(void) {
sensors.requestTemperatures(); // Send the command to get temperatures from all sensors.
// Loop through each device, print out temperature one by one
for(int i=0; i<numberOfDevices; i++) {
// Search the wire for address and store the address in tempDeviceAddress
if(sensors.getAddress(tempDeviceAddress, i)){
Serial.print("Temperature from sensor number: ");
Serial.println(i,DEC);
// Print the temperature
float tempC = sensors.getTempC(tempDeviceAddress); //Temperature in degree celsius
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
}// End of for loop
delay(5000);
}
/*----------------------------------------------------------------*/
// function to print a sensor address
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++) {
if (deviceAddress[i] < 16)
Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}//End of for loop
}
/*----------------------------------------------------------------*/
I'm starting to think it has to be my wiring or bread board but I've tested if over and over again, and even rewired it several times using a new 4.7K resistor and new leads in case one was faulty. nothing seems to change to results.
I'm starting to lose faith in this as it makes no sense! I'm new to this and so far spent hours trying to get this simple setup working following guides, at this rate it's going to take 1000's of hours to build even my simplest own project!
I haven't looked in your code yet but with onewire it is possible to have 10 or 20 sensors on one bus.
I have done this successfully. If wires get very long then there arise problems with signal quality.
Can you post multiple pictures of your arduino-board connected with two sensors.
In the pictures it should be clear visible and easy to analyse which wire is connected to which port.
This requires multiple pictures an overview and detail-pictures taken from vertical above the sockets
so that it is easy to see whichj wire is connectec to which socket.
best regards Stefan
The code in the OP's reply #5 works* on my setup (Uno and 3 DS18B20 sensors). Since the code works, the wiring seems to be the problem. I agree with StefanL38 that clear photos of the setup would help us to help the OP.
I have to wonder if the OP actually reads our replies. I ask that they read the forum guidelines so that they post code properly but the code is still in quote tags, not code tags.
- I have to comment out the
Serial.print(deviceAddress, HEX);
as it causes an error on my PC. With that commented I get
3 devices.
Found device 0 with address:
Found device 1 with address:
Found device 2 with address:
Temperature from sensor number: 0
Temp C: 25.56 Temp F: 78.01
Temperature from sensor number: 1
Temp C: 25.50 Temp F: 77.90
Temperature from sensor number: 2
Temp C: 25.31 Temp F: 77.56
Temperature from sensor number: 0
Temp C: 25.62 Temp F: 78.12
Temperature from sensor number: 1
Temp C: 25.50 Temp F: 77.90
Temperature from sensor number: 2
Temp C: 25.31 Temp F: 77.56
Thanks Stefan, I've taken a few photos hope you can shed some light on what's wrong with my wiring.
Sorry but i can't seem to use the code tags to put up code it just leaves it blank. hence why i'm using the quote tag instead
Just found a simpler way to upload the photos,
the 3 temperature sensors are connected at the bottom edge of the board (looking at the last picture), power and ground rails are at the top of the breadboard and i've used the far right 1a-e for the common data connections, 1a being to the Arduino board, 1b the resistor, 1c,d, & e are the 3 sensors data pins. hope that makes sense.

Check the value of the pull-up resistor with your Ohmmeter. It looks like a 47K 1% to me. It should be more like 4.7K.
I just tried a 47K on my setup. It works with one sensor connected, but when I connect 3 sensors it reports:
0 devices.
Yes that's it. I pulled the resistor off a strip that wasn't very clearly labelled and i didn't recheck the colour bands as I need a magnifying glass to see them now, (old age is cruel)! when i rebuilt the bread board i used another resistor off the same pack, again without checking or testing. Lesson learnt, always double check the resistors value with a multi-meter. Thanks again for you help in solving this. I can get on with building up my temp controller project now , and it's pouring with rain outside so no better time. I'm off to put the kettle on and get stuck in again coding.
Many thanks
I bought an Arduino component tester that I use a lot. I use it to verify the values of resistors instead of my meter. It is quicker than getting out the meter and test leads and hooking up.
i'll order one of those they look a handy bit of kit, would have saved me hours of head scratching!