I am trying to get a sensor working with I2C in Arduino. I have already used this sensor successfully with another micro. The I2C sequence is basically the same as most others EXCEPT that the datasheet seems to require a NACK prior to ending the transmission. The 4D IDE has a NACK command, and I confirmed that removing the NACK broke the communications. With the NACK command i get data.
The datasheet for the si7021 is http://www.silabs.com/Support%20Documents/TechnicalDocs/Si7021.pdf
On pg 19 is the sequence with the NACK after receiving 2nd Byte
If I am correct in my assumption, how do I send a NACK from Arduino?
My testing code is below. I never receive any data and also never get the “2nd wire end” reply.
Am I doing something wrong?
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial communication at 9600bps
}
int reading = 0;
void loop()
{
// step 1: instruct sensor to read echoes
Wire.beginTransmission(0x80); // transmit to device
Serial.println("wire begin 0x80");
Wire.write(byte(0xE5)); // sets register pointer to the command register (0xE5)
if (Wire.endTransmission () == 0)
{
Serial.println("1st wire end");
}
// step 2: wait for readings to happen
delay(70);
// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(0x81); // transmit to device in read mode
// step 4: request reading from sensor
Wire.requestFrom(0x81, 2); // request 2 bytes from slave device
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
}
if (Wire.endTransmission () == 0){
Serial.println("2nd wire end");
}
delay(250); // wait a bit since people have to read the output :)
}