Simple test

Ensure your device works with this simple test.

examples/qwiicas3935_simpletest.py
 1# SPDX-FileCopyrightText: Copyright (c) 2021 Gaston Williams
 2#
 3# SPDX-License-Identifier: MIT
 4
 5#  This is example is for the SparkFun Qwiic Relay.
 6#  SparkFun sells these at its website: www.sparkfun.com
 7#  Do you like this library? Help support SparkFun. Buy a board!
 8#  https://www.sparkfun.com/products/15168
 9
10"""
11 Qwiic AS3935 Lightning Detector Simple Test - qwiicas3935_simpletest.py
12 Written by Gaston Williams, July 4th, 2019
13 The Qwiic AS3935 is an I2C controlled lightning detector.
14
15 Simple Test:
16 This program uses the Qwiic AS3935 CircuitPython Library to check
17 that status of the Qwiic AS3935 Lightning Detector.
18"""
19import sys
20
21# import busio
22# import digitalio
23import board
24import sparkfun_qwiicas3935
25
26# Create bus object using our board's I2C port
27i2c = board.I2C()
28
29# Create an as3935 library object
30lightning = sparkfun_qwiicas3935.Sparkfun_QwiicAS3935_I2C(i2c)
31
32# OR create a library object using the Bus SPI port
33# CS can be any available GPIO pin
34
35# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
36# cs = digitalio.DigitalInOut(board.D20)
37# cs.direction = digitalio.Direction.OUTPUT
38# lightning = sparkfun_qwiicas3935.Sparkfun_QwiicAS3935_SPI(spi, cs)
39
40# Check if connected
41if lightning.connected:
42    print("AS3935 Lightning Detector connected.")
43else:
44    print("Lightning Detector does not appear to be connected. Please check wiring.")
45    sys.exit()
46
47# Read the Lightning Detector AFE mode and print it out.
48mode = lightning.indoor_outdoor
49
50if mode == lightning.OUTDOOR:
51    print("The Lightning Detector is in the Outdoor mode.")
52elif mode == lightning.INDOOR:
53    print("The Lightning Detector is in the Indoor mode.")
54else:
55    print("The Lightning Detector is in an Unknown mode.")

Examples (I2C)

  1. Basic Lightning (I2C) - Show lightning events.

examples/example1_basic_lightning_i2c.py
  1# SPDX-FileCopyrightText: Copyright (c) 2021 Gaston Williams
  2#
  3# SPDX-License-Identifier: MIT
  4
  5#  This is example is for the SparkFun Qwiic AS3935 Lightning Detector.
  6#  SparkFun sells these at its website: www.sparkfun.com
  7#  Do you like this library? Help support SparkFun. Buy a board!
  8#  https://www.sparkfun.com/products/15276
  9
 10"""
 11 Qwiic AS3935 Lightning Detector Example 1 - example1_basic_lightning.py
 12 Written by Gaston Williams, July 4th, 2019
 13 Based on Arduino code written by
 14 Elias Santistevan @ SparkFun Electronics, May, 2019
 15 The Qwiic AS3935 is an I2C (or SPI) controlled lightning detector.
 16
 17 Example 1 - Basic Lightning (I2C):
 18 This program uses the Qwiic AS3935 CircuitPython Library to
 19 control the Qwiic AS3935 Lightning detector over I2C to listen for
 20 lightning events. The lightning detector determines whether or
 21 not it's actual lightning, a disturber, or noise. In the case
 22 your environment has a lot of noise or electrical disturbances
 23 you can uncomment methods to adjust the noise floor or the disturber
 24 watch dog threshold values.
 25
 26
 27 For this example you will need to connect the INT pin on Qwiic to
 28 GPIO D21 on the Raspberry Pi.  The interrupt pin will go high when an
 29 event occurs.
 30"""
 31import sys
 32from time import sleep
 33import board
 34import digitalio
 35import sparkfun_qwiicas3935
 36
 37# global variables with defaults
 38noise_floor = 2
 39watchdog_threshold = 2
 40
 41# Set up Interrupt pin on GPIO D21 with a pull-down resistor
 42as3935_interrupt_pin = digitalio.DigitalInOut(board.D21)
 43as3935_interrupt_pin.direction = digitalio.Direction.INPUT
 44as3935_interrupt_pin.pull = digitalio.Pull.DOWN
 45
 46# Create bus object using our board's I2C port
 47i2c = board.I2C()
 48
 49# Create as3935 object
 50lightning = sparkfun_qwiicas3935.Sparkfun_QwiicAS3935_I2C(i2c)
 51
 52# define functions
 53def reduce_noise(value):
 54    # This function helps to adjust the sensor to your environment. More
 55    # environmental noise leads to more false positives. If you see lots of noise
 56    # events, try increasing the noise threshold with this function. The datsheet
 57    # warns that smartphone and smart watch displays, DC-DC converters, and/or
 58    # anything that operates in 500 kHz range are noise sources to be avoided.
 59    # The manufacturer's default value is 2 with a maximum value of 7."""
 60    value += 1
 61
 62    if value > 7:
 63        print("Noise floor is at the maximum value.")
 64        return 7
 65    print("Increasing the noise event threshold to ", value)
 66    lightning.noise_level = value
 67    return value
 68
 69
 70def increase_threshold(value):
 71    # This function is similar to the one above in that it will increase the
 72    # antenna's robustness against false positives. However, this function helps
 73    # to increase the robustness against "distrubers" and not "noise". If you
 74    # have a lot of disturbers trying increasing the watchdog threshold.
 75    # The default value is 2 and goes up to 10.
 76
 77    value += 1
 78    if value > 10:
 79        print("Watchdog threshold is at its maximum value")
 80        return 10
 81    print("Increasing the disturber watchdog threshold to ", value)
 82    lightning.watchdog_threshold = value
 83    return value
 84
 85
 86# main code
 87
 88print("AS3935 Franklin Lightning Detector")
 89
 90# Check if connected
 91if lightning.connected:
 92    print("Schmow-ZoW, Lightning Detector Ready!")
 93else:
 94    print("Lightning Detector does not appear to be connected. Please check wiring.")
 95    sys.exit()
 96
 97# get the current noise floor value
 98noise_floor = lightning.noise_level
 99print("The noise floor is ", noise_floor)
100
101# get the current disturber threshold
102watchdog_threshold = lightning.watchdog_threshold
103print("The disturber watchdog threshold is ", watchdog_threshold)
104
105print("Type Ctrl-C to exit program.")
106
107try:
108    while True:
109        # When the interrupt goes high
110        if as3935_interrupt_pin.value:
111            print("Interrupt:", end=" ")
112            interrupt_value = lightning.read_interrupt_register()
113
114            if interrupt_value == lightning.NOISE:
115                print("Noise.")
116                # uncomment line below to adjust the noise level
117                # (see function comments above)
118                # noise_floor = reduce_noise(noise_floor)
119            elif interrupt_value == lightning.DISTURBER:
120                print("Disturber.")
121                # uncomment the line below to adjust the watchdog threshold
122                # (see function comments above)
123                # watchdog_threshold = increase_threshold(watchdog_threshold)
124            elif interrupt_value == lightning.LIGHTNING:
125                print("Lightning strike detected!")
126                print("Approximately: " + str(lightning.distance_to_storm) + "km away!")
127                print("Energy value: " + str(lightning.lightning_energy))
128        sleep(1)
129
130except KeyboardInterrupt:
131    pass
  1. More Lightning Features (I2C) - Functions that prevent noise and false events.

examples/example2_more_lightning_features_i2c.py
  1# SPDX-FileCopyrightText: Copyright (c) 2021 Gaston Williams
  2#
  3# SPDX-License-Identifier: MIT
  4
  5#  This is example is for the SparkFun Qwiic AS3935 Lightning Detector.
  6#  SparkFun sells these at its website: www.sparkfun.com
  7#  Do you like this library? Help support SparkFun. Buy a board!
  8#  https://www.sparkfun.com/products/15276
  9
 10"""
 11 Qwiic AS3935 Example 2 - example2_more_lightning_features.py
 12 Written by Gaston Williams, July 6th, 2019
 13 Based on Arduino code written by
 14 Elias Santistevan @ SparkFun Electronics, May, 2019
 15 The Qwiic AS3935 is an I2C (or SPI) controlled lightning detector.
 16
 17 Example 2 - More Lightning Features (I2C):
 18 This program uses the Qwiic AS3935 CircuitPython Library to
 19 control the Qwiic AS3935 Lightning detector over I2C to listen for
 20 lightning events.  This example provides a more in depth guide
 21 to some more of the library's functions that aid in reducing false
 22 events and noise.
 23
 24 For this example you will need to connect the INT pin on Qwiic to
 25 GPIO D21 on the Raspberry Pi.  The interrupt pin will go high when an
 26 event occurs.
 27"""
 28import sys
 29from time import sleep
 30import board
 31import digitalio
 32import sparkfun_qwiicas3935
 33
 34# Set up Interrupt pin on GPIO D6 with a pull-down resistor
 35as3935_interrupt_pin = digitalio.DigitalInOut(board.D21)
 36as3935_interrupt_pin.direction = digitalio.Direction.INPUT
 37as3935_interrupt_pin.pull = digitalio.Pull.DOWN
 38
 39# Create bus object using our board's I2C port
 40i2c = board.I2C()
 41
 42# Create as3935 object
 43lightning = sparkfun_qwiicas3935.Sparkfun_QwiicAS3935_I2C(i2c)
 44
 45print("AS3935 Franklin Lightning Detector")
 46
 47# Check if connected
 48if lightning.connected:
 49    print("Schmow-ZoW, Lightning Detector Ready!")
 50else:
 51    print("Lightning Detector does not appear to be connected. Please check wiring.")
 52    sys.exit()
 53
 54# You can reset all the lightning detector settings back to their default values
 55# by uncommenting the line below.
 56# lightning.reset()
 57
 58# The lightning detector defaults to an indoor setting (less gain/sensitivity),
 59# if you plan on using this outdoors uncomment the following line:
 60# lightning.indoor_outdoor = lightning.OUTDOOR
 61
 62# Read the Lightning Detector Analog Front End (AFE) mode and print it out.
 63afe_mode = lightning.indoor_outdoor
 64
 65if afe_mode == lightning.OUTDOOR:
 66    print("The Lightning Detector is in the Outdoor mode.")
 67elif afe_mode == lightning.INDOOR:
 68    print("The Lightning Detector is in the Indoor mode.")
 69else:
 70    print("The Lightning Detector is in an Unknown mode.")
 71
 72# Disturbers are events that are false lightning events. If you see a lot
 73# of disturbers you can enable the disturber mask to have the chip not report
 74# those events as an interrupt.
 75# Uncomment one of the lines below to turn on disturber mask on or off
 76# lightning.mask_disturber = True
 77# lightning.mask_disturber = False
 78print("Are disturbers being masked?", end=" ")
 79if lightning.mask_disturber:
 80    print("Yes.")
 81else:
 82    print("No.")
 83
 84
 85# The Noise floor setting from 1-7, one being the lowest.
 86# The default setting is two.
 87# If you need to change the setting, uncomment the line below
 88# lightning.noise_level = 2
 89print("Noise level is set at: " + str(lightning.noise_level))
 90
 91# Watchdog threshold setting can be from 1-10, one being the lowest.
 92# The default setting is two.
 93# If you need to change the setting, uncomment the line below
 94# lightning.watchdog_threshold = 2
 95print("Watchdog Threshold is set to: " + str(lightning.watchdog_threshold))
 96
 97# Spike Rejection setting from 1-11, one being the lowest.
 98# The default setting is two. The shape of the spike is analyzed
 99# during the chip's validation routine. You can round this spike
100# at the cost of sensitivity to distant events.
101# If you need to change the setting, uncomment the line below.
102# lightning.spike_rejection = 2
103print("Spike Rejection is set to: " + str(lightning.spike_rejection))
104
105
106# This setting will change when the lightning detector issues an interrupt.
107# For example you will only get an interrupt after five lightning strikes
108# instead of one. The default is one, and it takes settings of 1, 5, 9 and 16.
109# If you want to change this value, uncomment the line below.
110# lightning.lightning_threshold = 1
111print(
112    "The number of strikes before interrupt is triggered: "
113    + str(lightning.lightning_threshold)
114)
115
116# When the distance to the storm is estimated, it takes into account other
117# lightning that was sensed in the past 15 minutes.
118# If you want to reset the time, you can uncomment the line below.
119# lightning.clear_statistics()
120
121# You can power down your lightning detector. You absolutely must call
122# the wakup funciton afterwards, because it recalibrates the internal
123# oscillators.
124# Uncomment the statements below to test power down and wake up functions
125
126# lightning.power_down()
127# print('AS3935 powered down.')
128# calibrated = lightning.wake_up()
129#
130# if calibrated:
131#    print('Successfully woken up!')
132# else:
133#    print('Error recalibrating internal osciallator on wake up.')
134
135
136print("Type Ctrl-C to exit program.")
137
138try:
139    while True:
140        # When the interrupt goes high
141        if as3935_interrupt_pin.value:
142            print("Interrupt:", end=" ")
143            interrupt_value = lightning.read_interrupt_register()
144
145            if interrupt_value == lightning.NOISE:
146                print("Noise.")
147            elif interrupt_value == lightning.DISTURBER:
148                print("Disturber.")
149            elif interrupt_value == lightning.LIGHTNING:
150                print("Lightning strike detected!")
151                # Distance estimation takes into account previous events.
152                print("Approximately: " + str(lightning.distance_to_storm) + "km away!")
153                # Energy is a pure number with no physical meaning.
154                print("Energy: " + str(lightning.lightning_energy))
155        sleep(1)
156
157except KeyboardInterrupt:
158    pass
  1. Tune Antenna (I2C) - Tune the resonance frequency of the antenna.

examples/example3_tune_antenna_i2c.py
 1# SPDX-FileCopyrightText: Copyright (c) 2021 Gaston Williams
 2#
 3# SPDX-License-Identifier: MIT
 4
 5#  This is example is for the SparkFun Qwiic AS3935 Lightning Detector.
 6#  SparkFun sells these at its website: www.sparkfun.com
 7#  Do you like this library? Help support SparkFun. Buy a board!
 8#  https://www.sparkfun.com/products/15276
 9
10"""
11 Qwiic AS3935 Example 3 - example3_tune_antenna.py
12 Written by Gaston Williams, July 6th, 2019
13 Based on Arduino code written by
14 Elias Santistevan @ SparkFun Electronics, May, 2019
15 The Qwiic AS3935 is an I2C (or SPI) controlled lightning detector.
16
17 Example 3 - Tune Antenna (I2C):
18 This program uses the Qwiic AS3935 CircuitPython Library to control
19 the Qwiic AS3935 Lightning detector over I2C to tune the resonance
20 frequency of the antenna. The chip provides internal capacitance that
21 can be modified by the  library. You'll need a logic analyzer,
22 oscillscope, or some method of reading a 32kHz square wave (depending
23 on the frequency divsior used) on the INT pin.
24"""
25import sys
26import board
27import sparkfun_qwiicas3935
28
29# Create bus object using our board's I2C port
30i2c = board.I2C()
31
32# Create as3935 object
33lightning = sparkfun_qwiicas3935.Sparkfun_QwiicAS3935_I2C(i2c)
34
35print("AS3935 Franklin Lightning Detector")
36
37# Check if connected
38if lightning.connected:
39    print("Ready to tune antenna.")
40else:
41    print("Lightning Detector does not appear to be connected. Please check wiring.")
42    sys.exit()
43
44# You can reset all the lightning detector settings back to their default values
45# by uncommenting the line below.
46# lightning.reset()
47
48# When reading the frequency, keep in mind that the given frequency is
49# divided by 16 by default. This can be changed to be divided by 32, 64, or
50# 128 using the line below. So for example when reading the frequency on a
51# board, if the frequency is 32.05kHz, multipling by 16 gives the result of
52# 512.8kHz. This is 2.56 percent away from the 500kHz ideal value and well
53# within the 3.5 percent optimal tolerance. If one were to change the
54# division ratio to 32, then one would read a value of 16kHz instead.
55# To change the division ratio uncomment the line below
56# lightning.division_ratio = 16
57
58# The following code is just a sanity check. It will return a value of
59# 16 by default but can be 32, 64, or 128, depending on what value you set.
60print("Division Ratio is set to: ", str(lightning.division_ratio))
61
62# Here you can set a value of 0-120, which increases the capacitance on
63# the RLC circuit in steps of 8pF, up to 120pF. The change in frequency is
64# very modest. At 15 (max - 120pF), the frequency is around 490kHz down from
65# 512kHz. The equation for calculating frequency in an RLC circuit is:
66# f = 1/(2pi*sqrt(LC))
67# To change the capacitance, uncomment the line below.
68# lightning.tune_cap = 0
69
70# When reading the internal capcitor value, it will return the value in pF.
71print("Internal Capacitor is set to: " + str(lightning.tune_cap))
72
73# This will tell the AS3935 to display the resonance frequncy as a digital
74# signal on the interrupt pin. There are two other internal oscillators
75# that within the AS3935 that can also be displayed on this line. See the
76# datasheet for more information.
77# Uncomment the lines below to turn the display on and off. To start
78# displaying the antenna frequency, use True as the state parameter. To
79# stop displaying the frequncy on the interrupt line, use False.
80
81# print('----Displaying oscillator on INT pin.----')
82# lightning.display_oscillator(True, lightning.ANTENNA_FREQ)
83# print('----Stop Display of oscillator on INT pin.----')
84# lightning.display_oscillator(False, lightning.ANTENNA_FREQ)

Examples (SPI)

  1. Basic Lightning (SPI) - Show lightning events.

examples/example1_basic_lightning_spi.py
  1# SPDX-FileCopyrightText: Copyright (c) 2021 Gaston Williams
  2#
  3# SPDX-License-Identifier: MIT
  4
  5#  This is example is for the SparkFun Qwiic AS3935 Lightning Detector.
  6#  SparkFun sells these at its website: www.sparkfun.com
  7#  Do you like this library? Help support SparkFun. Buy a board!
  8#  https://www.sparkfun.com/products/15276
  9
 10"""
 11 Qwiic AS3935 Lightning Detector Example 1 - example1_basic_lightning.py
 12 Written by Gaston Williams, July 4th, 2019
 13 Based on Arduino code written by
 14 Elias Santistevan @ SparkFun Electronics, May, 2019
 15 The Qwiic AS3935 is an I2C (or SPI) controlled lightning detector.
 16
 17 Example 1 - Basic Lightning (SPI):
 18 This program uses the Qwiic AS3935 CircuitPython Library to
 19 control the Qwiic AS3935 Lightning detector over SPI to listen for
 20 lightning events. The lightning detector determines whether or
 21 not it's actual lightning, a disturber, or noise. In the case
 22 your environment has a lot of noise or electrical disturbances
 23 you can uncomment methods to adjust the noise floor or the disturber
 24 watch dog threshold values.
 25
 26
 27 For this example you will need to connect the INT pin on Qwiic to
 28 GPIO D21 on the Raspberry Pi.  The interrupt pin will go high when an
 29 event occurs.
 30"""
 31import sys
 32from time import sleep
 33import board
 34import busio
 35import digitalio
 36import sparkfun_qwiicas3935
 37
 38# global variables with defaults
 39noise_floor = 2
 40watchdog_threshold = 2
 41
 42# Set up Interrupt pin on GPIO D21 with a pull-down resistor
 43as3935_interrupt_pin = digitalio.DigitalInOut(board.D21)
 44as3935_interrupt_pin.direction = digitalio.Direction.INPUT
 45as3935_interrupt_pin.pull = digitalio.Pull.DOWN
 46
 47# Create a library object using the Bus SPI port
 48spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
 49
 50# Set up chip select on D20
 51# CS can be any available GPIO pin
 52cs = digitalio.DigitalInOut(board.D20)
 53cs.direction = digitalio.Direction.OUTPUT
 54
 55lightning = sparkfun_qwiicas3935.Sparkfun_QwiicAS3935_SPI(spi, cs)
 56
 57# define functions
 58def reduce_noise(value):
 59    # This function helps to adjust the sensor to your environment. More
 60    # environmental noise leads to more false positives. If you see lots of noise
 61    # events, try increasing the noise threshold with this function. The datsheet
 62    # warns that smartphone and smart watch displays, DC-DC converters, and/or
 63    # anything that operates in 500 kHz range are noise sources to be avoided.
 64    # The manufacturer's default value is 2 with a maximum value of 7.
 65    value += 1
 66
 67    if value > 7:
 68        print("Noise floor is at the maximum value.")
 69        return 7
 70    print("Increasing the noise event threshold to ", value)
 71    lightning.noise_level = value
 72    return value
 73
 74
 75def increase_threshold(value):
 76    # This function is similar to the one above in that it will increase the
 77    # antenna's robustness against false positives. However, this function helps
 78    # to increase the robustness against "distrubers" and not "noise". If you
 79    # have a lot of disturbers trying increasing the watchdog threshold.
 80    # The default value is 2 and goes up to 10."""
 81
 82    value += 1
 83    if value > 10:
 84        print("Watchdog threshold is at its maximum value")
 85        return 10
 86    print("Increasing the disturber watchdog threshold to ", value)
 87    lightning.watchdog_threshold = value
 88    return value
 89
 90
 91# main code
 92
 93print("AS3935 Franklin Lightning Detector")
 94
 95# Check if connected
 96if lightning.connected:
 97    print("Schmow-ZoW, Lightning Detector Ready!")
 98else:
 99    print("Lightning Detector does not appear to be connected. Please check wiring.")
100    sys.exit()
101
102# get the current noise floor value
103noise_floor = lightning.noise_level
104print("The noise floor is ", noise_floor)
105
106# get the current disturber threshold
107watchdog_threshold = lightning.watchdog_threshold
108print("The disturber watchdog threshold is ", watchdog_threshold)
109
110print("Type Ctrl-C to exit program.")
111
112try:
113    while True:
114        # When the interrupt goes high
115        if as3935_interrupt_pin.value:
116            print("Interrupt:", end=" ")
117            interrupt_value = lightning.read_interrupt_register()
118
119            if interrupt_value == lightning.NOISE:
120                print("Noise.")
121                # uncomment line below to adjust the noise level (see function comments above)
122                # noise_floor = reduce_noise(noise_floor)
123            elif interrupt_value == lightning.DISTURBER:
124                print("Disturber.")
125                # uncomment the line below to adjust the watchdog threshold
126                # (see function comments above)
127                # watchdog_threshold = increase_threshold(watchdog_threshold)
128            elif interrupt_value == lightning.LIGHTNING:
129                print("Lightning strike detected!")
130                print("Approximately: " + str(lightning.distance_to_storm) + "km away!")
131                print("Energy value: " + str(lightning.lightning_energy))
132        sleep(1)
133
134except KeyboardInterrupt:
135    pass
  1. More Lightning Features (SPI) - Functions that prevent noise and false events.

examples/example2_more_lightning_features_spi.py
  1# SPDX-FileCopyrightText: Copyright (c) 2021 Gaston Williams
  2#
  3# SPDX-License-Identifier: MIT
  4
  5#  This is example is for the SparkFun Qwiic AS3935 Lightning Detector.
  6#  SparkFun sells these at its website: www.sparkfun.com
  7#  Do you like this library? Help support SparkFun. Buy a board!
  8#  https://www.sparkfun.com/products/15276
  9
 10"""
 11 Qwiic AS3935 Example 2 - example2_more_lightning_features.py
 12 Written by Gaston Williams, July 6th, 2019
 13 Based on Arduino code written by
 14 Elias Santistevan @ SparkFun Electronics, May, 2019
 15 The Qwiic AS3935 is an I2C (or SPI) controlled lightning detector.
 16
 17 Example 2 - More Lightning Features (SPI):
 18 This program uses the Qwiic AS3935 CircuitPython Library to
 19 control the Qwiic AS3935 Lightning detector over SPI to listen for
 20 lightning events.  This example provides a more in depth guide
 21 to some more of the library's functions that aid in reducing false
 22 events and noise.
 23
 24 For this example you will need to connect the INT pin on Qwiic to
 25 GPIO D21 on the Raspberry Pi.  The interrupt pin will go high when an
 26 event occurs.
 27"""
 28import sys
 29from time import sleep
 30import board
 31import busio
 32import digitalio
 33import sparkfun_qwiicas3935
 34
 35# Set up Interrupt pin on GPIO D6 with a pull-down resistor
 36as3935_interrupt_pin = digitalio.DigitalInOut(board.D21)
 37as3935_interrupt_pin.direction = digitalio.Direction.INPUT
 38as3935_interrupt_pin.pull = digitalio.Pull.DOWN
 39
 40# Create a library object using the Bus SPI port
 41spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
 42
 43# Set up chip select on D20
 44# CS can be any available GPIO pin
 45cs = digitalio.DigitalInOut(board.D20)
 46cs.direction = digitalio.Direction.OUTPUT
 47
 48lightning = sparkfun_qwiicas3935.Sparkfun_QwiicAS3935_SPI(spi, cs)
 49
 50print("AS3935 Franklin Lightning Detector")
 51
 52# Check if connected
 53if lightning.connected:
 54    print("Schmow-ZoW, Lightning Detector Ready!")
 55else:
 56    print("Lightning Detector does not appear to be connected. Please check wiring.")
 57    sys.exit()
 58
 59# You can reset all the lightning detector settings back to their default values
 60# by uncommenting the line below.
 61# lightning.reset()
 62
 63# The lightning detector defaults to an indoor setting (less gain/sensitivity),
 64# if you plan on using this outdoors uncomment the following line:
 65lightning.indoor_outdoor = lightning.OUTDOOR
 66
 67# Read the Lightning Detector Analog Front End (AFE) mode and print it out.
 68afe_mode = lightning.indoor_outdoor
 69
 70if afe_mode == lightning.OUTDOOR:
 71    print("The Lightning Detector is in the Outdoor mode.")
 72elif afe_mode == lightning.INDOOR:
 73    print("The Lightning Detector is in the Indoor mode.")
 74else:
 75    print("The Lightning Detector is in an Unknown mode.")
 76
 77# Disturbers are events that are false lightning events. If you see a lot
 78# of disturbers you can enable the disturber mask to have the chip not report
 79# those events as an interrupt.
 80# Uncomment one of the lines below to turn on disturber mask on or off
 81# lightning.mask_disturber = True
 82# lightning.mask_disturber = False
 83print("Are disturbers being masked?", end=" ")
 84if lightning.mask_disturber:
 85    print("Yes.")
 86else:
 87    print("No.")
 88
 89
 90# The Noise floor setting from 1-7, one being the lowest.
 91# The default setting is two.
 92# If you need to change the setting, uncomment the line below
 93# lightning.noise_level = 2
 94print("Noise level is set at: " + str(lightning.noise_level))
 95
 96# Watchdog threshold setting can be from 1-10, one being the lowest.
 97# The default setting is two.
 98# If you need to change the setting, uncomment the line below
 99# lightning.watchdog_threshold = 2
100print("Watchdog Threshold is set to: " + str(lightning.watchdog_threshold))
101
102# Spike Rejection setting from 1-11, one being the lowest.
103# The default setting is two. The shape of the spike is analyzed
104# during the chip's validation routine. You can round this spike
105# at the cost of sensitivity to distant events.
106# If you need to change the setting, uncomment the line below.
107# lightning.spike_rejection = 2
108print("Spike Rejection is set to: " + str(lightning.spike_rejection))
109
110
111# This setting will change when the lightning detector issues an interrupt.
112# For example you will only get an interrupt after five lightning strikes
113# instead of one. The default is one, and it takes settings of 1, 5, 9 and 16.
114# If you want to change this value, uncomment the line below.
115# lightning.lightning_threshold = 1
116print(
117    "The number of strikes before interrupt is triggered: "
118    + str(lightning.lightning_threshold)
119)
120
121# When the distance to the storm is estimated, it takes into account other
122# lightning that was sensed in the past 15 minutes.
123# If you want to reset the time, you can uncomment the line below.
124# lightning.clear_statistics()
125
126# You can power down your lightning detector. You absolutely must call
127# the wakup funciton afterwards, because it recalibrates the internal
128# oscillators.
129# Uncomment the statements below to test power down and wake up functions
130
131# lightning.power_down()
132# print('AS3935 powered down.')
133# calibrated = lightning.wake_up()
134#
135# if calibrated:
136#    print('Successfully woken up!')
137# else:
138#    print('Error recalibrating internal osciallator on wake up.')
139
140
141print("Type Ctrl-C to exit program.")
142
143try:
144    while True:
145        # When the interrupt goes high
146        if as3935_interrupt_pin.value:
147            print("Interrupt:", end=" ")
148            interrupt_value = lightning.read_interrupt_register()
149
150            if interrupt_value == lightning.NOISE:
151                print("Noise.")
152            elif interrupt_value == lightning.DISTURBER:
153                print("Disturber.")
154            elif interrupt_value == lightning.LIGHTNING:
155                print("Lightning strike detected!")
156                # Distance estimation takes into account previous events.
157                print("Approximately: " + str(lightning.distance_to_storm) + "km away!")
158                # Energy is a pure number with no physical meaning.
159                print("Energy: " + str(lightning.lightning_energy))
160        sleep(1)
161
162except KeyboardInterrupt:
163    pass
  1. Tune Antenna (SPI) - Tune the resonance frequency of the antenna.

