# Why must I loop 127 times for a "7-bit" address in this example?

I am learning about I2C on the Arduino. I was looking at a sample program to scan for I2C devices and saw this:

// This sketch tests the standard 7-bit addresses
// from 0 to 127. Devices with higher bit address
// might not be seen properly.


With the following code.

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

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


As far as I understand it, a bit is just 1. So, why how do 7 bits loop from 0 - 127?

• Why did this get down voted? The question is as clear as I could make given my understanding of the subject (which isn't much) and I learned a lot from the other posters responses.
– Kenn
Feb 6 '13 at 13:51

If I understood your question correctly, you have a problem understanding binary numbers, right?

Think of a decimal digit. A digit is only either of 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9, right? But a 7-digit number can be between 0000000 and 9999999. I presume you do understand this.

Now a decimal number, for example 2569 is actually a short form of writing like this (again in base 10 (i.e. decimal)):

9 * 10^0   +   6 * 10^1   +   5 * 10^2   +   2 * 10^3


If you have a number in another base, for example 1423 in base 5, then that is equivalent to writing the following (in base 10):

3 * 5^0   +   2 * 5^1   +   4 * 5^2   +   1 * 5^3


Which is basically the same, except all the 10s are changed for 5. That is, in the expanded form, each digit is multiplied by b^p where b is the base and p is its position in the number.

Now you know that in base 10, you have digits from 0 to 9. Similarly, in base 5, you have digits from 0 to 4. In base 2, you have digits from 0 to 1, which in other words is just 0 and 1.

So if you have a binary number like 1101, its expanded form is (in base 10):

1 * 2^0   +   0 * 2^1   +   1 * 2^2   +   1 * 2^3   = 13


If you have a 7 digit number in binary, which we normally call a 7-bit number, you can represent all numbers from 0000000 to 1111111. The first number corresponds to 0 and the second one corresponds to:

1*2^0 + 1*2^1 + 1*2^2 + 1*2^3 + 1*2^4 + 1*2^5 + 1*2^6
= 1 + 2 + 4 + 8 + 16 + 32 + 64


or simply 127.

P.S. The following loop:

for (address = 0; address <= 127; address++)


loops 128 times! ;)

"7-bit" is like saying "7-digit", but implies base two instead of base ten. So, a 7-bit number can represent $2^7=128$ values in the same way that a "conventional" 7-digit number can represent $10^7=10000000$ values.

The comments in this example are not quite clear. I2C uses 7-bit or 10-bit addresses. This example simply walks through the address possibilities of a 7-bits only, 0-127 decimal or (0x00-0x7f hex), and tries to read the status from each one. If 'no error' is encountered, it means a valid device(slave) was found at that address. It does not look at 10-bit addresses.

As mentioned above 7-bits means possibility of 128 slave devices attached to the same i2c bus.

This loop is run 128 times(0-127) to determine how many slaves are present on the bus by asking them to respond(via ack) and prints whenever a device presence is detected.

• Thanks - I have learned much about that stuff since I got into it. It totally makes sense now.
– Kenn
Feb 25 '13 at 21:00