Arduino code for Geiger counter. Uploads CPM values to every minute

1 year 9 months ago - 1 year 9 months ago #6149 by rootsolutions
Hello Friends!
I Share this:
Arduino code for Geiger counter. Uploads CPM values to every minute
Download from

Thank you.
Best regards!
from Argentina

* Geiger counter Kit could get on: search: geiger counter kit
* CPM (or counts per minute) is events quantity from Geiger Tube you get during one minute. Usually it used to
* calculate a radiation level. Different GM Tubes has different quantity of CPM for background. Some tubes can produce
* about 10-50 CPM for normal background, other GM Tube models produce 50-100 CPM or 0-5 CPM for same radiation level.
* Please refer your GM Tube datasheet for more information. Just for reference here, J305 and SBM-20 can generate
* about 10-50 CPM for normal background.
* The kit 3 wires that should be connected to Arduino UNO board: 5V, GND and INT. PullUp resistor is included on
* kit PCB. Connect INT wire to Digital Pin#2 (INT0), 5V to 5V, GND to GND. Then connect the Arduino with
* USB cable to the computer and upload this sketch.
* Author:JiangJie Zhang * If you have any questions, please connect This email address is being protected from spambots. You need JavaScript enabled to view it.
* License: MIT License
* Please use freely with attribution. Thank you!

// to upload data to use the following http get:

// 1.14µSv.h - Shelter population
// 5.7µSv.h - Evacuation of population
// 11.4µSv.h - Issue Iodine tablets
// 0.114µSv.h - Max daily dose == 1mSv.year

// wiring:
// Arduino pin 2 - geiger counter input (VIN on geiger board)
// Arduino pin 3 - reset PoE ethernet board - use jumper lead
// Arduino pin 4 - Activity LED, active high

#include <SPI.h>
#include <Ethernet.h>

byte mac = { 0x2E, 0x3D, 0x4E, 0x5F, 0x6E, 0x7D };
char server = ""; // name address for server (using DNS)

EthernetClient client;

#define LOG_PERIOD 60000 //Logging period in milliseconds, recommended value 15000-60000.
#define MAX_PERIOD 60000 //Maximum logging period without modifying this sketch
#define CONV_FACTOR 0.00812037 //See:
#define SS 10 //W5500 CS
#define RST 3 //W5500 RST - RJ add a jumper to change the reset of the board!
#define SLED 4 // system status LED

unsigned long volatile counts; //variable for GM Tube events
unsigned long cpm; //variable for CPM
unsigned int multiplier; //variable for calculation CPM in this sketch
unsigned long previousMillis; //variable for time measurement
float usvh; // variable for uSv per hour calculated figure
unsigned long ccount; // counter for the characters returned from the web service
byte okflag; // state machine to check for "OK" response from in body not header

