Last active
March 28, 2021 14:23
-
-
Save kibotu/c187a8e91dcb6582b4a43a760f60d958 to your computer and use it in GitHub Desktop.
Geiger counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import datetime | |
import RPi.GPIO as GPIO | |
from collections import deque | |
counts = deque() | |
usvh_ratio = 0.00812037037037 # This is for the J305 tube | |
geigerChannel = 7 | |
# https://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/ | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setwarnings(True) | |
# This method fires on edge detection (the pulse from the counter board) | |
def countme(channel): | |
global counts | |
timestamp = datetime.datetime.now() | |
counts.append(timestamp) | |
# Set the input with falling edge detection for geiger counter pulses | |
GPIO.setup(geigerChannel, GPIO.IN) | |
# https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/ | |
GPIO.add_event_detect(geigerChannel, GPIO.FALLING, callback=countme) | |
loop_count = 0 | |
# In order to calculate CPM we need to store a rolling count of events in the last 60 seconds | |
# This loop runs every second to update measurements and removes elements from the queue | |
# that are older than 60 seconds | |
while True: | |
loop_count = loop_count + 1 | |
try: | |
while counts[0] < datetime.datetime.now() - datetime.timedelta(seconds=60): | |
counts.popleft() | |
except IndexError: | |
pass # there are no records in the queue. | |
if loop_count == 10: | |
# Every 10th iteration (10 seconds), store a measurement in Influx | |
measurements = [ | |
{ | |
'measurement': 'sensor', | |
'fields': { | |
'cpm': int(len(counts)), | |
'usvh': "{:.2f}".format(len(counts) * usvh_ratio) | |
} | |
} | |
] | |
print(measurements) | |
loop_count = 0 | |
time.sleep(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip3 install spidev RPi.GPIO | |
python3 geigercounter.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment