Go Down

Topic: Assertion failure when the sketch is running (Read 1 time) previous topic - next topic


I am trying to get a 3.3V I2C temperature sensor working.
I modified a Wire, range sensor example. It uploads without problems. But the serial monitor shows a new kind of error when the sketch tries to run.:
Code: [Select]
assertion "(address & 0x80) == 0" failed: file "../source/twi.c", line 279, function: TWI_StartWrite
Exiting with status 1.

The error message gives source/twi.c path but in my machine the only twi.c is in C:\Program Files\arduino\hardware\arduino\avr\libraries\Wire\utility.

This is the whole code.

Code: [Select]
#include <Wire.h>
int reading = 0;

void setup()
  Wire.begin();                // join i2c bus (address optional for master)
  Serial.begin(9600);          // start serial communication at 9600bps

void loop()
   delay(70);                   // datasheet suggests at least 65 milliseconds

  // step 3: instruct sensor to return a particular echo reading
  Wire.beginTransmission(0x5a<<1); // transmit to device #112
  Wire.write(byte(0x08));      // sets register pointer to echo #1 register (0x02)
  Wire.endTransmission();      // stop transmitting

  // step 4: request reading from sensor
  Wire.requestFrom(0x5a<<1, 2);    // request 2 bytes from slave device #112

  // step 5: receive reading from sensor
  if(2 <= Wire.available())    // if two bytes were received
    reading = Wire.read();  // receive high byte (overwrites previous reading)
    reading = reading << 8;    // shift high byte to be high 8 bits
    reading |= Wire.read(); // receive low byte as lower 8 bits
    Serial.println(reading);   // print the reading

  delay(250);                  // wait a bit since people have to read the output :)


Hi there,

I'm using the DUE and now have the same problem. It's just as you have described it in the code. Well I have found out, that at some point the Arduino is replacing the address by 0xFF. In my code there is an ISR to talk to the I2C and after this had run 502 times it starts to play up. I don't have any clue why it is doing sth like this. Do you have the same problem or already a solution?


Mike S.


Mar 22, 2016, 03:44 pm Last Edit: Mar 22, 2016, 03:50 pm by alarmsiren
I direct your attention to the following page: Wire Reference, in particular the following quoted note:

There are both 7- and 8-bit versions of I2C addresses. 7 bits identify the device, and the eighth bit determines if it's being written to or read from. The Wire library uses 7 bit addresses throughout. If you have a datasheet or sample code that uses 8 bit address, you'll want to drop the low bit (i.e. shift the value one bit to the right), yielding an address between 0 and 127. However the addresses from 0 to 7 are not used because are reserved so the first address that can be used is 8.
The failed assertion is designed to ensure that the eighth bit is not set, because as the note above states, such an address is not correct for use in the Arduino Wire library.

Your code looks like this:

Code: [Select]
Wire.beginTransmission(0x5a<<1); // transmit to device #112

0x5A is your address. That by itself is perfectly valid. If in your datasheet that's an 8-bit address, you'll need to shift it right by one, but what you've done is left shift it which makes it bigger not smaller. It results in 0xB4, which has the eighth bit set, hence the assertion failure.

You should use either of:
Code: [Select]
Wire.beginTransmission(0x5a); //Use this if your address is 7-bit
Code: [Select]
Wire.beginTransmission(0x5a>>1); //Use this if your address is 8-bit


Your problem sounds like memory corruption since its not instant, and unrelated to LMI's issue (apart from the sympton). You'll need to post your code if you want anyone to be able to help you track that problem down.

Go Up