Has someone experience with interfacing the AS3935 lightning sensor using I2C? I found a library but it only works with SPI as far as I can tell. I really don't want to give up 4 pins for SPI as I need those for other things. I2C would be great as I use a couple more sensors with I2C so no extra pins only for AS3935.
-
Please post a link to this library. And preferably to the AS3935. That way your question can be answered more readily.Nick Gammon– Nick Gammon ♦2015年08月21日 21:59:23 +00:00Commented Aug 21, 2015 at 21:59
-
Datasheet www1.futureelectronics.com/doc/AUSTRIAMICROSYSTEMS/AS3935.pdfMarkU– MarkU2015年08月22日 07:43:08 +00:00Commented Aug 22, 2015 at 7:43
-
I've used other I2C devices with Arduino UNO (version 1.6 IDE); a word of advice: since AS3935 wants I2C repeated-start condition (Sr) to read device registers, you may need to patch the Wire library... see forum.arduino.cc/index.php?topic=137607.0 and github.com/helenarobotics/Arduino/commit/… -- Modification to file C:\Program Files (x86)\Arduino-1.6.0\hardware\arduino\avr\libraries\Wire\utility/twi.c --- line 468 twi_stop(); // <---- comment out this lineMarkU– MarkU2015年08月22日 07:45:38 +00:00Commented Aug 22, 2015 at 7:45
1 Answer 1
Just use these functions to read/write AS3935 registers, replacing the SPI ones (_devAddr is the device address, usually 0x03) :
// read/write functions for I2C mode
uint8_t AS3935::_i2cRead(uint8_t addr)
{
// send register number
Wire.beginTransmission(_devAddr);
Wire.write(addr);
Wire.endTransmission(false); // <<<--- THE 'false' here is mandatory!
// request register data
Wire.requestFrom(_devAddr, (uint8_t)1);
// read data
return Wire.read();
}
void AS3935::_i2cWrite(uint8_t addr, uint8_t data)
{
Wire.beginTransmission(_devAddr);
Wire.write(addr);
Wire.write(data);
Wire.endTransmission();
}
The "trick" is to use Wire.endTransmission(false) inside the read routine; this avoid to send the stop bit, which is mandatory for AS3935 read operation.