Feather M0: Communicating to 1-Wire DS2482 AND Serial COM Port

I'm trying to set up a 1-Wire network using the DS2482 driver and Feather M0 (LoRa RFM95 variant).

I have troubles using the DS2482 1-wire library I've found while also accessing the feather through the COM port on my computer.

Here are the symptoms:

I CAN access the feather through the serial monitor when not using the DS2482 library (only using the Wire.h i2c library) AND I can see that the DS2482 is on the i2c bus at address 0x18. The following code works.

#include <Wire.h>

void setup()
{
  Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

I CANNOT access the feather through the serial monitor when trying to use the DS2482 1-wire library. When I program the feather with the code below, the COM port disappears and I can't tell using the serial console if the feather is communicating on the i2c bus correctly.

#include <Wire.h>
#include <OneWire.h>
#include <Arduino.h>

OneWire oneWire;

void printAddress(uint8_t deviceAddress[8])
{
  Serial.print("{ ");
  for (uint8_t i = 0; i < 8; i++)
  {
    // zero pad the address if necessary
    Serial.print("0x");
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
    if (i<7) Serial.print(", ");
    
  }
  Serial.print(" }");
}

void setup()
{
  Serial.begin(9600);

}

void loop()
{
  Serial.println("Checking for I2C devices...:");
  if (oneWire.checkPresence())
  {
    Serial.println("DS2482-100 present");
    
    oneWire.deviceReset();
    
    Serial.println("\tChecking for 1-Wire devices...");
    if (oneWire.wireReset())
    {
      Serial.println("\tDevices present on 1-Wire bus");
      
      uint8_t currAddress[8];
      
      Serial.println("\t\tSearching 1-Wire bus...");
      
      while (oneWire.wireSearch(currAddress))
      {
        Serial.print("\t\t\tFound device: ");
        printAddress(currAddress);
        Serial.println();
      }
      
      oneWire.wireResetSearch();
      
    }
    else
      Serial.println("No devices on 1-Wire bus");
  }
  else
    Serial.println("No DS2482-100 present");
    
  delay(5000);
}

The issue happens in any code where I attempt to construct a oneWire device:

#include <OneWire.h>

OneWire oneWire;

Which is weird to me because it seems all the constructor does (bold) is start the i2c bus communication just like i2c bus scanning example that worked above.

#include "OneWire.h"
#include <Wire.h>

// Constructor with no parameters for compatability with OneWire lib
OneWire::OneWire()
{
	// Address is determined by two pins on the DS2482 AD1/AD0
	// Pass 0b00, 0b01, 0b10 or 0b11
	mAddress = 0x18;
	mError = 0;
	Wire.begin();
}

OneWire::OneWire(uint8_t address)
{
	// Address is determined by two pins on the DS2482 AD1/AD0
	// Pass 0b00, 0b01, 0b10 or 0b11
	mAddress = 0x18 | address;
	mError = 0;
	Wire.begin();
}

... the rest of the library

I'm not quite sure what to try next... I know the i2c bus and serial monitor can work at the same time (which is something I've seen people have trouble with on forums). I don't know why constructing the oneWire device causes the COM port to disappear. Please help.

In case anyone ends up running into the same issue....

I solved this by removing the Wire.begin(); line from the OneWire constructor:

// Constructor with no parameters for compatability with OneWire lib
OneWire::OneWire()
{
	// Address is determined by two pins on the DS2482 AD1/AD0
	// Pass 0b00, 0b01, 0b10 or 0b11
	mAddress = 0x18;
	mError = 0;
//	Wire.begin();
}

Instead I put Wire.begin(); within my setup

void setup()
{
  Serial.begin(9600);
  Wire.begin();

}

Clearly something goes wrong attempting to start the i2c bus from within the constructor.

Thanks Harwasch

Worked a treat for me as well :slight_smile:

Netless