I don't connect any sensor connected to the 1wire port.
With the first program, the response is "no addresses"
//=============================================================================
//Libraries
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> // Source: LiquidCrystal_V1.2.1.zip
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
#define ONE_WIRE_BUS 12 //Data wire is plugged into port 12
OneWire sensors(ONE_WIRE_BUS); // Connect your 1-wire device to pin 12
//Pass our oneWir reference to Dallas Temperature
//DallasTemperature sensors(&oneWire);
//=============================================================================
void setup(void) {
Serial.begin(9600);
lcd.begin(20,4);
discoverOneWireDevices();
}
//=============================================================================
void loop(void) {
// nothing to see here
}
//=============================================================================
void discoverOneWireDevices(void) {
byte i;
byte present = 0;
//byte data[12];
byte addr[8];
Serial.print("Looking for 1-Wire devices...\n\r");
while(sensors.search(addr)) {
printAddress(addr);
//if ( OneWire::crc8( addr, 7) != addr[7]) { Serial.print("CRC is not valid!\n"); return;}
}
Serial.print("\n\r\n\rThat's it.\r\n");
sensors.reset_search();
return;
}
//================================================================================================================
void printAddress(uint8_t temp[]){
int i;
//Serial.print("\n\rFound \'1-Wire\' device with address:\n\r");
for( i = 0; i < 8; i++) {
Serial.print("0x");
if (temp[i] < 16) {Serial.print('0');} //print a prevealing zero if necessary
Serial.print(temp[i], HEX); ;
if (i < 7)
{Serial.print(", ");} //print a separator between all bytes
} //End for loop
Serial.print("\n");
} //End subroutine
With this second program, two unknown addresses will come; they are not installed. What is the difference with the first "detect 1wire addresses"? I am only interested to find DS18b20 temperature sensors? According the datasheet the least significant byte should always be '0x28'.
//================================================================================================================
//Libraries
#include <DallasTemperature.h>
#include <EEPROM.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> // Source: LiquidCrystal_V1.2.1.zip
#include <OneWire.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
#define ONE_WIRE_BUS 12 //Data wire is pluggeg into port 12 of Arduino Nano
OneWire ds(ONE_WIRE_BUS); // Connect your 1-wire devices to pin 12, ds stands for
//Define global parameters
byte i; //position within arraybye
byte j=0; //Number of ds passed
uint8_t adrTcvForward[8];
uint8_t adrTcvBack[8];
uint8_t adrTvvForward[8];
uint8_t adrTvvBack[8];
String myString;
//================================================================================================================
void setup(void) {
Serial.begin(9600);
lcd.begin(20,4);
//This call has to be moved to a control menu (so it will be executed once)
Identify_1WireDevices();
}
//================================================================================================================
void loop(void) {
byte n;
//
//for (n=0, n<1, n++) //Assign a bytearray once to adrTcvForth
// {
// for (int i=0; i<8; i++) {adrTcvForward[i]=EEPROM.read(100+i);}
// for (int i=0; i<8; i++) {adrTcvBack[i]=EEPROM.read(108+i);}
// for (int i=0; i<8; i++) {adrTvvForward[i]=EEPROM.read(116+i);}
// for (int i=0; i<8; i++) {adrTvvBack[i]=EEPROM.read(124+i);}
// } //End for
} //End loop
//================================================================================================================
void Identify_1WireDevices(void) {
boolean blnExists;
byte addr[8];
byte addr1[8];
byte addr2[8];
byte addr3[8];
byte addr4[8];
int intSensorsPassed = 0;
Serial.print("Looking for 1-Wire devices...\n\r");
lcd.clear(); lcd.print ("Connect sensor 1");
do {
ds.search(addr);
printAddress(addr);cr();
if (addr[0] != 0x28) //The least significant byt of the DS18b20 should be 0x28
{printAddress(addr); Serial.println (" doesn't concern to the DS-family.");}
else
{switch (intSensorsPassed) {
case 0:
//for (byte i =0; i<8; i++) {addr1[i] = addr[i];} //Store the first addres in addr1
memcpy (addr, addr1, 8); //Store the first addres in addr1
intSensorsPassed++;
Serial.print ("Assigned to " + String(intSensorsPassed) + ": "); printAddress(addr1); cr();
Serial.println ("Sensors passed = " + String(intSensorsPassed)+ "\n");
lcd.setCursor(0,intSensorsPassed); lcd.print ("Add sensor " + String(intSensorsPassed + 1));
break;
case 1:
//check if the address was earlier identified
//Serial.println("\nIncoming address = "); printAddress(addr);
if (memcmp(addr, addr1, 8) != 0) {
//Seems to be another (new) address
//Serial.println("Unknown sensor found");
for (byte i =0; i<8; i++) {addr2[i] = addr[i];} //Store the addess in addr2
memcpy (addr, addr2, 8);
//printAddress(addr); Serial.print("\n");
intSensorsPassed++;
Serial.print ("Assigned to " + String(intSensorsPassed) + ": "); printAddress(addr2); cr();
Serial.println ("Sensors passed = " + String(intSensorsPassed) + "\n");
lcd.setCursor(0,intSensorsPassed); lcd.print ("Add sensor " + String(intSensorsPassed + 1));
} //End if
break;
case 2:
//check if the address was earlier identified
//Serial.println("\nIncoming address = "); printAddress(addr);
if ((memcmp(addr, addr1, 8) != 0) && (memcmp(addr, addr2, 8) != 0)) {
//Serial.println("Unknown sensor found"); //Seems to be another (new) address
memcpy (addr, addr3, 8); //Copy the addess to addr3
intSensorsPassed++;
Serial.print ("Assigned to " + String(intSensorsPassed) + ": "); printAddress(addr3); cr();
Serial.println ("Sensors passed = " + String(intSensorsPassed) + "\n");
lcd.setCursor(0,intSensorsPassed); lcd.print ("Add sensor " + String(intSensorsPassed + 1));
} //End if
break;
case 3:
//check if the address was earlier identified
//Serial.println("\nIncoming address = "); printAddress(addr);
if ((memcmp(addr, addr1, 8) != 0) && (memcmp(addr, addr2, 8) != 0) && (memcmp(addr, addr3, 8) != 0) ) {
//Serial.println("Unknown sensor found"); //Seems to be another (new) address
memcpy (addr, addr4, 8); //Copy the addess to addr3
//printAddress(addr); Serial.print("\n");
intSensorsPassed++;
Serial.print ("Assigned to " + String(intSensorsPassed) + ": "); printAddress(addr4); cr();
Serial.println ("Sensors passed = " + String(intSensorsPassed) + "\n");
} //End if
break;
} //End switch
} //End if
} while (intSensorsPassed<4); //End while sensors Passed
Serial.print("\n\nThat's it.");
} //End function
//================================================================================================================
void printAddress(uint8_t temp2[]){
int i;
for( i = 0; i < 8; i++) {
Serial.print("0x"); //Starting with the least significant byte
if (temp2[i] < 16) {Serial.print('0');} //print a prevealing zero if necessary
Serial.print(temp2[i], HEX); ;
if (i < 7)
{Serial.print(", ");} //print a separator between all bytes
} //End for loop
} //End subroutine
//================================================================================================================
void cr()
{Serial.println("\n");}