void tube_impulse(){ //subprocedure for capturing events from Geiger Kit

void setup(){ //setup subprocedure
delay(5000); // delay boot
counts = 0;
cpm = 0;
multiplier = MAX_PERIOD / LOG_PERIOD; //calculating multiplier, depend on your log period
//while(!Serial){} // needed for Leonardo board
attachInterrupt(digitalPinToInterrupt(2), tube_impulse, FALLING); //define external interrupts
pinMode(SLED, OUTPUT); // the overall status LED for the outside of the case - reflects a sucessful upload
digitalWrite(SLED, HIGH); // turn LED on like a power led (it will blink off)

// bring up ethernet:
pinMode(SS, OUTPUT);
pinMode(RST, OUTPUT);
digitalWrite(SS, HIGH);
digitalWrite(RST,LOW); //Reset this module
digitalWrite(SS, LOW);
// end of copied sequence - no idea if this is needed with the modern Ethernet driver!

Ethernet.init(10); // Most Arduino shields use pin 10

Serial.println("Initialize Ethernet...");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP."); }

//Ethernet.maintain(); // renew lease

if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found."); }

if (Ethernet.hardwareStatus() == EthernetW5500) {
Serial.println("W5500 Ethernet controller detected."); }

if (Ethernet.linkStatus() == LinkON) {
Serial.println("Ethernet cable is connected."); }

// print your local IP address:
Serial.print("IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
if (thisByte < 3) { Serial.print("."); }

void loop(){ //main cycle
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > LOG_PERIOD){
previousMillis = currentMillis;
cpm = counts * multiplier;
counts = 0; // reset counter just as timing for next cycle has already started!
if (cpm > 50000) { cpm = 0; } // trap to stop runaway readings getting uploaded.
usvh = cpm * CONV_FACTOR;
Serial.print("CPM: ");
Serial.print("uSv/h: ");
Serial.print('\n'); // Carriage Return

// convert cpm into a string ready to transmit
String v = String(cpm);

// send the reading to the web service

if (client.connect(server, 80)) {

client.print("GET /radmon.php?function=submit&user=YOUR_USERNAME&password=YOUR_PASSWORD&value=");

client.println("&unit=CPM HTTP/1.1");
client.println("User-Agent: testing-my-arduino-ethernet-geiger-counter-board");
client.println("Connection: close");
// wait for a response and print it to serial
ccount = 0; // character count
okflag = 0; // reset OK detect state machine state
while (client.available()) {
char c =;

if (ccount > 15 && okflag == 0 && c == 'O') { okflag = 1; } else {
if (ccount > 15 && okflag == 1) {
if (c == 'K') { okflag = 2; } else { okflag = 0; }}}
if (okflag == 2) {
digitalWrite(SLED, LOW); // turn LED off for 1 second to show everything is working!
digitalWrite(SLED, HIGH); // turn LED back on
} else {
Serial.println("Not able to connect. Will try again next cycle!");


Last edit: 1 year 9 months ago by rootsolutions.
The following user(s) said Thank You: mw0uzo

Please Log in or Create an account to join the conversation.

1 year 8 months ago #6150 by diegok
Hola, como estas, una pregunta esta todo bien en tu contador geiger? porque tenes valores muy altos de CPM, yo estoy cerca de Montevideo y tengo valores de background normales

Please Log in or Create an account to join the conversation.

1 year 8 months ago #6151 by rootsolutions
mirate este video que armamos con cevicas, en mar del plata tambien hay picos, a ver si la gente se anima a comprar una placa geiger y aunque sea la instalan en windows, asi tenemos mediciones en todo el pais

Si viste!
es la zona y las cosas que estan pasando,
lo tengo dentro de mi casa al lado de una ventana.

Please Log in or Create an account to join the conversation.

1 year 8 months ago #6152 by manuelgomez1
Bienvenidos, nuevos usuarios del cono sur!
Los picos en su contador son casi seguro un problema en la construcción del contador, quizás en la calibración al momento de censar los pulsos con otra placa y enviarlos a Radmon (me ocurrió ese mismo problema al momento de construir el mio, ya que el elevado ruido y voltaje confundían al ESP y contaba 200 de fondo cuando el conteo real era de 20). En especial si el tubo es un SBM-20 o similar; 8500 pulsos en un tubo de ese tipo son casi 50 microsievert (fuera del reactor de Chernobyl hay 15 µSv).
Al contrario de lo que mencionas en el video, si existen monitores ambientales de este tipo en Argentina, somos un país nuclear, es normativo hacerlo. La información de los mismos no se publica, pero un pico de 50 µSv haría saltar muchas alarmas...
Ademas, a la distancia que estas del tren como una fuente teórica de la contaminación, es imposible que te llegue semejante cantidad (y el maquinista habría muerto).

Please Log in or Create an account to join the conversation.

1 year 8 months ago #6154 by diegok
Probaste de irte con el geiger a otra zona? porque podria haber algo en tu casa o cerca, me parece raro, yo lleve una vez el geiger a caba y no habia nada fuera de lo normal,

Please Log in or Create an account to join the conversation.

1 year 8 months ago #6158 by rjelbert
Hi - that's the code I wrote :-) Glad it is useful. Best to check back on github every so often because I'm likely to make minor changes. It's not the neatest code but works. See:

Please Log in or Create an account to join the conversation.

Moderators: Gamma-Man
Time to create page: 0.197 seconds
Powered by Kunena Forum
Everything's free. Please support us by considering a donation. Log in first!
Solar powered Raspberry Pi 4 server stats: CPU 30% Memory 13% Swap 11% CPU temp=61.8'C Uptime 10 Days