Come scoprire l'indirizzo i2c di un dispositivo con Arduino


Ciao!
in questo post vedremo come trovare l'indirizzo i2c di un dispositivo.

Scoprire l'indirizzo i2c è molto semplice, è sufficiente collegare la periferica che vogliamo analizzare al nostro Arduino.
Nell'immagine qui sotto potete vedere il collegamento ad un display SD1306, ma l'esempio vale per tutte le periferiche i2c.




Dopo aver effettuato i collegamenti, caricate nell'Arduino il codice che trovate qui sotto:


  1. // i2c_scanner
  2. //
  3. // Version 1
  4. //    This program (or code that looks like it)
  5. //    can be found in many places.
  6. //    For example on the Arduino.cc forum.
  7. //    The original author is not know.
  8. // Version 2, Juni 2012, Using Arduino 1.0.1
  9. //     Adapted to be as simple as possible by Arduino.cc user Krodal
  10. // Version 3, Feb 26  2013
  11. //    V3 by louarnold
  12. // Version 4, March 3, 2013, Using Arduino 1.0.3
  13. //    by Arduino.cc user Krodal.
  14. //    Changes by louarnold removed.
  15. //    Scanning addresses changed from 0...127 to 1...119,
  16. //    according to the i2c scanner by Nick Gammon
  17. //    http://www.gammon.com.au/forum/?id=10896
  18. // Version 5, March 28, 2013
  19. //    As version 4, but address scans now to 127.
  20. //    A sensor seems to use address 120.
  21. //
  22. //
  23. // This sketch tests the standard 7-bit addresses
  24. // Devices with higher bit address might not be seen properly.
  25. //
  26. #include <Wire.h>
  27. void setup()
  28. {
  29.   Wire.begin();
  30.   Serial.begin(115200);
  31.   Serial.println("\nI2C Scanner");
  32. }
  33. void loop()
  34. {
  35.   byte error, address;
  36.   int nDevices;
  37.   Serial.println("Scanning...");
  38.   nDevices = 0;
  39.   for(address = 1; address < 127; address++ )
  40.   {
  41.     // The i2c_scanner uses the return value of
  42.     // the Write.endTransmisstion to see if
  43.     // a device did acknowledge to the address.
  44.     Wire.beginTransmission(address);
  45.     error = Wire.endTransmission();
  46.     if (error == 0)
  47.     {
  48.       Serial.print("I2C device found at address 0x");
  49.       if (address<16)
  50.         Serial.print("0");
  51.       Serial.print(address,HEX);
  52.       Serial.println("  !");
  53.       nDevices++;
  54.     }
  55.     else if (error==4)
  56.     {
  57.       Serial.print("Unknow error at address 0x");
  58.       if (address<16)
  59.         Serial.print("0");
  60.       Serial.println(address,HEX);
  61.     }    
  62.   }
  63.   if (nDevices == 0)
  64.     Serial.println("No I2C devices found\n");
  65.   else
  66.     Serial.println("done\n");
  67.   delay(5000);           // wait 5 seconds for next scan
  68. }



Poi aprite il Serial Monitor, se il dispositivo viene rilevato, vedrete qualcosa di simile a questo:



Come potete vedere, nel serial monitor è indicato l'indirizzo del dispositivo i2c collegato ad Arduino.
Ora potrete utilizzare questa informazione per comunicare in modo corretto con il dispositivo.



Questo è tutto!
Seguimi sulle mie pagine per rimanere sempre aggiornato sui nuovi post!

Nessun commento:

Posta un commento

Lascia un commento qui sotto, ti risponderò il prima possibile!

Altri Post