examples/example3_tune_antenna_spi.py
 1# SPDX-FileCopyrightText: Copyright (c) 2021 Gaston Williams
 2#
 3# SPDX-License-Identifier: MIT
 4
 5#  This is example is for the SparkFun Qwiic AS3935 Lightning Detector.
 6#  SparkFun sells these at its website: www.sparkfun.com
 7#  Do you like this library? Help support SparkFun. Buy a board!
 8#  https://www.sparkfun.com/products/15276
 9
10"""
11 Qwiic AS3935 Example 3 - example3_tune_antenna.py
12 Written by Gaston Williams, July 6th, 2019
13 Based on Arduino code written by
14 Elias Santistevan @ SparkFun Electronics, May, 2019
15 The Qwiic AS3935 is an I2C (or SPI) controlled lightning detector.
16
17 Example 3 - Tune Antenna (SPI):
18 This program uses the Qwiic AS3935 CircuitPython Library to control
19 the Qwiic AS3935 Lightning detector over I2C to tune the resonance
20 frequency of the antenna. The chip provides internal capacitance that
21 can be modified by the  library. You'll need a logic analyzer,
22 oscillscope, or some method of reading a 32kHz square wave (depending
23 on the frequency divsior used) on the INT pin.
24"""
25import sys
26import board
27import busio
28import digitalio
29import sparkfun_qwiicas3935
30
31# Create a library object using the Bus SPI port
32spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
33
34# Set up chip select on D20
35# CS can be any available GPIO pin
36cs = digitalio.DigitalInOut(board.D20)
37cs.direction = digitalio.Direction.OUTPUT
38
39lightning = sparkfun_qwiicas3935.Sparkfun_QwiicAS3935_SPI(spi, cs)
40
41print("AS3935 Franklin Lightning Detector")
42
43# Check if connected
44if lightning.connected:
45    print("Ready to tune antenna.")
46else:
47    print("Lightning Detector does not appear to be connected. Please check wiring.")
48    sys.exit()
49
50# You can reset all the lightning detector settings back to their default values
51# by uncommenting the line below.
52# lightning.reset()
53
54# When reading the frequency, keep in mind that the given frequency is
55# divided by 16 by default. This can be changed to be divided by 32, 64, or
56# 128 using the line below. So for example when reading the frequency on a
57# board, if the frequency is 32.05kHz, multipling by 16 gives the result of
58# 512.8kHz. This is 2.56 percent away from the 500kHz ideal value and well
59# within the 3.5 percent optimal tolerance. If one were to change the
60# division ratio to 32, then one would read a value of 16kHz instead.
61# To change the division ratio uncomment the line below
62# lightning.division_ratio = 16
63
64# The following code is just a sanity check. It will return a value of
65# 16 by default but can be 32, 64, or 128, depending on what value you set.
66print("Division Ratio is set to: ", str(lightning.division_ratio))
67
68# Here you can set a value of 0-120, which increases the capacitance on
69# the RLC circuit in steps of 8pF, up to 120pF. The change in frequency is
70# very modest. At 15 (max - 120pF), the frequency is around 490kHz down from
71# 512kHz. The equation for calculating frequency in an RLC circuit is:
72# f = 1/(2pi*sqrt(LC))
73# To change the capacitance, uncomment the line below.
74# lightning.tune_cap = 0
75
76# When reading the internal capcitor value, it will return the value in pF.
77print("Internal Capacitor is set to: " + str(lightning.tune_cap))
78
79# This will tell the AS3935 to display the resonance frequncy as a digital
80# signal on the interrupt pin. There are two other internal oscillators
81# that within the AS3935 that can also be displayed on this line. See the
82# datasheet for more information.
83# Uncomment the lines below to turn the display on and off. To start
84# displaying the antenna frequency, use True as the state parameter. To
85# stop displaying the frequncy on the interrupt line, use False.
86
87# print('----Displaying oscillator on INT pin.----')
88# lightning.display_oscillator(True, lightning.ANTENNA_FREQ)
89# print('----Stop Display of oscillator on INT pin.----')
90# lightning.display_oscillator(False, lightning.ANTENNA_FREQ)