Create the DS18B20 list in the C++ array

Zápisník experimentátora

Hierarchy: DS18B20 Senzor teploty

If you use multiple sensors in one program and need to clearly distinguish between them, you do not avoid creating a list of individual sensor addresses. In this article, we create a program that will make it easier for us. The result will be an C++ array of addresses that you can use later in the next code.

We will use the wiring as shown below, only to connect more sensors. They also engage, add only one sensor to the three open holes below the sensor in the image. If you have more, pair them with other drivers. Pull-up resistor will still be only one.

In the ds18b20array.ino file there is a program that makes it easy to work with sensors. It searches for all the sensors on the bus and sends the source code to the serial port, which can be easily included in its other programs. The advantage is that the result is a field of address for sensors, which is not necessary to search the bus.

It also brings down the disadvantages. It is no longer possible to change one sensor to another. On the other hand, however, one who works with multiple sensors at once needs to know exactly which sensor has what address and where the developer placed it.

#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 (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

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

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

///
/// setup of paremeters
///
void setup()
{
Serial.begin(9600);
// Start up the library
sensors.begin();

// Grab a count of devices on the wire
int numberOfDevices = sensors.getDeviceCount();

Serial.println("DeviceAddress da[]={");
// Loop through each device, print out address
for (int i = 0; i < numberOfDevices; i++)
  {
  // Search the wire for address
  if (sensors.getAddress(tempDeviceAddress, i))
    {
    Serial.print("  {");
    for (uint8_t i = 0; i < 8; i++)
      {
      Serial.print("0x");
      if (tempDeviceAddress[i] < 16) Serial.print("0");
      Serial.print(tempDeviceAddress[i], HEX);
      if (i < 7)
        Serial.print(",");
      }
    if (i < numberOfDevices-1)
      Serial.print("}, // sensor ");
    else
      Serial.print("}  // sensor ");
    Serial.print(i, DEC);
    Serial.println("");
    }
  }
Serial.println("};");
Serial.println("");
Serial.println("const int dacount=sizeof(da)/sizeof(DeviceAddress);");
}

///
/// main loop
///
void loop()
{
// nothing to do
}

Output from the program

The result is a C++ code that you easily copy into your program.

DeviceAddress da[]={
  {0x28,0x8C,0x6E,0x45,0x06,0x00,0x00,0x8A}, // sensor 0
  {0x28,0x49,0xBD,0x44,0x06,0x00,0x00,0x61}  // sensor 1
};

const int dacount=sizeof(da)/sizeof(DeviceAddress);

Source code

The source code of the program is located on GitHub.


08.03.2019


Menu