Geiger Counter

Got my Geiger up and running.
Made the kit from the Banggood offering

although you can buy it ready made.
Connected the output (oddly marked as VIN) an interrupt (IO 2) on an Arduino Nano. Also connected a DHT22 Temperature and Humidity sensor (IO 4). The Sketch I wrote is below (it contains a little redundant code from an earlier iteration!)…

/*
 * Geiger Counte and DHT22 Sketch
 * Written for the Nano
 */

#include "DHT.h"
#define DHTPIN 4
#define INTPIN 2
#define DHTTYPE DHT22

unsigned long int count, prevCount15, prevCount60;
int count15 = 0, count60 = 0, is4 = 0, cpm = 0;
unsigned long int prevMillis = 0, currMillis = 0, ledMillis = 0;
const int interval15 = 15000;
const int blinkMillis = 20;
float microSievertsPerHour, minsSinceStartup, averageMSPH, microSieverts60;

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }
  dht.begin();
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  attachInterrupt(digitalPinToInterrupt(INTPIN), countPulse, FALLING);

}

void loop() {
  // put your main code here, to run repeatedly:
  if (ledMillis > 0 and (millis() - ledMillis) >= blinkMillis){ // Turn LED off after a count
    ledMillis = 0; 
    digitalWrite(LED_BUILTIN, LOW);
  }
  if (millis() - prevMillis >= interval15){ // Do this part every 15 seconds
    count15 = count - prevCount15;
    prevCount15 = count;
    cpm = count15 * 4;
    microSievertsPerHour = cpm / 151.0; // 151 is a conversion factor I found for the M4011 tube
    minsSinceStartup = millis() /60000.0;
    averageMSPH = count / (minsSinceStartup * 151.0);
    prevMillis += interval15; 
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    Serial.print(count); Serial.print(", ");
    Serial.print(count15); Serial.print(", ");
    Serial.print(cpm); Serial.print(", ");
    Serial.print(microSievertsPerHour); Serial.print(", ");
    Serial.print(t); Serial.print(", ");
    Serial.print(h);
    Serial.println();
  }
}

void countPulse(){ // Pulse LED
  digitalWrite(LED_BUILTIN, HIGH);
  ledMillis = millis();
  count++;
}

I power the whole thing from a USB port in my NIC running HA and the data in the form of a CSV string comes from the Nano directly into HA every 15 seconds. No need for WiFi and MQTT from an ESP8266 which was my original plan).
The Serial CSV string is read by HA into the entity ‘nano’ with the following lines in the config file

sensor:
  - platform: serial
    name: nano
    serial_port: /dev/serial/by-id/usb-1a86_USB_Serial-if00-port0
    baudrate: 115200

The serial port would need to be changed for your installation. I used the ‘by-id’ method (as opposed to ‘ttyUSB1’ as the 1 sometimes changed, whereas the ‘by-id’ reference never changes. To find your ref go into Settings/System/Hardware/the … menu top right/All Hardware, then scroll down to find it.
The individual vales are then extracted from the CSV string by the following in the config file…

template:
  sensor:
    - name: geiger_count_total_from_startup
      state: "{{ states('sensor.nano').split(',')[0] | float(default=0) }}"
      icon: mdi:radioactive
    - name: geiger_count_last_15_seconds
      state: "{{ states('sensor.nano').split(',')[1] | float(default=0) }}"
      icon: mdi:radioactive
      unit_of_measurement: "Cp15s"
    - name: geiger_cpm
      state: "{{ states('sensor.nano').split(',')[2] | float(default=0) }}"
      icon: mdi:radioactive
      unit_of_measurement: "CPM"
    - name: geiger_uSvph
      state: "{{ states('sensor.nano').split(',')[3] | float(default=0) }}"
      icon: mdi:radioactive
      unit_of_measurement: "µSv/h"
    - name: geiger_temperature
      state: "{{ states('sensor.nano').split(',')[4] | float(default=0) }}"
      icon: mdi:thermometer
      unit_of_measurement: "°C"
    - name: geiger_humidity
      state: "{{ states('sensor.nano').split(',')[5] | float(default=0) }}"
      icon: mdi:water-percent
      unit_of_measurement: "%"

I do a little bit of processing with automations to get some other values which are then displayed on a Lovelace tab. As you can see, it’s only been running for a few days.


And here’s a picture of the finished unit with the Nano mounted in a holder I got from The Pi shop. I had to extend the nylon legs to fit it in.

If this helps anybody out, let me know in the comments.

4 Likes

Wow cool! Hope for esphome support soon…

Here is a post about that Geiger counter with ESPHome: