Chat freely about anything...

User avatar
By WStan
#23821 sensors.requestTemperatures() TEST with 5 DS18B20 sensors on a single line

Code: Select all#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 12

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

int numberOfDevices; // Number of temperature devices found

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

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
 
  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();
 
  // locate devices on the bus
  Serial.print("Locating devices...");
 
  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
 
  // 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("Found device ");
      Serial.print(i, DEC);
      Serial.print(" with address: ");
      printAddress(tempDeviceAddress);
      Serial.println();
      
      Serial.print("Setting resolution to ");
      Serial.println(TEMPERATURE_PRECISION, DEC);
      
      // set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
      sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
      
       Serial.print("Resolution actually set to: ");
      Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
      Serial.println();
   }else{
      Serial.print("Found ghost device at ");
      Serial.print(i, DEC);
      Serial.print(" but could not detect address. Check power and cabling");
   }
  }

}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
  // method 1 - slower
  //Serial.print("Temp C: ");
  //Serial.print(sensors.getTempC(deviceAddress));
  //Serial.print(" Temp F: ");
  //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit

  // method 2 - faster
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}

void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
 
 
  // Loop through each device, print out temperature data
  for(int i=0;i<numberOfDevices; i++)
  {
    delay(1000);
    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i))
   {
      // Output the device ID
      Serial.print("Temperature for device: ");
      Serial.println(i,DEC);
      
      // It responds almost immediately. Let's print out the data
      printTemperature(tempDeviceAddress); // Use a simple function to print out the data
   }
   //else ghost device! Check your power requirements and cabling
   
  }

  delay(1000);
}

// function to print a device 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);
  }
}


Pull up resistor value
4,70k results: Locating devices...Found 0 devices.
1,00k results: Locating devices...Found 5 devices. Temperatures for devices: 85
0,50k results: Locating devices...Found 5 devices. Temperatures for devices: 127.94
0,25k results: Locating devices...Found 5 devices. Temperatures for devices: See bellow:

Code: Select all[*]<?????$??(????????????Dallas Temperature IC Control Library Demo
Locating devices...Found 5 devices.
Parasite power is: ON
Found device 0 with address: 28E2A35C04000070
Setting resolution to 12
Resolution actually set to: 12
Found device 1 with address: 282A9E6003000006
Setting resolution to 12
Resolution actually set to: 12
Found device 2 with address: 28496D5C04000054
Setting resolution to 12
Resolution actually set to: 12
Found device 3 with address: 28E5297202000037
Setting resolution to 12
Resolution actually set to: 12
Found device 4 with address: 2863046103000060
Setting resolution to 12
Resolution actually set to: 12
Requesting temperatures...DONE
Temperature for device: 0
Temp C: 24.12 Temp F: 75.43
Temperature for device: 1
Temp C: 24.69 Temp F: 76.44
Temperature for device: 2
Temp C: 26.44 Temp F: 79.59
Temperature for device: 3
Temp C: 31.00 Temp F: 87.80
Temperature for device: 4
Temp C: 26.12 Temp F: 79.03
Requesting temperatures...DONE
Temperature for device: 0
Temp C: 34.69 Temp F: 94.44
Temperature for device: 1
Temp C: 36.69 Temp F: 98.04
Temperature for device: 2
Temp C: 38.63 Temp F: 101.53
Temperature for device: 3
Temp C: 43.19 Temp F: 109.74
Temperature for device: 4
Temp C: 37.94 Temp F: 100.29
Requesting temperatures...DONE
Temperature for device: 0
Temp C: 24.06 Temp F: 75.31
Temperature for device: 1
Temp C: 24.69 Temp F: 76.44
Temperature for device: 2
Temp C: 26.50 Temp F: 79.70
Temperature for device: 3
Temp C: 31.06 Temp F: 87.91
Temperature for device: 4
Temp C: 26.12 Temp F: 79.03
Requesting temperatures...DONE
Temperature for device: 0
Temp C: 127.94 Temp F: 262.29
Temperature for device: 1
Temp C: 127.94 Temp F: 262.29
Temperature for device: 2
Temp C: 127.94 Temp F: 262.29
Temperature for device: 3
Temp C: 127.94 Temp F: 262.29
Temperature for device: 4
Temp C: 127.94 Temp F: 262.29
Requesting temperatures...DONE
Temperature for device: 0
Temp C: 24.12 Temp F: 75.43
Temperature for device: 1
Temp C: 24.69 Temp F: 76.44

and so on
Last edited by WStan on Tue Jul 21, 2015 12:04 pm, edited 3 times in total.