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

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!

1 Like