Jag har tänkt och använda två stycken DS18B20 termometrar på min Arduino men jag får det inte fungera. Jag lyckas att koppla så jag kan använda bara en.
Då följer jag denna manual: http://www.hobbytronics.co.uk/ds18b20-arduino
Men när jag ska använda "Multiple" så lyckas jag inte.
Jag kopplar mitt schema så här:

Och jag använder denna kod:
Kod: Markera allt
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
// 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);
// arrays to hold device addresses
DeviceAddress insideThermometer, outsideThermometer;
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// assign address manually. the addresses below will beed to be changed
// to valid device addresses on your bus. device address can be retrieved
// by using either oneWire.search(deviceAddress) or individually via
// sensors.getAddress(deviceAddress, index)
//insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };
//outsideThermometer = { 0x28, 0x3F, 0x1C, 0x31, 0x2, 0x0, 0x0, 0x2 };
// search for devices on the bus and assign based on an index. ideally,
// you would do this to initially discover addresses on the bus and then
// use those addresses and manually assign them (see above) once you know
// the devices on your bus (and assuming they don't change).
//
// method 1: by index
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
if (!sensors.getAddress(outsideThermometer, 1)) Serial.println("Unable to find address for Device 1");
// method 2: search()
// search() looks for the next device. Returns 1 if a new address has been
// returned. A zero might mean that the bus is shorted, there are no devices,
// or you have already retrieved all of them. It might be a good idea to
// check the CRC to make sure you didn't get garbage. The order is
// deterministic. You will always get the same devices in the same order
//
// Must be called before search()
//oneWire.reset_search();
// assigns the first address found to insideThermometer
//if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");
// assigns the seconds address found to outsideThermometer
//if (!oneWire.search(outsideThermometer)) Serial.println("Unable to find address for outsideThermometer");
// show the addresses we found on the bus
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
Serial.print("Device 1 Address: ");
printAddress(outsideThermometer);
Serial.println();
// set the resolution to 9 bit
sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
Serial.print("Device 0 Resolution: ");
Serial.print(sensors.getResolution(insideThermometer), DEC);
Serial.println();
Serial.print("Device 1 Resolution: ");
Serial.print(sensors.getResolution(outsideThermometer), DEC);
Serial.println();
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
// zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
// function to print a device's resolution
void printResolution(DeviceAddress deviceAddress)
{
Serial.print("Resolution: ");
Serial.print(sensors.getResolution(deviceAddress));
Serial.println();
}
// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
Serial.print("Device Address: ");
printAddress(deviceAddress);
Serial.print(" ");
printTemperature(deviceAddress);
Serial.println();
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures();
Serial.println("DONE");
// print the device information
printData(insideThermometer);
printData(outsideThermometer);
}
Kod: Markera allt
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
Men då kvarstår frågan: Hur ska jag koppla så mitt schema blir rätt? Jag har bara sökt på "Multiple DS18B20 arduino" på google och sökt upp hur dom andra har kopplat. Men jag lyckas inte. Jag skulle vilja ha din hjälp för koppla rätt schema för koden är inte fel på.
EDIT:
Jag har hittat denna kod. Denna är för "single" DS18B20 som är ihopkopplade. Men jag är ändå intresserad hur man använder orginalbiblioteket för "Multiple" DS18B20. Om man måste ha någon "sheild" eller något annat?
Kod: Markera allt
// OneWire and DallasTemperature libraries from
// http://milesburton.com/index.php?title=Dallas_Temperature_Control_Library
// Code based on examples from above and at
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
// See also http://www.arduino.cc/playground/Learning/OneWire
#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);
int numberOfSensors;
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("1-Wire DS18B20 example code");
// Start up the library
sensors.begin();
delay(5000); //important on linux as serial port can lock up otherwise
numberOfSensors = discoverOneWireDevices();
Serial.println();
}
void loop(void)
{
printTemperaturesToSerial();
delay(1000); //wait 10 sec
}
void printTemperaturesToSerial(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");
// Read each of our sensors and print the value
for(int i=0; i < numberOfSensors; i++) {
Serial.print("Temperature for Device ");
Serial.print( i );
Serial.print(" is: ");
// Why "byIndex"? You can have more than one IC on the same bus.
// 0 refers to the first IC on the wire
Serial.println( sensors.getTempCByIndex(i) );
}
Serial.println();
}
// Based on http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
int discoverOneWireDevices(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];
int count = 0;
Serial.println("Looking for 1-Wire devices...");
while(oneWire.search(addr)) {
Serial.print("Found \'1-Wire\' device with address: ");
for( i = 0; i < 8; i++) {
Serial.print("0x");
if (addr[i] < 16) {
Serial.print('0');
}
Serial.print(addr[i], HEX);
if (i < 7) {
Serial.print(", ");
}
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return 0;
}
Serial.println();
count++;
}
Serial.println("That's it.");
oneWire.reset_search();
return count;
}
Denna kod kan kortas ned till detta om någon är intresserad:
Kod: Markera allt
// OneWire and DallasTemperature libraries from
// http://milesburton.com/index.php?title=Dallas_Temperature_Control_Library
// Code based on examples from above and at
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
// See also http://www.arduino.cc/playground/Learning/OneWire
#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);
int numberOfSensors;
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("1-Wire DS18B20 example code");
// Start up the library
sensors.begin();
delay(5000); //important on linux as serial port can lock up otherwise
numberOfSensors = 2;
Serial.println();
}
void loop()
{
printTemperaturesToSerial();
delay(1000); //wait 10 sec
}
void printTemperaturesToSerial() {
// 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");
// Read each of our sensors and print the value
for(int i=0; i < numberOfSensors; i++) {
Serial.print("Temperature for Device ");
Serial.print( i );
Serial.print(" is: ");
// Why "byIndex"? You can have more than one IC on the same bus.
// 0 refers to the first IC on the wire
Serial.println( sensors.getTempCByIndex(i) );
}
Serial.println();
}