0

I'm using the Motor Shield v2 and I'm looking for a way to automatically discover the addresses of attached motor shields.

  • 3
    research how to discover i2c devices – jsotola Oct 17 '18 at 17:57
  • 1
    @gre_gor, sempaiscuba, jsotola, Greenonline, per1234 again on-hold with a good answer – Juraj Oct 18 '18 at 9:04
  • @Juraj But I don't consider it a good question. – gre_gor Oct 18 '18 at 19:33
  • 1
    @gre_gor, why not? you can improve it – Juraj Oct 18 '18 at 19:34
1

If you configure each I2C device to have a unique address, then it's easy to determine which ones are connected.

This sketch is derived from the I2C Scanner sketch found here: I2cScanner

// This sketch tests the standard 7-bit addresses.
// Devices with higher bit address might not be seen properly.
#include <Wire.h>
byte shieldOneAddress = 0x27;
byte shieldTwoAddress = 0x32;
byte error, address;

void setup(){

  Wire.begin();
  Serial.begin(9600);
  Serial.println("Scanning for shields...");

  for(address = 1; address < 127; address++){

    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if(error == 0){
      if(address == shieldOneAddress){
        Serial.println("Shield One is connected.");
      }
      if(address == shieldTwoAddress){
        Serial.println("Shield Two is connected.");
      }
    }

  }
  Serial.println("Scan complete.\n");
}

void loop(){}
| improve this answer | |
  • This code detects a device connected at address 112. This is the all call address. It doesn't detect the individual address of a motor shield (0x60 in my case). – mikeLundquist Dec 18 '18 at 14:39
  • 1
    So ^that is true when the motor shield library isn't imported, but when the motor shield library is imported it works fine. Maybe they do some setup, not sure. – mikeLundquist Dec 18 '18 at 19:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.