Arduino Forum

Development => Other Software Development => Topic started by: robtillaart on Nov 05, 2013, 04:53 pm

Title: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Nov 05, 2013, 04:53 pm
There are several good I2C scanners available for the Arduino.
However I missed the feature to scan if a device is reachable at different speeds.

So time to roll my own ...

(I2C EEPROM 24LC256  attached to I2C bus)

when
#define PRINTALL false
the output looks like
Code: [Select]

Multispeed - I2C Scanner - V0.1 -

ADDR ADDR 10 50 100 200 400 [KHz]
-----------------------------------------------------------------------
80 0x50 V V V V V

done...


when
#define PRINTALL true
the output looks like
Code: [Select]
Multispeed - I2C Scanner - V0.1 -

ADDR ADDR 10 50 100 200 400 [KHz]
-----------------------------------------------------------------------
0 0x0 . . . . .
1 0x1 . . . . .
2 0x2 . . . . .
3 0x3 . . . . .
4 0x4 . . . . .
5 0x5 . . . . .
6 0x6 . . . . .
7 0x7 . . . . .
8 0x8 . . . . .
9 0x9 . . . . .
10 0xA . . . . .
11 0xB . . . . .
12 0xC . . . . .
13 0xD . . . . .
14 0xE . . . . .
15 0xF . . . . .
16 0x10 . . . . .
17 0x11 . . . . .
18 0x12 . . . . .
19 0x13 . . . . .
20 0x14 . . . . .
21 0x15 . . . . .
22 0x16 . . . . .
23 0x17 . . . . .
24 0x18 . . . . .
25 0x19 . . . . .
26 0x1A . . . . .
27 0x1B . . . . .
28 0x1C . . . . .
29 0x1D . . . . .
30 0x1E . . . . .
31 0x1F . . . . .
32 0x20 . . . . .
33 0x21 . . . . .
34 0x22 . . . . .
35 0x23 . . . . .
36 0x24 . . . . .
37 0x25 . . . . .
38 0x26 . . . . .
39 0x27 . . . . .
40 0x28 . . . . .
41 0x29 . . . . .
42 0x2A . . . . .
43 0x2B . . . . .
44 0x2C . . . . .
45 0x2D . . . . .
46 0x2E . . . . .
47 0x2F . . . . .
48 0x30 . . . . .
49 0x31 . . . . .
50 0x32 . . . . .
51 0x33 . . . . .
52 0x34 . . . . .
53 0x35 . . . . .
54 0x36 . . . . .
55 0x37 . . . . .
56 0x38 . . . . .
57 0x39 . . . . .
58 0x3A . . . . .
59 0x3B . . . . .
60 0x3C . . . . .
61 0x3D . . . . .
62 0x3E . . . . .
63 0x3F . . . . .
64 0x40 . . . . .
65 0x41 . . . . .
66 0x42 . . . . .
67 0x43 . . . . .
68 0x44 . . . . .
69 0x45 . . . . .
70 0x46 . . . . .
71 0x47 . . . . .
72 0x48 . . . . .
73 0x49 . . . . .
74 0x4A . . . . .
75 0x4B . . . . .
76 0x4C . . . . .
77 0x4D . . . . .
78 0x4E . . . . .
79 0x4F . . . . .
80 0x50 V V V V V
81 0x51 . . . . .
82 0x52 . . . . .
83 0x53 . . . . .
84 0x54 . . . . .
85 0x55 . . . . .
86 0x56 . . . . .
87 0x57 . . . . .
88 0x58 . . . . .
89 0x59 . . . . .
90 0x5A . . . . .
91 0x5B . . . . .
92 0x5C . . . . .
93 0x5D . . . . .
94 0x5E . . . . .
95 0x5F . . . . .
96 0x60 . . . . .
97 0x61 . . . . .
98 0x62 . . . . .
99 0x63 . . . . .
100 0x64 . . . . .
101 0x65 . . . . .
102 0x66 . . . . .
103 0x67 . . . . .
104 0x68 . . . . .
105 0x69 . . . . .
106 0x6A . . . . .
107 0x6B . . . . .
108 0x6C . . . . .
109 0x6D . . . . .
110 0x6E . . . . .
111 0x6F . . . . .
112 0x70 . . . . .
113 0x71 . . . . .
114 0x72 . . . . .
115 0x73 . . . . .
116 0x74 . . . . .
117 0x75 . . . . .
118 0x76 . . . . .
119 0x77 . . . . .
120 0x78 . . . . .
121 0x79 . . . . .
122 0x7A . . . . .
123 0x7B . . . . .
124 0x7C . . . . .
125 0x7D . . . . .
126 0x7E . . . . .
127 0x7F . . . . .

done...


I'm thinking about adding a few more features and make it menu driven , so one can do different tests without reloading.

as always, remarks and comments are welcome

latest code - https://github.com/RobTillaart/Arduino/tree/master/sketches/MultiSpeedI2CScanner -
Code: [Select]

//
//    FILE: MultiSpeedI2CScanner.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.04
// PURPOSE: I2C scanner @different speeds
//    DATE: 2013-11-05
//     URL: http://forum.arduino.cc/index.php?topic=197360
//
// Released to the public domain
//

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

// scans devices from 50 to 800KHz I2C speeds.
// lower than 50 is not possible
// DS3231 RTC works on 800 KHz. TWBR = 2; (?)
long speed[] = {
  50, 100, 200, 250, 400, 500, 800 };
const int speeds = sizeof(speed)/sizeof(speed[0]);

// DELAY BETWEEN TESTS
#define RESTORE_LATENCY  5    // for delay between tests of found devices.
bool delayFlag = false;

// MINIMIZE OUTPUT
bool printAll = true;
bool header = true;

// STATE MACHINE
enum states {
  STOP, ONCE, CONT, HELP };
states state = STOP;

uint32_t startScan;
uint32_t stopScan;

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  displayHelp();
}


void loop()
{
  switch (getCommand())
  {
  case 's':
    state = ONCE;
    break;
  case 'c':
    state = CONT;
    break;
  case 'd':
    delayFlag = !delayFlag;
    Serial.print(F("<delay="));
    Serial.println(delayFlag?F("5>"):F("0>"));
    break;
  case 'e':
    // eeprom test TODO
    break;
  case 'h':
    header = !header;
    Serial.print(F("<header="));
    Serial.println(header?F("yes>"):F("no>"));
    break;
  case '?':
    state = HELP;
    break;
  case 'p':
    printAll = !printAll;
    Serial.print(F("<print="));
    Serial.println(printAll?F("all>"):F("found>"));
    break;
  case 'q':
    state = HELP;
    break;
  default:
    break;
  }

  switch(state)
  {
  case ONCE:
    I2Cscan();
    state = HELP;
    break;
  case CONT:
    I2Cscan();
    delay(1000);
    break;   
  case HELP:
    displayHelp();
    state = STOP;
    break;
  case STOP:
    break;
  default: // ignore all non commands
    break;
  }
}

char getCommand()
{
  char c = '\0';
  if (Serial.available())
  {
    c = Serial.read();
  }
  return c;
}

void displayHelp()
{
  Serial.println(F("\nArduino I2C Scanner - 0.1.03\n"));
  Serial.println(F("\ts = single scan"));
  Serial.println(F("\tc = continuous scan - 1 second delay"));
  Serial.println(F("\tq = quit continuous scan"));
  Serial.println(F("\td = toggle latency delay between successful tests."));
  Serial.println(F("\tp = toggle printAll - printFound."));
  Serial.println(F("\th = toggle header - noHeader."));
  Serial.println(F("\t? = help - this page"));
  Serial.println();
}


void I2Cscan()
{
  startScan = millis();
  uint8_t count = 0;

  if (header)
  {
    Serial.print(F("TIME\tDEC\tHEX\t"));
    for (uint8_t s = 0; s < speeds; s++)
    {
      Serial.print(F("\t"));
      Serial.print(speed[s]);
    }
    Serial.println(F("\t[KHz]"));
    for (uint8_t s = 0; s < speeds + 5; s++)
    {
      Serial.print(F("--------"));
    }
    Serial.println();
  }

  // TEST
  // 0.1.04: tests only address range 8..120
  // --------------------------------------------
  // Address R/W Bit Description
  // 0000 000   0 General call address
  // 0000 000   1 START byte
  // 0000 001   X CBUS address
  // 0000 010   X reserved - different bus format
  // 0000 011   X reserved - future purposes
  // 0000 1XX   X High Speed master code
  // 1111 1XX   X reserved - future purposes
  // 1111 0XX   X 10-bit slave addressing
  for (uint8_t address = 8; address < 120; address++)
  {
    bool printLine = printAll;
    bool found[speeds];
    bool fnd = false;

    for (uint8_t s = 0; s < speeds ; s++)
    {
      TWBR = (F_CPU/(speed[s]*1000) - 16)/2;
      Wire.beginTransmission (address);
      found[s] = (Wire.endTransmission () == 0);
      fnd |= found[s];
      // give device 5 millis
      if (fnd && delayFlag) delay(RESTORE_LATENCY);
    }

    if (fnd) count++;
    printLine |= fnd;

    if (printLine)
    {
      Serial.print(millis());
      Serial.print(F("\t"));
      Serial.print(address, DEC);
      Serial.print(F("\t0x"));
      Serial.print(address, HEX);
      Serial.print(F("\t"));

      for (uint8_t s = 0; s < speeds ; s++)
      {
        Serial.print(F("\t"));
        Serial.print(found[s]? F("V"):F("."));
      }
      Serial.println();
    }
  }

  stopScan = millis();
  if (header)
  {
    Serial.println();
    Serial.print(count);
    Serial.print(F(" devices found in "));
    Serial.print(stopScan - startScan);
    Serial.println(F(" milliseconds."));
  }
}
Title: Re: MultiSpeed I2C Scanner - 10,50,100,200,400 KHz.
Post by: rodriguesluis on Nov 06, 2013, 10:30 am
hi there, it's possible the sensor can work with different speed I2C, but only send that data only in at one speed spec??

thank you for your code and for your time

José Luis Rodrigues
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Nov 06, 2013, 02:10 pm

hi there, it's possible the sensor can work with different speed I2C, but only send that data only in at one speed spec??
...

Good question, I don't know.
What I know is that an I2C EEPROM like the 24LC256, has an ~5 millisecond latency after a write. In this period the chip is not reachable (read or write). So different I2C devices can have artefacts, and when I extend this tool I hope to be able to handle them.
FYI my EEPROM lib can be found - http://playground.arduino.cc/Main/LibraryForI2CEEPROM -

The scanner now only checks address reachability (sort of ping for I2C) as there are I2C devices that do not work on all speeds.
For specific devices I want to add additional tests (EEPROM, RTC, DISPLAY, PCF8574, ...)
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Nov 16, 2013, 02:23 pm
Have added some (menu driven) features. When the application starts you get this menu
Code: [Select]

Arduino I2C Scanner - 0.1.02

s = single scan
c = continuous scan - 1 second delay
q = quit continuous scan
d = toggle delay(5) between addresses.
p = toggle printAll - printFound.
h = toggle header - noHeader.
? = help - this page

It can do just a single scan, and it gives output as shown in post above.
A continuous scan, gives the same output with a one second delay in between. This is useful to patch/change address lines to.
To stop the continuous scanning press q (quit).

Then there are three flags that modifies the behaviour.
d: adds 5 millisecond delay between addresses. This is because some devices have a latency and will not react immediately. I'm not happy with this behaviour and it will change in next version.

p: imho the most useful flag, print complete list of addresses or only the ones found. Reduces the output to something the eye can follow.
h: removes the header/footer from the output so only the lines of addresses responding will be shown. Works especially well in combination with p, and I have doubted for some time to combine the p and h flag.

The combination I use often is printFound, noHeader and continuous. It gives output like
Code: [Select]

Arduino I2C Scanner - 0.1.02

s = single scan
c = continuous scan - 1 second delay
q = quit continuous scan
d = toggle delay(5) between addresses.
p = toggle printAll - printFound.
h = toggle header - noHeader.
? = help - this page

<print=found>
<header=no>
104 0x68 V V V V V V V
104 0x68 V V V V V V V
104 0x68 V V V V V V V
104 0x68 V V V V V V V
104 0x68 V V V V V V V
104 0x68 V V V V V V V
104 0x68 V V V V V V V
104 0x68 V V V V V V V


One can see the number of columns has changed as I added additional speeds for I2C: 250 and 500 KHz, 2 speeds at which my RTC and EEPROM work quite well. I also tested 1000 KHz and got mixed results. The RTC did not work reliably at that speed.
(I might try 800 KHz, also a perfect divider of 16 MHz.)

Although still work in progress, the tool already met some of its goals. First on my list is to get delay() behaviour right.

As always comments and remarks are welcome.

update: - code (0.1.02 version)  removed.
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Nov 16, 2013, 04:26 pm
update:
+ removed the 10 KHz test as TWBR is not valid for 10KHz as TWBR is a byte .. :smiley-red:  
+ updated title of thread.

new 0.1.03 version:  (replaces all earlier versions)
+ removed 10 KHz
+ added 800 KHz (my RTC DS3231 seems to work reliably on this speed)
+ fixed TWBR math
+ d: delay toggle => now only waits for 5 millis if device is found.
+ added millis() time stamp in the output.

latest version of the code see first post
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: pito on Nov 16, 2013, 06:31 pm
FYI: scan of the GY-80 10DOF module (with v. 0.1.03):
Code: [Select]
TIME    DEC     HEX             50      100     200     250     400     500     800     [KHz]
------------------------------------------------------------------------------------------------
1561    0       0x0             .       .       .       .       .       .       .
1563    1       0x1             .       .       .       .       .       .       V
1565    2       0x2             .       .       .       .       .       .       .
1568    3       0x3             .       .       .       .       .       .       V
1570    4       0x4             .       .       .       .       .       .       .
..
1618    24      0x18            .       .       .       .       .       .       .
1620    25      0x19            .       .       .       .       .       .       V
1623    26      0x1A            .       .       .       .       .       .       .
1626    27      0x1B            .       .       .       .       .       .       V
1628    28      0x1C            .       .       .       .       .       .       .
1631    29      0x1D            .       .       .       .       .       .       V
1633    30      0x1E            V       V       V       V       V       V       .
1635    31      0x1F            .       .       .       .       .       .       V
1638    32      0x20            .       .       .       .       .       .       .
1640    33      0x21            .       .       .       .       .       .       V
1643    34      0x22            .       .       .       .       .       .       .
1645    35      0x23            .       .       .       .       .       .       V
1647    36      0x24            .       .       .       .       .       .       .
1650    37      0x25            .       .       .       .       .       .       V
1652    38      0x26            .       .       .       .       .       .       .
1655    39      0x27            .       .       .       .       .       .       V
1657    40      0x28            .       .       .       .       .       .       .
1659    41      0x29            .       .       .       .       .       .       V
1662    42      0x2A            .       .       .       .       .       .       .
..
1756    80      0x50            .       .       .       .       .       .       .
1759    81      0x51            .       .       .       .       .       .       V
1761    82      0x52            .       .       .       .       .       .       .
1764    83      0x53            V       V       V       V       V       V       V
1766    84      0x54            .       .       .       .       .       .       .
1768    85      0x55            .       .       .       .       .       .       V
1771    86      0x56            .       .       .       .       .       .       .
1773    87      0x57            .       .       .       .       .       .       V
1776    88      0x58            .       .       .       .       .       .       .
1778    89      0x59            .       .       .       .       .       .       V
1780    90      0x5A            .       .       .       .       .       .       .
1783    91      0x5B            .       .       .       .       .       .       V
1785    92      0x5C            .       .       .       .       .       .       .
1788    93      0x5D            .       .       .       .       .       .       V
1790    94      0x5E            .       .       .       .       .       .       .
1793    95      0x5F            .       .       .       .       .       .       V
1796    96      0x60            .       .       .       .       .       .       .
1798    97      0x61            .       .       .       .       .       .       V
1801    98      0x62            .       .       .       .       .       .       .
1803    99      0x63            .       .       .       .       .       .       V
1805    100     0x64            .       .       .       .       .       .       .
1808    101     0x65            .       .       .       .       .       .       V
1810    102     0x66            .       .       .       .       .       .       .
1813    103     0x67            .       .       .       .       .       .       V
1815    104     0x68            .       .       .       .       .       .       .
1818    105     0x69            V       V       V       V       V       V       V
1820    106     0x6A            .       .       .       .       .       .       .
1823    107     0x6B            .       .       .       .       .       .       V
1825    108     0x6C            .       .       .       .       .       .       .
1828    109     0x6D            .       .       .       .       .       .       V
1830    110     0x6E            .       .       .       .       .       .       .
1833    111     0x6F            .       .       .       .       .       .       V
1836    112     0x70            .       .       .       .       .       .       .
1839    113     0x71            .       .       .       .       .       .       V
1841    114     0x72            .       .       .       .       .       .       .
1844    115     0x73            .       .       .       .       .       .       V
1846    116     0x74            .       .       .       .       .       .       .
1849    117     0x75            .       .       .       .       .       .       V
1851    118     0x76            .       .       .       .       .       .       .
1854    119     0x77            V       V       V       V       V       V       V
1856    120     0x78            .       .       .       .       .       .       .
1859    121     0x79            .       .       .       .       .       .       V
1861    122     0x7A            .       .       .       .       .       .       .
1864    123     0x7B            .       .       .       .       .       .       V
1866    124     0x7C            .       .       .       .       .       .       .
1869    125     0x7D            .       .       .       .       .       .       V
1871    126     0x7E            .       .       .       .       .       .       .
1874    127     0x7F            .       .       .       .       .       .       V

66 devices found in 322 milliseconds.
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Nov 16, 2013, 08:38 pm
Hi Pito,
Very interesting results especially the 800 KHz results at every odd address. I have not seen it

What board are you using?
What is the value of the pull up resistors used? I assume at this speed the squareness of the signal is very important?
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: pito on Nov 16, 2013, 08:59 pm
Solder-less breadboard, 1284p @16MHz, 3V3, maybe 10k pullups (I have to check).
I'll try with smaller pullups..
2k2 pullups:
Code: [Select]
TIME    DEC     HEX             50      100     200     250     400     500     800     [KHz]
------------------------------------------------------------------------------------------------
303     0       0x0             .       .       .       .       .       .       .
306     1       0x1             .       .       .       .       .       .       V
308     2       0x2             .       .       .       .       .       .       .
310     3       0x3             .       .       .       .       .       .       V
312     4       0x4             .       .       .       .       .       .       .
314     5       0x5             .       .       .       .       .       .       .
316     6       0x6             .       .       .       .       .       .       .
319     7       0x7             .       .       .       .       .       .       V
321     8       0x8             .       .       .       .       .       .       .
323     9       0x9             .       .       .       .       .       .       .
325     10      0xA             .       .       .       .       .       .       .
327     11      0xB             .       .       .       .       .       .       .
329     12      0xC             .       .       .       .       .       .       .
332     13      0xD             .       .       .       .       .       .       .
334     14      0xE             .       .       .       .       .       .       .
336     15      0xF             .       .       .       .       .       .       V
338     16      0x10            .       .       .       .       .       .       .
342     17      0x11            .       .       .       .       .       .       .
344     18      0x12            .       .       .       .       .       .       .
346     19      0x13            .       .       .       .       .       .       .
349     20      0x14            .       .       .       .       .       .       .
351     21      0x15            .       .       .       .       .       .       .
353     22      0x16            .       .       .       .       .       .       .
356     23      0x17            .       .       .       .       .       .       .
358     24      0x18            .       .       .       .       .       .       .
360     25      0x19            .       .       .       .       .       .       .
363     26      0x1A            .       .       .       .       .       .       .
365     27      0x1B            .       .       .       .       .       .       .
367     28      0x1C            .       .       .       .       .       .       .
370     29      0x1D            .       .       .       .       .       .       .
372     30      0x1E            V       V       V       V       V       V       .
374     31      0x1F            .       .       .       .       .       .       V
377     32      0x20            .       .       .       .       .       .       .
379     33      0x21            .       .       .       .       .       .       .
381     34      0x22            .       .       .       .       .       .       .
385     35      0x23            .       .       .       .       .       .       .
..
412     47      0x2F            .       .       .       .       .       .       .
415     48      0x30            .       .       .       .       .       .       .
417     49      0x31            .       .       .       .       .       .       .
419     50      0x32            .       .       .       .       .       .       .
422     51      0x33            .       .       .       .       .       .       .
424     52      0x34            .       .       .       .       .       .       .
427     53      0x35            .       .       .       .       .       .       .
430     54      0x36            .       .       .       .       .       .       .
432     55      0x37            .       .       .       .       .       .       .
434     56      0x38            .       .       .       .       .       .       .
437     57      0x39            .       .       .       .       .       .       .
439     58      0x3A            .       .       .       .       .       .       .
441     59      0x3B            .       .       .       .       .       .       .
444     60      0x3C            .       .       .       .       .       .       .
446     61      0x3D            .       .       .       .       .       .       .
448     62      0x3E            .       .       .       .       .       .       .
451     63      0x3F            .       .       .       .       .       .       V
453     64      0x40            .       .       .       .       .       .       .
455     65      0x41            .       .       .       .       .       .       .
..
482     76      0x4C            .       .       .       .       .       .       .
484     77      0x4D            .       .       .       .       .       .       .
487     78      0x4E            .       .       .       .       .       .       .
489     79      0x4F            .       .       .       .       .       .       .
491     80      0x50            .       .       .       .       .       .       .
494     81      0x51            .       .       .       .       .       .       .
496     82      0x52            .       .       .       .       .       .       .
498     83      0x53            .       V       V       V       V       V       .
500     84      0x54            .       .       .       .       .       .       .
503     85      0x55            .       .       .       .       .       .       .
505     86      0x56            .       .       .       .       .       .       .
..
541     101     0x65            .       .       .       .       .       .       .
543     102     0x66            .       .       .       .       .       .       .
546     103     0x67            .       .       .       .       .       .       .
548     104     0x68            .       .       .       .       .       .       .
551     105     0x69            V       V       V       V       V       V       .
553     106     0x6A            .       .       .       .       .       .       .
557     107     0x6B            .       .       .       .       .       .       .
559     108     0x6C            .       .       .       .       .       .       .
561     109     0x6D            .       .       .       .       .       .       .
564     110     0x6E            .       .       .       .       .       .       .
566     111     0x6F            .       .       .       .       .       .       .
569     112     0x70            .       .       .       .       .       .       .
571     113     0x71            .       .       .       .       .       .       .
573     114     0x72            .       .       .       .       .       .       .
576     115     0x73            .       .       .       .       .       .       .
578     116     0x74            .       .       .       .       .       .       .
581     117     0x75            .       .       .       .       .       .       .
583     118     0x76            .       .       .       .       .       .       .
585     119     0x77            V       V       V       V       V       V       .
588     120     0x78            .       .       .       .       .       .       .
590     121     0x79            .       .       .       .       .       .       .
593     122     0x7A            .       .       .       .       .       .       .
595     123     0x7B            .       .       .       .       .       .       .
598     124     0x7C            .       .       .       .       .       .       .
601     125     0x7D            .       .       .       .       .       .       .
603     126     0x7E            .       .       .       .       .       .       .
606     127     0x7F            .       .       .       .       .       .       V


The same with 680ohm pullups. The GY-80 module includes a level translator afaik, so the pullups value may not have direct impact on the signal shape at the sensors..
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Nov 16, 2013, 09:04 pm
I have pull ups of 3.2K (measured value), so my signal might be more "square"
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: Krodal on Feb 10, 2014, 07:01 am
I have added a link to your MultiSpeed I2C Scanner in http://playground.arduino.cc/Main/I2cScanner
See the "Interesting Links" section.
But I don't like that link to reply 4.

robtillaart, could you give your Multispeed I2C Scanner it's own place and adjust that link in the i2c_scanner ?
You could add it to the i2c_scanner page, but perhaps that is confusing for new users, that page is very simple now, and that is better for new users.
Another option to edit your first post, and make that the main article and for your scanner.
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Feb 10, 2014, 07:00 pm
good proposal,
will put the link in the first post.
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Feb 10, 2014, 07:13 pm
@krodal
+ moved last code to first post
+ updated link in the 12csanner page
+ added a small header in i2c scanner page (you may remove it)
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Apr 20, 2014, 10:31 am
Short testrun on a MEGA r3 today => worked smooth.
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: Thee on Apr 23, 2014, 07:25 am
Hi Rob,

I used your MultispeedScanner 0.1.03 for Test an I2C Circuit. It works fine with a Duemilanova, but if I use an Arduino Due (with levelshifter from 3.3V to 5V) I got no Answer from any I2C chip. The change for the Due I did is  to comment out the  "TWBR-Statement". I used IDE 1.5.6-R2.
In this Postings you can find als some scope and small pieces from the a logic analyzer.
http://forum.arduino.cc/index.php?topic=234999.msg1692208#msg1692208 (http://forum.arduino.cc/index.php?topic=234999.msg1692208#msg1692208)

Regards, Detlef
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Apr 23, 2014, 12:48 pm
AFAIK, the DUE has problems with I2C  but I never dived into the details.

Can you get meaningful results from other I2C scanners on the DUE?
- http://playground.arduino.cc/Main/I2cScanner -
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: orangkucing on Jun 17, 2014, 07:46 pm
Two modifications were necessary for MultiSpeed I2C Scanner to work with Due.

1. TWBR must be emulated by adding the following code after the #include statements (cf.  https://github.com/jrowberg/i2cdevlib/issues/48 (https://github.com/jrowberg/i2cdevlib/issues/48)):

#if defined(__arm__)
class TWBRemulation
{
public:
   inline TWBRemulation & operator = (int val) __attribute__((always_inline)) {
   if (val == 12 || val == ((F_CPU / 400000) - 16) / 2) {
       #if F_BUS == 48000000
       I2C0_F = 0x1A; // 400 kHz
       #elif F_BUS == 24000000
       I2C0_F = 0x45; // 400 kHz
       #endif
   }
   return *this;
}
};
TWBRemulation TWBR;
#endif


2. Due's pull-up resistors are unsuitable for most I2C devices (cf. http://forum.arduino.cc/index.php?PHPSESSID=n5nfq19euhrhc12gtv03r0j3j2&topic=146802.15 (http://forum.arduino.cc/index.php?PHPSESSID=n5nfq19euhrhc12gtv03r0j3j2&topic=146802.15)). Thus I removed the onboard network resistor, and used external 4.7k pull-ups.

(Maybe using the Wire1 library instead of Wire and connecting an externally-pulluped I2C bus to SCL1/SDA1 pins will work, too, but I just checked the above mentioned workaround works.)

And please use a Nightly build version of Arduino IDE. Because the Wire library before 1.5.7 including the current 1.5.6r2 has a serious bug on the return values of TwoWire::endTransmission function. (cf. https://github.com/arduino/Arduino/pull/1994 (https://github.com/arduino/Arduino/pull/1994) )
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Jun 17, 2014, 09:11 pm
Thanks for these additions  (have no time to confirm, sorry )

Did you get meaningful results with the I2C scanner now?
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: orangkucing on Jun 18, 2014, 12:49 am
Oh....
I'm sorry.  This morning I found the addition 1 is meaningless and not working as multi speeds.  It only scans at 100kHz.
So if you only need to scan at 100kHz, simply comment out the TWBR code and use the nightly ide 1.5.7.

(cf. http://www.i2cdevlib.com/forums/topic/88-mpu-dmp6-with-arduino-due-twbr-not-declared/ (http://www.i2cdevlib.com/forums/topic/88-mpu-dmp6-with-arduino-due-twbr-not-declared/))
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Jul 05, 2014, 11:02 am
updated to 0.1.04  (see first post)

main change is restricting the # addresses to be scanned as I2C address 0..7 and 120..127 have special meanings.

or download from here - https://github.com/RobTillaart/Arduino/tree/master/sketches/MultiSpeedI2CScanner -
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Jul 06, 2014, 10:44 pm
updated to version 0.1.05 on github
- https://github.com/RobTillaart/Arduino/tree/master/sketches/MultiSpeedI2CScanner -

not tested enough imho, so not updated in the first post.

as always comments and remarks are welcome.
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: Peter_n on Dec 07, 2014, 05:29 pm
Hi,
I have added the Multi Speed I2C to my webserver, running on a Arduino Mega 2560.

But when I let it try 500kHz my webserver crashes.
Is it possible to detect a I2C error without causing a crash ?

In Arduino 1.5.8 the new Wire.setClock() can be used.
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Dec 07, 2014, 06:36 pm
Is it possible to detect a I2C error without causing a crash ?
Really depends on the cause of the crash.
Does the I2C scanner work when used standalone (no webserver)?
Does the webserver crash if you just set the I2C clock to 500KHz?

You may post your code ....
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: Peter_n on Dec 08, 2014, 02:12 am
I found the problem, but I don't know how to solve it.
Setting the TWBR down to 2 (for 800kHz) is normally no problem.

Only in the situation when I have a Arduino as Slave at 5V and another Arduino as Slave at 3.3V (using a level shifter). In that situation the sketch never returnes from Wire.endTransmission() when the speed is too high. At the moment it happens at 670kHz, when TWBR becomes 3.


Below is my function to make a webpage.
On the 'home' page I have a link to "SCAN". When I click it, the file "SCAN" is requested. But instead loading it from the SD card, I recognize the keyword and call the function "MultiSpeedI2CScanner".
Everything (table, colspan) is only up to 400kHz. To allow the higher speeds, the html code for the table should be adjusted.

Code: [Select]

// MultiSpeedI2CScanner
// To be used in a webserver, it outputs HTML code with the result
// The new Wire.setClock() is available in Arduino IDE 1.5.8, but that is not used yet.
// It is assumed that Wire.begin() has been called already.
//
// Using:
//
//  FILE: MultiSpeedI2CScanner.ino
//  AUTHOR: Rob Tillaart
//  VERSION: 0.1.04
//  PURPOSE: I2C scanner @different speeds
//  DATE: 2013-11-05
//  URL: http://forum.arduino.cc/index.php?topic=197360
//
// Released to the public domain
//

#include <Wire.h>
#include <Ethernet.h>


// scans devices from 50 to 800KHz I2C speeds.
// lower than 50 is not possible
// DS3231 RTC works on 800 KHz. TWBR = 2; (?)
const long speed[] = {                             // 'const' added
  50, 100, 200, 250, 400 };            // , 500, 800 };       // only up to 400 !
const int speeds = sizeof(speed)/sizeof(speed[0]);

// DELAY BETWEEN TESTS
#define RESTORE_LATENCY  5    // for delay between tests of found devices.
bool delayFlag = false;

// MINIMIZE OUTPUT
bool printAll = false;
bool header = true;


void MultiSpeedI2CScanner(EthernetClient *pClient)
{
  //  Wire.begin();          // already called in the sketch of my webserver

  pClient->println(F("<html>"));
  pClient->println(F("<head>"));
  pClient->println(F("<meta content=\"text/html; charset=UTF-8\" http-equiv=\"content-type\">"));
  pClient->println(F("<title>Multi Speed I2C scanner</title>"));
 
  // Make a nice style for the table.
  pClient->println(F("<style type=\"text/css\">"));
  pClient->println(F("table { color:#303030; border-width: 1px; border-color: #0000A0; border-collapse: collapse; }"));
  pClient->println(F("table th { border-width: 1px; padding: 6px; border-style: solid; border-color: #0000A0; background-color: #C0FFFF; }"));
  pClient->println(F("table td { border-width: 1px; padding: 6px; border-style: solid; border-color: #0000A0; background-color: #D0D0FF; }"));
  pClient->println(F("</style>"));
 
  pClient->println(F("</head>"));
  pClient->println(F("<body>"));
 
  I2Cscan( pClient);

  pClient->println(F("</body>"));
  pClient->println(F("</html>"));
}

void I2Cscan(EthernetClient *pClient)
{
  uint32_t startScan;
  uint32_t stopScan;
 
  startScan = millis();
  uint8_t count = 0;

  // The style is defined in the header.
  pClient->println(F("<table>"));
  pClient->println(F("<tbody>"));

  if (header)
  {
    pClient->print(F("<tr>"));
    pClient->print(F("<th colspan=\"8\">Multi Speed I2C scanner</th>"));
    pClient->println(F("</tr>"));
   
    pClient->print(F("<tr>"));
    pClient->print(F("<th>TIME</th>"));
    pClient->print(F("<th colspan=\"2\">Address</th>"));
    pClient->print(F("<th colspan=\"5\">Speed [kHz]</th>"));
    pClient->println(F("</tr>"));

    pClient->print(F("<tr>"));
    pClient->print(F("<th>&nbsp;</th>"));
    pClient->print(F("<th>DEC</th>"));
    pClient->print(F("<th>HEX</th>"));
   
    for (uint8_t s = 0; s < speeds; s++)
    {
      pClient->print(F("<th>"));
      pClient->print(speed[s]);
      pClient->print(F("</th>"));
    }
   
    pClient->println(F("</tr>"));
  }

  // TEST
  // 0.1.04: tests only address range 8..120
  // --------------------------------------------
  // Address R/W Bit Description
  // 0000 000   0 General call address
  // 0000 000   1 START byte
  // 0000 001   X CBUS address
  // 0000 010   X reserved - different bus format
  // 0000 011   X reserved - future purposes
  // 0000 1XX   X High Speed master code
  // 1111 1XX   X reserved - future purposes
  // 1111 0XX   X 10-bit slave addressing
  for (uint8_t address = 8; address < 120; address++)
  {
    bool printLine = printAll;
    bool found[speeds];
    bool fnd = false;

    for (uint8_t s = 0; s < speeds ; s++)
    {
      TWBR = (F_CPU/(speed[s]*1000) - 16)/2;

      Wire.beginTransmission (address);
      found[s] = (Wire.endTransmission () == 0);
      fnd |= found[s];
     
      // give device 5 millis
      if (fnd && delayFlag)
        delay(RESTORE_LATENCY);
    }

    if (fnd) count++;
    printLine |= fnd;

    if (printLine)
    {
      pClient->print(F("<tr>"));

      pClient->print(F("<td>"));
      pClient->print(millis());
      pClient->print(F("</td>"));

      pClient->print(F("<td>"));
      pClient->print(address, DEC);
      pClient->print(F("</td>"));

      pClient->print(F("<td>0x"));
      pClient->print(address, HEX);
      pClient->print(F("</td>"));

      for (uint8_t s = 0; s < speeds ; s++)
      {
        pClient->print(F("<td>"));
        pClient->print(found[s]? F("V"):F("."));
        pClient->print(F("</td>"));
      }
      pClient->println(F("</tr>"));

    }
  }

  pClient->println(F("</table>"));

  stopScan = millis();
  if (header)
  {
    pClient->println(F("<br>"));
    pClient->print(count);
    pClient->print(F(" devices found in "));
    pClient->print(stopScan - startScan);
    pClient->print(F(" milliseconds."));
    pClient->println(F("<br>"));
  }
}
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Dec 08, 2014, 08:30 pm
The problem might be that the interrupts of printing interfere with the bits of the I2C bus.
You need a logic analyser for that I guess.
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: Peter_n on Dec 09, 2014, 02:51 am
I think that my I2C bus is crumbling down at 670kHz. The sketch for the webpage is the resulting sketch with the client.prints. I tested it with your sketch with and without Serial.prints, with and without delay and so on. I even did Serial.flush() everywhere to be sure that the Serial library was not longer busy. The result was still the same.

I was assuming that my I2C could be best at 100kHz, because of wires and Arduino Slaves, but now I feel confident to use 200kHz :smiley-grin:

Well, I didn't buy an oscilloscope (yet) : http://forum.arduino.cc/index.php?topic=281572.0 (http://forum.arduino.cc/index.php?topic=281572.0)
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: derniwi on Dec 17, 2014, 09:07 am
Hi,

for using Arduino Due you need at least Arduino IDE 1.5.8. The guys have changed some parts including wire. So you can use this tool if you replace this line from the code:
Code: [Select]

      TWBR = (F_CPU/(speed[s]*1000) - 16)/2;

with this part
Code: [Select]
#if ARDUINO >= 158
      Wire.setClock(speed[s]*1000);
#else
      TWBR = (F_CPU/(speed[s]*1000) - 16)/2;
#endif

I tried this with the Arduino nightly build of 1.6.0 and it works fine on my Due.

Best regards
Nils
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Dec 17, 2014, 08:54 pm
added this to the scanner 0.1.06 version

- https://github.com/RobTillaart/Arduino/tree/master/sketches/MultiSpeedI2CScanner -
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: MrMirza on Jul 25, 2015, 10:37 am
Hi,

for using Arduino Due you need at least Arduino IDE 1.5.8. The guys have changed some parts including wire. So you can use this tool if you replace this line from the code:
Code: [Select]

      TWBR = (F_CPU/(speed[s]*1000) - 16)/2;

with this part
Code: [Select]
#if ARDUINO >= 158
      Wire.setClock(speed[s]*1000);
#else
      TWBR = (F_CPU/(speed[s]*1000) - 16)/2;
#endif

I tried this with the Arduino nightly build of 1.6.0 and it works fine on my Due.

Best regards
Nils
I did it but ID.exe crashes while compiling !
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: MrMirza on Jul 25, 2015, 10:42 am
Serial port Monitor returns this line : .."›ْ5گ.ضè,_ذPbڑMڑسح

any idea?

I'm using Mega2560 with RAMPS 1.4 and LCD is connected on "ReprapsDiscount Smart controller"

My problem is that lcd dos not start working !
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Jul 25, 2015, 09:01 pm
do you have links to the RAMPS1.4 - LCD datasheets?
what is a ReprapsDiscount Smart controller?
how are these connected?

the I2C pins for a MEGA are different than on an UNO (did you use the right ones?)

For I2C you also need GND connected, and 4K7 pull up resistors are needed.

How do you power the device?


It looks like your PC uses an Arabic font? is that possible?
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Jul 25, 2015, 11:55 pm
Started beta coding (0.1.07 beta)  on support for scanning Wire and Wire1 if supported e.g. DUE.

If some other functionality is missing, please let me know. I might add it :)
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: MrMirza on Jul 28, 2015, 08:57 am
do you have links to the RAMPS1.4 - LCD datasheets?
what is a ReprapsDiscount Smart controller?
how are these connected?

the I2C pins for a MEGA are different than on an UNO (did you use the right ones?)

For I2C you also need GND connected, and 4K7 pull up resistors are needed.

How do you power the device?


It looks like your PC uses an Arabic font? is that possible?
The red board at right is "ReprapsDiscount Smart controller"


باتشکر از یو آپلود (http://uupload.ir/) برای میزبانی رایگان عکس

I did not do any pin or use any data sheets because all the boards are pre-installed !


The board uses the usb power and an externall adaptor for stepmotors !

It is not "Arabic", It is "Farsi"
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: MrMirza on Jul 28, 2015, 12:21 pm
I found the problem, The baud rate in port scanner was wrong !

now the problem is "0 devices found in 346 milliseconds"

anw tnx for yr attention and yr codes !
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Jul 28, 2015, 09:26 pm
> It is not "Arabic", It is "Farsi"
You are right, sorry

do you have some datasheet of that board?
I'm not familiar with it
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: MrMirza on Jul 29, 2015, 08:24 am
Np, u r welcome

In RepRap Wiki (http://www.reprap.org/wiki/RepRapDiscount_Smart_Controller) it is as below
LCD Connect Schematics (http://www.reprap.org/mediawiki/images/7/79/LCD_connect_SCHDOC.pdf)
RepRapDiscount Controller Final Schematics (http://www.reprap.org/mediawiki/images/7/70/Controller_final_reprapdiscount.pdf)
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: MrMirza on Jul 29, 2015, 02:06 pm
It seems that VDD pin power is 4.3v , is it the reason that no lcd is found by scanner codes even lcd's ledes turn on ?
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Jul 29, 2015, 03:18 pm
how is the system powered?
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: MrMirza on Jul 31, 2015, 02:50 pm
ATX 12 V , 20 A and USB
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: istvanlacza on Jan 06, 2017, 04:02 pm
There are several good I2C scanners available for the Arduino.
However I missed the feature to scan if a device is reachable at different speeds.

So time to roll my own ...

(I2C EEPROM 24LC256  attached to I2C bus)

when
#define PRINTALL false
the output looks like
Code: [Select]

Multispeed - I2C Scanner - V0.1 -

ADDR ADDR 10 50 100 200 400 [KHz]
-----------------------------------------------------------------------
80 0x50 V V V V V

done...


when
#define PRINTALL true
the output looks like
Code: [Select]
Multispeed - I2C Scanner - V0.1 -

ADDR ADDR 10 50 100 200 400 [KHz]
-----------------------------------------------------------------------
0 0x0 . . . . .
1 0x1 . . . . .
2 0x2 . . . . .
3 0x3 . . . . .
4 0x4 . . . . .
5 0x5 . . . . .
6 0x6 . . . . .
7 0x7 . . . . .
8 0x8 . . . . .
9 0x9 . . . . .
10 0xA . . . . .
11 0xB . . . . .
12 0xC . . . . .
13 0xD . . . . .
14 0xE . . . . .
15 0xF . . . . .
16 0x10 . . . . .
17 0x11 . . . . .
18 0x12 . . . . .
19 0x13 . . . . .
20 0x14 . . . . .
21 0x15 . . . . .
22 0x16 . . . . .
23 0x17 . . . . .
24 0x18 . . . . .
25 0x19 . . . . .
26 0x1A . . . . .
27 0x1B . . . . .
28 0x1C . . . . .
29 0x1D . . . . .
30 0x1E . . . . .
31 0x1F . . . . .
32 0x20 . . . . .
33 0x21 . . . . .
34 0x22 . . . . .
35 0x23 . . . . .
36 0x24 . . . . .
37 0x25 . . . . .
38 0x26 . . . . .
39 0x27 . . . . .
40 0x28 . . . . .
41 0x29 . . . . .
42 0x2A . . . . .
43 0x2B . . . . .
44 0x2C . . . . .
45 0x2D . . . . .
46 0x2E . . . . .
47 0x2F . . . . .
48 0x30 . . . . .
49 0x31 . . . . .
50 0x32 . . . . .
51 0x33 . . . . .
52 0x34 . . . . .
53 0x35 . . . . .
54 0x36 . . . . .
55 0x37 . . . . .
56 0x38 . . . . .
57 0x39 . . . . .
58 0x3A . . . . .
59 0x3B . . . . .
60 0x3C . . . . .
61 0x3D . . . . .
62 0x3E . . . . .
63 0x3F . . . . .
64 0x40 . . . . .
65 0x41 . . . . .
66 0x42 . . . . .
67 0x43 . . . . .
68 0x44 . . . . .
69 0x45 . . . . .
70 0x46 . . . . .
71 0x47 . . . . .
72 0x48 . . . . .
73 0x49 . . . . .
74 0x4A . . . . .
75 0x4B . . . . .
76 0x4C . . . . .
77 0x4D . . . . .
78 0x4E . . . . .
79 0x4F . . . . .
80 0x50 V V V V V
81 0x51 . . . . .
82 0x52 . . . . .
83 0x53 . . . . .
84 0x54 . . . . .
85 0x55 . . . . .
86 0x56 . . . . .
87 0x57 . . . . .
88 0x58 . . . . .
89 0x59 . . . . .
90 0x5A . . . . .
91 0x5B . . . . .
92 0x5C . . . . .
93 0x5D . . . . .
94 0x5E . . . . .
95 0x5F . . . . .
96 0x60 . . . . .
97 0x61 . . . . .
98 0x62 . . . . .
99 0x63 . . . . .
100 0x64 . . . . .
101 0x65 . . . . .
102 0x66 . . . . .
103 0x67 . . . . .
104 0x68 . . . . .
105 0x69 . . . . .
106 0x6A . . . . .
107 0x6B . . . . .
108 0x6C . . . . .
109 0x6D . . . . .
110 0x6E . . . . .
111 0x6F . . . . .
112 0x70 . . . . .
113 0x71 . . . . .
114 0x72 . . . . .
115 0x73 . . . . .
116 0x74 . . . . .
117 0x75 . . . . .
118 0x76 . . . . .
119 0x77 . . . . .
120 0x78 . . . . .
121 0x79 . . . . .
122 0x7A . . . . .
123 0x7B . . . . .
124 0x7C . . . . .
125 0x7D . . . . .
126 0x7E . . . . .
127 0x7F . . . . .

done...


I'm thinking about adding a few more features and make it menu driven , so one can do different tests without reloading.

as always, remarks and comments are welcome

latest code - https://github.com/RobTillaart/Arduino/tree/master/sketches/MultiSpeedI2CScanner -
Code: [Select]

//
//    FILE: MultiSpeedI2CScanner.ino
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.04
// PURPOSE: I2C scanner @different speeds
//    DATE: 2013-11-05
//     URL: http://forum.arduino.cc/index.php?topic=197360
//
// Released to the public domain
//

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

// scans devices from 50 to 800KHz I2C speeds.
// lower than 50 is not possible
// DS3231 RTC works on 800 KHz. TWBR = 2; (?)
long speed[] = {
  50, 100, 200, 250, 400, 500, 800 };
const int speeds = sizeof(speed)/sizeof(speed[0]);

// DELAY BETWEEN TESTS
#define RESTORE_LATENCY  5    // for delay between tests of found devices.
bool delayFlag = false;

// MINIMIZE OUTPUT
bool printAll = true;
bool header = true;

// STATE MACHINE
enum states {
  STOP, ONCE, CONT, HELP };
states state = STOP;

uint32_t startScan;
uint32_t stopScan;

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  displayHelp();
}


void loop()
{
  switch (getCommand())
  {
  case 's':
    state = ONCE;
    break;
  case 'c':
    state = CONT;
    break;
  case 'd':
    delayFlag = !delayFlag;
    Serial.print(F("<delay="));
    Serial.println(delayFlag?F("5>"):F("0>"));
    break;
  case 'e':
    // eeprom test TODO
    break;
  case 'h':
    header = !header;
    Serial.print(F("<header="));
    Serial.println(header?F("yes>"):F("no>"));
    break;
  case '?':
    state = HELP;
    break;
  case 'p':
    printAll = !printAll;
    Serial.print(F("<print="));
    Serial.println(printAll?F("all>"):F("found>"));
    break;
  case 'q':
    state = HELP;
    break;
  default:
    break;
  }

  switch(state)
  {
  case ONCE:
    I2Cscan();
    state = HELP;
    break;
  case CONT:
    I2Cscan();
    delay(1000);
    break;    
  case HELP:
    displayHelp();
    state = STOP;
    break;
  case STOP:
    break;
  default: // ignore all non commands
    break;
  }
}

char getCommand()
{
  char c = '\0';
  if (Serial.available())
  {
    c = Serial.read();
  }
  return c;
}

void displayHelp()
{
  Serial.println(F("\nArduino I2C Scanner - 0.1.03\n"));
  Serial.println(F("\ts = single scan"));
  Serial.println(F("\tc = continuous scan - 1 second delay"));
  Serial.println(F("\tq = quit continuous scan"));
  Serial.println(F("\td = toggle latency delay between successful tests."));
  Serial.println(F("\tp = toggle printAll - printFound."));
  Serial.println(F("\th = toggle header - noHeader."));
  Serial.println(F("\t? = help - this page"));
  Serial.println();
}


void I2Cscan()
{
  startScan = millis();
  uint8_t count = 0;

  if (header)
  {
    Serial.print(F("TIME\tDEC\tHEX\t"));
    for (uint8_t s = 0; s < speeds; s++)
    {
      Serial.print(F("\t"));
      Serial.print(speed[s]);
    }
    Serial.println(F("\t[KHz]"));
    for (uint8_t s = 0; s < speeds + 5; s++)
    {
      Serial.print(F("--------"));
    }
    Serial.println();
  }

  // TEST
  // 0.1.04: tests only address range 8..120
  // --------------------------------------------
  // Address R/W Bit Description
  // 0000 000   0 General call address
  // 0000 000   1 START byte
  // 0000 001   X CBUS address
  // 0000 010   X reserved - different bus format
  // 0000 011   X reserved - future purposes
  // 0000 1XX   X High Speed master code
  // 1111 1XX   X reserved - future purposes
  // 1111 0XX   X 10-bit slave addressing
  for (uint8_t address = 8; address < 120; address++)
  {
    bool printLine = printAll;
    bool found[speeds];
    bool fnd = false;

    for (uint8_t s = 0; s < speeds ; s++)
    {
      TWBR = (F_CPU/(speed[s]*1000) - 16)/2;
      Wire.beginTransmission (address);
      found[s] = (Wire.endTransmission () == 0);
      fnd |= found[s];
      // give device 5 millis
      if (fnd && delayFlag) delay(RESTORE_LATENCY);
    }

    if (fnd) count++;
    printLine |= fnd;

    if (printLine)
    {
      Serial.print(millis());
      Serial.print(F("\t"));
      Serial.print(address, DEC);
      Serial.print(F("\t0x"));
      Serial.print(address, HEX);
      Serial.print(F("\t"));

      for (uint8_t s = 0; s < speeds ; s++)
      {
        Serial.print(F("\t"));
        Serial.print(found[s]? F("V"):F("."));
      }
      Serial.println();
    }
  }

  stopScan = millis();
  if (header)
  {
    Serial.println();
    Serial.print(count);
    Serial.print(F(" devices found in "));
    Serial.print(stopScan - startScan);
    Serial.println(F(" milliseconds."));
  }
}

Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: Allar on Feb 20, 2017, 11:16 pm
I'm having an issue with the I2C scanners both the multispeed and the regular one.

I am using the Nucleo L476RG (STM32L476RGT6U).

The results for both the I2C scanner and the multispeed end up comming with nothing.

Using the code by Rob Tillaart V 0.1.06
Code: [Select]

TIME DEC HEX 50 100 200 250 400 500 800 [KHz]
------------------------------------------------------------------------------------------------
55701 0 0x00 . . . . . . .
55703 1 0x01 . . . . . . .
55706 2 0x02 . . . . . . .
55709 3 0x03 . . . . . . .
55711 4 0x04 . . . . . . .
55714 5 0x05 . . . . . . .
55717 6 0x06 . . . . . . .
55719 7 0x07 . . . . . . .
55722 8 0x08 . . . . . . .
55725 9 0x09 . . . . . . .
55727 10 0x0A . . . . . . .
55730 11 0x0B . . . . . . .
55733 12 0x0C . . . . . . .
55736 13 0x0D . . . . . . .
55738 14 0x0E . . . . . . .
55741 15 0x0F . . . . . . .
55744 16 0x10 . . . . . . .
55747 17 0x11 . . . . . . .
55750 18 0x12 . . . . . . .
55752 19 0x13 . . . . . . .
55755 20 0x14 . . . . . . .
55758 21 0x15 . . . . . . .
55761 22 0x16 . . . . . . .
55763 23 0x17 . . . . . . .
55766 24 0x18 . . . . . . .
55769 25 0x19 . . . . . . .
55772 26 0x1A . . . . . . .
55774 27 0x1B . . . . . . .
55777 28 0x1C . . . . . . .
55780 29 0x1D . . . . . . .
55783 30 0x1E . . . . . . .
55785 31 0x1F . . . . . . .
55788 32 0x20 . . . . . . .
55791 33 0x21 . . . . . . .
55794 34 0x22 . . . . . . .
55796 35 0x23 . . . . . . .
55799 36 0x24 . . . . . . .
55802 37 0x25 . . . . . . .
55805 38 0x26 . . . . . . .
55807 39 0x27 . . . . . . .
55810 40 0x28 . . . . . . .
55813 41 0x29 . . . . . . .
55816 42 0x2A . . . . . . .
55819 43 0x2B . . . . . . .
55821 44 0x2C . . . . . . .
55824 45 0x2D . . . . . . .
55827 46 0x2E . . . . . . .
55830 47 0x2F . . . . . . .
55832 48 0x30 . . . . . . .
55835 49 0x31 . . . . . . .
55838 50 0x32 . . . . . . .
55841 51 0x33 . . . . . . .
55843 52 0x34 . . . . . . .
55846 53 0x35 . . . . . . .
55849 54 0x36 . . . . . . .
55852 55 0x37 . . . . . . .
55854 56 0x38 . . . . . . .
55857 57 0x39 . . . . . . .
55860 58 0x3A . . . . . . .
55863 59 0x3B . . . . . . .
55865 60 0x3C . . . . . . .
55868 61 0x3D . . . . . . .
55871 62 0x3E . . . . . . .
55874 63 0x3F . . . . . . .
55876 64 0x40 . . . . . . .
55879 65 0x41 . . . . . . .
55882 66 0x42 . . . . . . .
55885 67 0x43 . . . . . . .
55888 68 0x44 . . . . . . .
55890 69 0x45 . . . . . . .
55893 70 0x46 . . . . . . .
55896 71 0x47 . . . . . . .
55899 72 0x48 . . . . . . .
55901 73 0x49 . . . . . . .
55904 74 0x4A . . . . . . .
55907 75 0x4B . . . . . . .
55910 76 0x4C . . . . . . .
55912 77 0x4D . . . . . . .
55915 78 0x4E . . . . . . .
55918 79 0x4F . . . . . . .
55921 80 0x50 . . . . . . .
55923 81 0x51 . . . . . . .
55926 82 0x52 . . . . . . .
55929 83 0x53 . . . . . . .
55932 84 0x54 . . . . . . .
55934 85 0x55 . . . . . . .
55937 86 0x56 . . . . . . .
55940 87 0x57 . . . . . . .
55943 88 0x58 . . . . . . .
55945 89 0x59 . . . . . . .
55948 90 0x5A . . . . . . .
55951 91 0x5B . . . . . . .
55954 92 0x5C . . . . . . .
55957 93 0x5D . . . . . . .
55959 94 0x5E . . . . . . .
55962 95 0x5F . . . . . . .
55965 96 0x60 . . . . . . .
55968 97 0x61 . . . . . . .
55970 98 0x62 . . . . . . .
55973 99 0x63 . . . . . . .
55976 100 0x64 . . . . . . .
55979 101 0x65 . . . . . . .
55982 102 0x66 . . . . . . .
55984 103 0x67 . . . . . . .
55987 104 0x68 . . . . . . .
55990 105 0x69 . . . . . . .
55993 106 0x6A . . . . . . .
55996 107 0x6B . . . . . . .
55999 108 0x6C . . . . . . .
56001 109 0x6D . . . . . . .
56004 110 0x6E . . . . . . .
56007 111 0x6F . . . . . . .
56010 112 0x70 . . . . . . .
56013 113 0x71 . . . . . . .
56016 114 0x72 . . . . . . .
56019 115 0x73 . . . . . . .
56021 116 0x74 . . . . . . .
56024 117 0x75 . . . . . . .
56027 118 0x76 . . . . . . .
56030 119 0x77 . . . . . . .
56033 120 0x78 . . . . . . .
56036 121 0x79 . . . . . . .
56039 122 0x7A . . . . . . .
56041 123 0x7B . . . . . . .
56044 124 0x7C . . . . . . .
56047 125 0x7D . . . . . . .
56050 126 0x7E . . . . . . .
56053 127 0x7F . . . . . . .

0 devices found in 368 milliseconds.

Arduino I2C Scanner - 0.1.06

Scanmode:
s = single scan
c = continuous scan - 1 second delay
q = quit continuous scan
d = toggle latency delay between successful tests. 0 - 5 ms
Output:
p = toggle printAll - printFound.
h = toggle header - noHeader.
a = toggle address range, 0..127 - 8..120
Speeds:
0 = 50 - 800 Khz
1 = 100 KHz only
2 = 200 KHz only
4 = 400 KHz only
8 = 800 KHz only

? = help - this page



However if I address the device (BME280 0x76) on its own with no scanner I can RW to it without issues.
Any Ideas?
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: robtillaart on Jul 15, 2017, 01:55 pm
Finally got some time to add a new feature to the MultiSpeed I2C scanner, one I needed myself :) .

TEENSY_3.5 support  - tested Wire1 and Wire2. (TEENSY_3.6 uses the same libs, not tested)

The @ key is used to step though the available I2C ports.

You can find the latest 0.1.7 version on github @
https://github.com/RobTillaart/Arduino/tree/master/sketches/MultiSpeedI2CScanner

As always remarks and comments are welcome,

Rob
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: pe1mxp on Dec 31, 2017, 12:54 am
Really the sketch I needed. Very good written.
Title: Re: MultiSpeed I2C Scanner - 50,100,200,400 KHz.
Post by: Geochemuino on Aug 21, 2019, 08:26 pm
How can I use to Same I2C devices as Slaves?
I connect Three QMC5883L to Arduino uno and scanned with all I2C_Scanner codes but they just show me one address as 0*D and writes " one device found"
??????