JTK – Notes and Ramblings Things we have found …

5/13/2020

Lightning

Filed under: General,Home Automation — taing @ 1:18 pm

My original Embedded Adventures MOD-1016v8 lightning detector based on the as3935 failed. I ordered a replacement from Sparkfun. The Sparkfun hook-up guide discusses the sensors I2C interface being not as reliable as the SPI interface. Sparkfun also has a github for the PCB. Originally I was using code based on a Python I2C library. So a change is needed.

My first thought was to port the Sparkfun Arduino library over to Raspberry Pi using wiringPi. It should be noted that as of August 2019 the author of wiringPi is no longer supporting it. This was a bit frustrating but after a bit of testing with the scope and bus pirate and a lot of googling. This issues seem to be resolved.

At first there was data on MOSI but nothing coming back. After attaching the Bus Pirate using the setup below:

m 5 (for SPI mode)
Speed: 125khz
Clock polarity: Idle low (default)
Output Clock Edge: Active to idle (default)
Input sample phase: End (NOT default)
CS: /CS (default)
Output type: Normal

I was able to communicate. Notice the Input Phase angle being set to “end”. When it was set to “middle”, the default, writes worked but reads shifted the result one bit to the right effectively dropping the LSB. This turns out to be vital for the wiringPi code also. The standard SPI help page for wiringPi doesn’t mention anything about mode or phase. But our friend google gave me a discussion about possible modes. So instead of the basic wiringPiSPISetup(channel, speed) function I needed to use wiringPiSPISetupMode(channel, speed, mode). Bit 0 is clock phase, bit 1 is clock polarity. The other bits are not possible via wiringPi but as discussed elsewhere can be set by a direct ioctl call.

I also slowed the speed down toe 125khz from the original 2Mhz.

For wiringPi i used the following pins:

GND: pi pin 25
SCK: pi pin 23 (SCLK)
MISO: pi pin 21 (MISO)
MOSI: pi pin 19 (MOSI)
3.3v: pi pin 17
INT: pi pin 15 (GPIO 22)
CS: pi pin 24 (CE0 / GPIO 8)

I considered a version using bcm2835 but didn’t go very far down this road. No work has been done on that front.

I also decided to play a bit with Sparkfun and CircuitPython and sparkfun-circuitpython-qwiicas3935 1.0.3. The complete CircuitPython bundle is on github. The as3935 bundle can also be found on github.

The Pi I was working with is a Pi II with Jesse installed. Installing Python 3.5 or higher on Jesse is more work than simply making a newer Buster Lite sd card. Then it is a matter of following the Sparkfun / AdaFruit instructions to get all of the necessities installed:

sudo apt-get install python3-pip
sudo pip3 install --upgrade setuptools
pip3 install RPI.GPIO
pip3 install sparkfun-circuitpython-qwiicas3935

The secret to getting this to work is realizing that you can’t use CE0/GPIO 8 for CS with the code provided. Circuit Python Bus Device(documentation) can’t manage the pin directly when SPI is in use and the hardware doesn’t handle it correctly either. Using Pi Pin 13 / GPIO 27 / circuit Python board.D27 as CS and things work fine.

An alternative is to change the _read_register() function to use write_readinto() in place of write() followed by readinto(). Using these the system manages CE0 just fine for CS.

The library file sparkfun_qwiicas3935.py is installed at /usr/local/lib/python3.7/dist-packages. My mods can be found here. The key bit is:

<code>self._spi.write_readinto(bytearray([addr,0]), result</code>)
# self._spi.write(bytearray([addr]))
<code># read the next byte after writing the address</code>
# self._spi.readinto(result)

The modified code also comments out the manual CS line manipulation.

Additional resources can be found at Instructables.

A copy of the AS3935 datasheet can be found here.

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress