Informatics

radiation detection with Raspberry Pi

The Raspberry Pi is a nice piece of hardware.

Moreover, it can be expanded with an “Arduino to Raspberry Pi connection bridge”, which allows it to interact with other boards made for Arduino. For example, sensor boards.

In this case I’ve been playing with a sensor board for radiation detection.

It is as cool as it seems :)

Once all is conected (very easy), the first question is: why the LCD and the leds are not showing anything?

Well, they must be commanded from somewhere, and since there’s no Arduino for doing the job… the RPi must do it!

The site cookinghacks has some code, but it is for Arduino boards, again… So one has to adapt things in different ways.

First of all, from arduPi, we know the conversion from Arduino to RPi GPIO pins:

int raspberryPinNumber(int arduinoPin){
switch(arduinoPin){
case 2: return 18; break;
case 3: return 23; break;
case 4: return 24; break;
case 5: return 25; break;
case 6: return  4; break;
case 7: return 17; break;
case 8: if(REV == 1){return 21;}else{return 27;} break;
case 9: return 22; break;
case 10: return 8; break;
case 11: return 10; break;
case 12: return 9; break;
case 13: return 11; break;
}
}

So, we could write things from scratch if that would be needed… hopefully, that won’t be the case: arduPi has most functions translated, so we should use them.

For example, the code to read the CPM from the radiation board compiles fine. Well, after the lines for arduPi classes initialization are commented (it’s done in last version of arduPi in the cpp itself):

//SerialPi Serial;
//WirePi Wire;
//SPIPi SPI;

And the loop() subroutine is retouch not to initiate too soon: this is extracted from another example of the same site that I’ll use later. At least in my RPi, the original code easily leads to a wrong initialization of the gpio, as a pin is tried to be read before the interruption is attached…

/sys/class/gpio/gpio18/value: No such file or directory

This is avoided with:

void loop(){
if (millis()-timePreviousMeassure > 10000){
detachInterrupt(2);

attachInterrupt(2,countPulse,FALLING);

delay (10000);
}
}

This way, the code of the loop isn’t run until after 10 s have elapsed from the first run…

Of course, remember to run the code always as root.

The code for CPM values printing on terminal is here: geiger_tube.tgz

Also as git repository here.

Compile it as usual:

$ g++ -lrt -lpthread geiger_tube.c arduPi.cpp -o geiger_tube

And… let’s run it: it’ll output values every ten seconds:

$ sudo ./geiger_tube

CPM=6
0.048720 uSv/h
CPM=12
0.097440 uSv/h
CPM=30
0.243600 uSv/h
CPM=12
0.097440 uSv/h

This step achieved, the next step is to control LEDs and LCD screen.

The problem with the code on the site is that it is not adapted at all for RPi: the first thing is to change the attachInterrupt() and detachInterrupt() to use the “2” value, instead of “0”.

Now, it uses a LiquidCrystal.h that is distributed with arduino-core, so let’s install it on the RPi:

$ sudo apt-get install arduino-core

78.5 MB of additional disk space will be used (!).

A quick examination of this “/usr/share/arduino/libraries/LiquidCrystal/LiquidCrystal.h” and its “#include” seems to allow a quick (but nonetheless, painful) port…

I couldn’t use an I2C approach, as the LCD that comes with this board does not support it: it’s a LMB162ABC. This known, one could read the LCD manual, and using some code as base, port it to C… but the approach wouldn’t be as rich as the already available Arduino library.

So, the arduino code must be deArduinazed from spurious Arduino dependences and RPized to use arduPi library.

And… yes here it is: geiger_lcdnleds.tgz

Also as git repository here.

Just compile it with:

$ g++ -lrt -lpthread geiger_lcdnleds.c LiquidCrystal.cpp Print.cpp arduPi.cpp -o geiger_lcdnleds

And run it!, the LCD will show your CPM and equivalent uSv/h values:

$ sudo ./geiger_lcdnleds

And hopefully… none of the LEDs will light… the thresholds are set too high for a common background radiation environment, unless you’re using Uranium glass samples or something like that  :-o

Just change them to see that they’re really there: 3 green and 2 red. For example to:

// Funny! threshold values for the led bar
#define TH1 5
#define TH2 10
#define TH3 15
#define TH4 20
#define TH5 40

Which are the values used to take this photo :)

RPi with arduino radiation sensor board


PS: you may also be interested in this code for cumulative counters with Arduino.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s