Skip to main content
shortened and fixed wording
Source Link
Ian
  • 11k
  • 3
  • 24
  • 65

Why are they loopingmust I loop 127 times for a "7-bit" address in this example?

So I am decided to get a Arduino to learnlearning 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.
        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(" !");
    }
}

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

A realize this is a probably total newbie question but keep in mind I know nothing about this stuff so I wouldn't even know where to look to understand what that means...

thanks!

Why are they looping 127 times for a "7-bit" address in this example?

So I am decided to get a Arduino to learn about I2C. 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.
        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(" !");
    }
}

As far as I understand a bit is just 1, so why if it's 7 bits does it loop from 0 - 127?

A realize this is a probably total newbie question but keep in mind I know nothing about this stuff so I wouldn't even know where to look to understand what that means...

thanks!

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.
        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(" !");
    }
}

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

Source Link
Kenn
  • 193
  • 6

Why are they looping 127 times for a "7-bit" address in this example?

So I am decided to get a Arduino to learn about I2C. 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.
        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(" !");
    }
}

As far as I understand a bit is just 1, so why if it's 7 bits does it loop from 0 - 127?

A realize this is a probably total newbie question but keep in mind I know nothing about this stuff so I wouldn't even know where to look to understand what that means...

thanks!