DS18B20 adding multiple identical sensors arduino uno

hi just got my first sensor working, but my goal is to have 3 temp sensors and one luminosity sensor.

but when adding the second DS18B20 widget, the sketch has not included the first one. anyone have an idea for the code for two or 3 sensors?

Here is mine with the arduino uno with a 5100 sheild an one DS18b20 sensor:

#include <OneWire.h>

#include <SPI.h>

#include <Ethernet.h>
#include <EthernetUdp.h>
#include <EthernetServer.h>
#include <Dhcp.h>
#include <EthernetClient.h>
#include <Dns.h>
#include <util.h>

  /*
Cayenne DS18B20 Example

This sketch shows how to send temperature data to a DS18B20 Sensor in the Cayenne Dashboard.

The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:
1. Install the OneWire library (http://www.pjrc.com/teensy/td_libs_OneWire.html) from the Arduino Library Manager.
2. Install the DallasTemperature library (http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library#Code.2FLibrary) from the Arduino Library Manager.
3. In the Cayenne Dashboard add a new DS18B20 widget.
4. Set the widget to Value Display.
5. Select Virtual Pins and a virtual pin number.
6. Set VIRTUAL_PIN to the pin number you selected.
7. Attach a DS18B20 to an digital pin on your Arduino.
   Schematic:
   [Ground] -- [DS18B20] -- [4.7k resistor] -- [5V]
                   |______________|
                   |
              Digital Pin
8. Set the tmpPin variable to match the pin used to connect the DS18B20.
9. Set the token variable to match the Arduino token from the Dashboard.
10. Compile and upload this sketch.
11. Once the Arduino connects to the Dashboard it should automatically update the DS18B20 widget with data.
*/

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneEthernet.h>

// Virtual Pin of the DS18B20 widget.
#define VIRTUAL_PIN V1

// Digital pin the DS18B20 is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
const int tmpPin = 7;


OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "";

void setup()
{
    Serial.begin(9600);
    Cayenne.begin(token);
    sensors.begin();
}

void loop()
{
    Cayenne.run();
}

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)
{
    // Send the command to get temperatures.
    sensors.requestTemperatures();
    // This command writes the temperature in Celsius to the Virtual Pin.
    Cayenne.celsiusWrite(VIRTUAL_PIN, sensors.getTempCByIndex(0));
    // To send the temperature in Fahrenheit use the corresponding code below.
    //Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}

I think you can initialize 3 sensors pin then read values one by one with different virtual pin.

Yes but how.

The code only gives posibility of one sensor:

const int tmpPin = 7;

Im not a coder so dont know how to work around it

I’m learning code too, so you can try to change

#define VIRTUAL_PIN1 V1
#define VIRTUAL_PIN2 V2
#define VIRTUAL_PIN3 V3

const int tmpPin1 = 7;
const int tmpPin2 = 8;
const int tmpPin3 = 9;

OneWire oneWire1(tmpPin1);
OneWire oneWire2(tmpPin2);
OneWire oneWire3(tmpPin3);

DallasTemperature sensors(&oneWire1);
DallasTemperature sensors(&oneWire2);
DallasTemperature sensors(&oneWire3);

And change CAYENNE_OUT to responsible pin

2 Likes

I found out how to have multiple DS18B20 sensors.

Just a matter of (like you suggested kuncono) repeating alot of variables. But the trick is to remember to make them all unique.

There is proberly a smarter way of looping or array it but again im not a coding guy.

Here’s my working example of 3 sensors on the arduino uno:

#include <OneWire.h>

#include <SPI.h>

#include <Ethernet.h>
#include <EthernetUdp.h>
#include <EthernetServer.h>
#include <Dhcp.h>
#include <EthernetClient.h>
#include <Dns.h>
#include <util.h>

  /*
Cayenne DS18B20 Example

This sketch shows how to send temperature data to a DS18B20 Sensor in the Cayenne Dashboard.

The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:
1. Install the OneWire library (http://www.pjrc.com/teensy/td_libs_OneWire.html) from the Arduino Library Manager.
2. Install the DallasTemperature library (http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library#Code.2FLibrary) from the Arduino Library Manager.
3. In the Cayenne Dashboard add a new DS18B20 widget.
4. Set the widget to Value Display.
5. Select Virtual Pins and a virtual pin number.
6. Set VIRTUAL_PIN to the pin number you selected.
7. Attach a DS18B20 to an digital pin on your Arduino.
   Schematic:
   [Ground] -- [DS18B20] -- [4.7k resistor] -- [5V]
                   |______________|
                   |
              Digital Pin
8. Set the tmpPin variable to match the pin used to connect the DS18B20.
9. Set the token variable to match the Arduino token from the Dashboard.
10. Compile and upload this sketch.
11. Once the Arduino connects to the Dashboard it should automatically update the DS18B20 widget with data.
*/

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneEthernet.h>

// Virtual Pin of the DS18B20 widget.
#define VIRTUAL_PIN1 V1
#define VIRTUAL_PIN2 V2
#define VIRTUAL_PIN3 V3

// Digital pin the DS18B20 is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
const int tmpPin1 = 7;
const int tmpPin2 = 6;
const int tmpPin3 = 5;



OneWire oneWire1(tmpPin1);
OneWire oneWire2(tmpPin2);
OneWire oneWire3(tmpPin3);

DallasTemperature sensors1(&oneWire1);
DallasTemperature sensors2(&oneWire2);
DallasTemperature sensors3(&oneWire3);


// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "xxxx";

void setup()
{
    Serial.begin(9600);
    Cayenne.begin(token);
    sensors1.begin();
        sensors2.begin();
        sensors3.begin();
}

void loop()
{
    Cayenne.run();
}

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN1)
{
    // Send the command to get temperatures.
    sensors1.requestTemperatures();
    // This command writes the temperature in Celsius to the Virtual Pin.
    Cayenne.celsiusWrite(VIRTUAL_PIN1, sensors1.getTempCByIndex(0));
    // To send the temperature in Fahrenheit use the corresponding code below.
    //Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}
CAYENNE_OUT(VIRTUAL_PIN2)
{
    // Send the command to get temperatures.
    sensors2.requestTemperatures();
    // This command writes the temperature in Celsius to the Virtual Pin.
    Cayenne.celsiusWrite(VIRTUAL_PIN2, sensors2.getTempCByIndex(0));
    // To send the temperature in Fahrenheit use the corresponding code below.
    //Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}
CAYENNE_OUT(VIRTUAL_PIN3)
{
    // Send the command to get temperatures.
    sensors3.requestTemperatures();
    // This command writes the temperature in Celsius to the Virtual Pin.
    Cayenne.celsiusWrite(VIRTUAL_PIN3, sensors3.getTempCByIndex(0));
    // To send the temperature in Fahrenheit use the corresponding code below.
    //Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}
3 Likes

a user asked for wiring multiple DS18B20 digital sensors

Did a quick and dirty drawing in fritzing:

1 Like

im not an expert, but just reading that it should be possible to parallel connect the sensors, but then I dont know how the code should be.

Hi tvj, thank you!
You state you are not a “coding guy”, and yet you have a working example, on the contrary, all those smart technical people who boast lots of higher knowledge, still did not practically solve the issue.

As in beer-ware, I owe you a beer :wink:

Regards, Giorgio

Hi guys.

Where I can put this custom code?

What are you looking to do? This thread is explaining how to add more DS18B20 sensors to an Arduino device using the Arduino IDE. If you scroll up to post 5 you’ll see the code to do it.

Ok.

Thank you.

hola, yo yambien intento conectar varios sensores a un solo pin, alguien puede ayudar?

Hello is this code OK for MQTT:

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

#define SENSOR1_PIN 2// Temperature sensor on LED ligths.
#define SENSOR2_PIN 3// Temperature sensor on Aquarium.
#define SENSOR3_PIN 4// Temperature sensor on Control Unit.


#define VIRTUAL_CHANNEL 1
#define VIRTUAL_CHANNEL 2
#define VIRTUAL_CHANNEL 3


OneWire oneWire1(SENSOR1_PIN);
OneWire oneWire2(SENSOR2_PIN);
OneWire oneWire3(SENSOR3_PIN);

DallasTemperature sensors1(&oneWire1);
DallasTemperature sensors2(&oneWire2);
DallasTemperature sensors3(&oneWire3);

void setup()
{
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
	sensors.begin();
}

void loop()
{
	Cayenne.loop();
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL 1)
{
	// Send the command to get temperatures.
	sensors1.requestTemperatures();
	// This command writes the temperature in Celsius to the Virtual Channel.
	Cayenne.celsiusWrite(VIRTUAL_CHANNEL 1, sensors1.getTempCByIndex(0));
;
}
// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL 2)
{
  // Send the command to get temperatures.
  sensors2.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Channel.
  Cayenne.celsiusWrite(VIRTUAL_CHANNEL 1, sensors2.getTempCByIndex(0));
  ;
}
  // This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL 3)
{
  // Send the command to get temperatures.
  sensors1.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Channel.
  Cayenne.celsiusWrite(VIRTUAL_CHANNEL 3, sensors3.getTempCByIndex(0));
;
}

You have some extra ; in there, and it’s been a long time since I used the DallasTemperature library, but looks ok other than that. Are you getting errors, or noticing that something doesn’t work?

1 Like

I tried to install One wire library without success. Do you know why?

check you internet connection.or download the library from GitHub - PaulStoffregen/OneWire: Library for Dallas/Maxim 1-Wire Chips and add it to arduino library folder.

I think latest DS18B20 library INCLUDES the 1-wire library and you should not include it separately in your code. If you get errors about 1-wire stuff being redefined, that’s the problem.

1 Like