Can I connect more than one ds18b20 to the same arduino?


#1

Hi guys
I have just connected a ds18b20 to my Arduino and after a bit of fuss it’s now working great.
Is it possible to add more than one ds18b20 to the same Arduino?
If so then how?
Thanks in advance
Rich


#2

Hi Rich-miller,
I not not have an Arduino module but use 3 off DS18B20’s on my RPi2. If you can follow the help guide in the “Docs” section (http://www.cayenne-mydevices.com/docs/#ds18b20-temperature) and get 1 working then connect extra DS18b20’s in parallel with each other, i.e. like I have to do on the RPi2 then that should work. I recall someone had a number of these sensors in parallel and got them working. Search for DS18b20 and see what projects people have used them on here and maybe someone has used multiple DS18b20 on an Arduino.
BAZ


#3

Hi Rich-miller,
Have a look here on this website: https://program-plc.blogspot.com/2015/11/plc-arduino-multiple-ds18b20-digital.html
There is a wiring diagram within that showing more than one DS18B20 on the Arduino.

For more info on I2C comms on Arduino this site is useful http://howtomechatronics.com/tutorials/arduino/how-i2c-communication-works-and-how-to-use-it-with-arduino

BAZ


#4

Hi Baz
Thank for your replies.
Not wanting to push my luck but would you be able to tel me how I can then connect to Cayenne. i.e. do I need to edit the example sketch, although the two ds18b20’s are connecting to the same Arduino I need to monitor them individually with two widgets on my dashboard.
Thanks again for your previous help, thanks in advance for this question.
Rich


#5

Hi Rich,
Would love to help but I have never used an Arduino, never heard of them till I got my first RPi1b back in the day :open_mouth:
Looking at the “Code examples (Sketch)” section within “Docs”, under “Sensors” there is an example of “DS18B20” which may be what you are looking for.
Might be best to read up on that part of the “Docs” webpage. I have found it useful in the past.
If you are really stuck, maybe someone with Arduino experience may be able to help

BAZ


#6

Get a sketch going with the DS18B20s, then get a sketch going with Cayenne, then put both in a blender, and blend for a minute or two…

What are you using for an Ethernet or Wifi shield? The Cayenne code is very simple, so integration should not be difficult at all.

Let us know when you have the separate sketches working, and I can help you with integration if you get stuck.

Cheers,

Craig


#7

Hi @rich-miller,

There should be no problem to add multiple DS18B20s (or any other usual sensor) to your Arduino setup. I’ll assume you know how to physically wire up the 2nd one since you’ve gotten the first one running.

It’s mostly a matter of duplicating the code in the sketch file but making sure you use unique variable names. In this example I have one sensor on digital pin 7 and virtual pin V1, and the second sensor on digital pin 5 and virtual pin V2.

If you look at the following, I took the original sketch from Cayenne and duplicated everything with the letter B so there would be unique functions and variables for each sensor. You can name them differently of course.

#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
    #define VIRTUAL_PINB V2

    // 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;
    const int tmpPinB = 5;

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

    OneWire oneWireB(tmpPinB);
    DallasTemperature sensorsB(&oneWireB);

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

    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));
    }

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

If you just want to copy/paste that code it should behave so long as you make sure the physical pins tmpPin and tmpPinB match your wiring, and that the virtual pins VIRTUAL_PIN and VIRTUAL_PINB match your Cayenne dashboard widgets. And to enter your own auth token for that device where I put YOUR_AUTH_CODE

Let me know if you get stuck anywhere!


Bs18d20
#8

I should add that if you’re copy/pasting, my code above assumes a W5100 Ethernet Shield – if your connection is different you’ll need to include the library for your Ethernet/WiFi Shield rather than CayenneEthernet.h


#9

Yep. It’s that easy. Presses Easy button


#10

Hi Baz

Thanks for your replies, somebody else has gotten back to me and told me how to connect two or more DS18b20’s to Cayenne. Your info has still be very useful .

Thank you
Rich.


#11

@rsiegel

Hi

I’ve tried as you suggested but I get the same reading on each widget, i’m using V4=d5 and V5=d7. the two readings on the widgets both come from V4-d5, I can’t get V5=d7 to work at all even if I unplug V4=d5. Would you mind checking my sketch.
#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 V4
#define VIRTUAL_PINB V5

// 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 = 5;
const int tmpPinB = 7;

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

OneWire oneWireB(tmpPinB);
DallasTemperature sensorsB(&oneWireB);

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

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));
}

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

Kind regards
Rich


#12

@rsiegel

I don’t know what happened at the top of my pasted sketch above but it does include onewire and dallas libraries


#13

@rich-miller

I think the issue here is that you switched to the fahrenheitWrite function that I had commented out in my original sketch. So in that 2nd to last line of your sketch, you’d need to change sensors.getTempFByIndex(0) to sensorsB.getTempFByIndex(0) so its referencing the correct sensor.

Also just for your reference, Cayenne was interpreting my temperature as Fahrenheit and letting me control C vs F at the dashboard widget level despite using the Celsius function in the script. I think it will work your way too, but consider that if you start getting numbers that don’t make sense.


#14

@rsiegel

Wow you have a sharp eye, it is working perfectly now.

I used the Fahrenheit part of the sketch as the first time I tried the Celsius nothing worked at all, I assumed maybe a bug as the dashboard defaults Fahrenheit. I will now try the Celsius sketch.

I really do appreciate your help,

I see you are Technical Product Manager… loving your work, nice one!

Kind regards
Rich


#15

I get paid to do it, so I try :blush:

Thanks for your patience while we worked through it, you know where to find me if you run into something else!


#16

See this also. It’s not needed to connect the sensors on two ports. only one should do the work. For me this is working perfectly with two sensors:
(add hashtag before define and include and add the symbols for less than and for more than before and after the file names after include and define.
I add an I2C LCD also, but you can remove it.

define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
include OneWire.h
include DallasTemperature.h
include Wire.h
include LiquidCrystal_I2C.h // F Malpartida’s NewLiquidCrystal library
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// If you’re not using the Ethernet W5100 shield, change this to match your connection type. See Communications examples.
include CayenneEthernet.h

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

// 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 = 2;
float cap1;
float cap2;
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();
lcd.begin (16,2); // initialize the lcd
lcd.backlight();
}

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.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
cap1 = sensors.getTempCByIndex(0);
Cayenne.celsiusWrite(VIRTUAL_PIN1, cap1);
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
lcd.setCursor(0,0);
lcd.write(" ");
lcd.setCursor(0,0);
lcd.write(“B=”);
lcd.print(cap1);
lcd.write((char)223);
lcd.write(“C”);
}

CAYENNE_OUT(VIRTUAL_PIN2)
{
// Send the command to get temperatures.
sensors.requestTemperatures();
// This command writes the temperature in Celsius to the Virtual Pin.
cap2 = sensors.getTempCByIndex(1);
Cayenne.celsiusWrite(VIRTUAL_PIN2, cap2);
// To send the temperature in Fahrenheit use the corresponding code below.
//Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
lcd.setCursor(0,1);
lcd.write(" ");
lcd.setCursor(0,1);
lcd.write(“A=”);
lcd.print(cap2);
lcd.write((char)223);
lcd.write(“C”);
}


#17

I created a working sketch with two sensors DS18V20.
Maybe it’s not perfect but it works.
All work is checked.
SKETCH To_Pin_DS18B20_Arduino_Cayenne_My_version.
To_Pin_DS18B20_Arduino_Cayenne_My_version.txt (1.9 KB)

Подключение двух датчиков ds18b20 Cayenne Arduino
Two Pin DS18B20 cayenne arduino


#18

This works perfectly and retains the 1 wire benefits as opposed to using a digital pin for each and every sensor. Many thanks for sharing.


#19

I’ve done some further work and have connected multiple DS18B20 (x4) along with a DHT11, and BMP85 sensor with no problem. I’m sure the code is not very optimal and there is a waste in continually returning the Temp & Humidity readings the way I have within the Loop but my project is not going to be time critical.

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <DHT.h>
#include <DHT_U.h>

// If you're not using the Ethernet W5100 shield, change this to match your connection type
#include <CayenneEthernet.h>

// Virtual Pin & Config of the DS18B20
#define VIRTUAL_PIN1 V1
#define VIRTUAL_PIN2 V2
#define VIRTUAL_PIN3 V5
#define VIRTUAL_PIN4 V6
const int tmpPin = 2;
float cap1;
float cap2;
float cap3;
float cap4;
OneWire oneWire1(tmpPin);
DallasTemperature sensors(&oneWire1);

//Virtual Pin & Config of the BMP085
#define TEMPERATURE_PIN V3
#define BAROMETER_PIN V4
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10180);
bool bmpSensorDetected = true;

//Virtual Pin & Config for DHT11
#define DHT_HUMIDITY_PIN V7
#define DHT_TEMPERATURE_PIN V8
#define DHTPIN            4         // Pin which is connected to the DHT sensor.
#define DHTTYPE           DHT11     // DHT 11 
float dht11T;
float dht11H;
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;                   //Delay between sensor reading

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

void setup()
{
  Serial.begin(115200);   //Ensure you set the correct Baud rate if you intend to add/use serial writes for debugging
  Cayenne.begin(token);
  sensors.begin();
  dht.begin();
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  // Set delay between sensor readings based on sensor details.
  delayMS = sensor.min_delay / 1000;

  if (!bmp.begin())
  {
    CAYENNE_LOG("No BMP sensor detected");
    bmpSensorDetected = false;
  }

}

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

}

void DHTSensor () {

  // Get temperature event
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  dht11T = (event.temperature);
  Serial.print("Temperature:: ");
  Serial.print(dht11T);
  Serial.println("'C");

  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  dht11H = (event.relative_humidity);
  Serial.print("Humidity: ");
  Serial.print(dht11H);
  Serial.println("%");
  // Delay between measurements.
  //delay(delayMS);
}

// This function is called when the Cayenne widget requests data for  DHT11 Humidity
CAYENNE_OUT(DHT_HUMIDITY_PIN)
{
  Cayenne.virtualWrite(DHT_HUMIDITY_PIN, (dht11H), HUMIDITY, PERCENT);
}

// This function is called when the Cayenne widget requests data for  DHT11 Temperature
CAYENNE_OUT(DHT_TEMPERATURE_PIN)
{
  Cayenne.virtualWrite(DHT_TEMPERATURE_PIN, (dht11T), TEMPERATURE, CELSIUS);
}

// START - These functions are called when the Cayenne widget requests data for the DS18B20 Temp Sensors (x4)
CAYENNE_OUT(VIRTUAL_PIN1)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  cap1 = sensors.getTempCByIndex(0);
  Cayenne.celsiusWrite(VIRTUAL_PIN1, cap1);
}
CAYENNE_OUT(VIRTUAL_PIN2)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  cap2 = sensors.getTempCByIndex(1);
  Cayenne.celsiusWrite(VIRTUAL_PIN2, cap2);
}
CAYENNE_OUT(VIRTUAL_PIN3)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  cap3 = sensors.getTempCByIndex(2);
  Cayenne.celsiusWrite(VIRTUAL_PIN3, cap3);
}
CAYENNE_OUT(VIRTUAL_PIN4)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  cap4 = sensors.getTempCByIndex(3);
  Cayenne.celsiusWrite(VIRTUAL_PIN4, cap4);
}
// END OF - These functions are called when the Cayenne widget requests data for the DS18B20 Temp Sensors (x4)


// START - This function is called when the Cayenne widget requests data for the BMP085 Barometer/Temp Sensor Virtual Pin.
CAYENNE_OUT(BAROMETER_PIN)
{
  if (bmpSensorDetected)
  {
    // Send the command to get data.
    sensors_event_t event;
    bmp.getEvent(&event);

    if (event.pressure)
    {
      // Send the value to Cayenne in hectopascals.
      Cayenne.hectoPascalWrite(BAROMETER_PIN, event.pressure);
    }
  }
  else
  {
    CAYENNE_LOG("No BMP sensor detected");
  }
}

// This function is called when the Cayenne widget requests data for the temperature's Virtual Pin.
CAYENNE_OUT(TEMPERATURE_PIN)
{
  if (bmpSensorDetected)
  {
    float temperature;
    bmp.getTemperature(&temperature);
    // Send the value to Cayenne in Celsius.
    Cayenne.celsiusWrite(TEMPERATURE_PIN, temperature);
  }
  else
  {
    CAYENNE_LOG("No BMP sensor detected");
  }
}
// END OF - This function is called when the Cayenne widget requests data for the BMP085 Barometer/Temp Sensor Virtual Pin.