2

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.

Gerben
11.3k3 gold badges22 silver badges34 bronze badges
asked Aug 21, 2015 at 18:44
3
  • Please post a link to this library. And preferably to the AS3935. That way your question can be answered more readily. Commented Aug 21, 2015 at 21:59
  • Datasheet www1.futureelectronics.com/doc/AUSTRIAMICROSYSTEMS/AS3935.pdf Commented 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 line Commented Aug 22, 2015 at 7:45

1 Answer 1

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.

answered Jan 3, 2018 at 11:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.