GPIO on Raspberry Pi

Post your Gambas programming questions here.
phb
Posts: 4
Joined: Monday 27th July 2020 1:18pm

GPIO on Raspberry Pi

Post by phb »

Hello everybody - my name is Peter - I´m not new to Basic but to Gambas! So one could say I´m a Gambas newbie. I have a Raspberry Pi 3 B and Gambas 3.12.2. Years ago I wrote a program in BASCOM for my selfmade Geigercounter to measure natural background radiation. Now I´m trying to convert this program to the raspberry pi using Gambas. It´way harder than I thought :( :( This is what I "achieved" so far:
Public Sub Form_Open()

'determine GPIO 23 = Pin 16 as Input / pull-up Resistor to +Ub
Shell "gpio -g mode 23 in up"
' enable Timer1 for polling
Timer1.Enabled = True
Timer1.Delay = 100
Shell "gpio -g read 23" 'read the content of pin 16
' If...
End
I´m far from sure if this is even going - I would need to know the state of pin 16 where the geigercounter is connected to - a negative edge means a count - the counts have to be added for one minute to get the cpm (counts per minute) - then the values could be displayed and/or written to a logfile. My Bascom program used Interruptprogramming for this purpose - I don´t know if this is possible with Gambas.
Now my question is how do I transfer the value of pin 16 to variable? Is there a better method for polling an input pin in Gambas?
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: GPIO on Raspberry Pi

Post by cogier »

Hi phb and welcome to the forum.

If you double-click on the Timer you put on the form Gambas will take you straight to the timer routine I think you need. The routine will be called every 10th of a second (Timer1.Delay = 100).

If you define a variable before any subroutines it will be global. You can then use the shell command to send the result to your variable: -
sResult As String

Public Sub Form_Open()

   Timer1.Delay = 100
   Timer1.Start    ' Same as Timer1.Enabled = True

End

Public Sub Timer1_Timer()

  Shell "gpio -g mode 23 in up " To sResult   

End
I would need to see more of your old code to work out exactly what you are after but I hope this is a start.

I have moved these posts to the General Forum
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: GPIO on Raspberry Pi

Post by stevedee »

phb wrote: Monday 27th July 2020 1:39pm ...I wrote a program in BASCOM for my selfmade Geigercounter to measure natural background radiation. Now I´m trying to convert this program to the raspberry pi using Gambas...

Hi Peter, I'm wondering what the output from your Geiger counter looks like. I assume its a series of random pulses. If that is the case, what's the minimum time between pulses?

Your timer is set for 100ms, which I would have thought is too slow to detect more than 1 pulse in 100ms.

Using interrupts on a Raspberry Pi is probably not the answer either (see my old blog post: http://captainbodgit.blogspot.com/2014/ ... or-to.html).

I would have thought you needed some binary counter logic in your Geiger counter, and then maybe read 4 bits at a time (using 4 digital i/o lines on the Pi) and then clear the counter ready for the next count.

However, I'm probably way off the mark in my understanding of what you are trying to do.
phb
Posts: 4
Joined: Monday 27th July 2020 1:18pm

Re: GPIO on Raspberry Pi

Post by phb »

@cogier
I would need to see more of your old code to work out exactly what you are after but I hope this is a start.
Hello cogier - thanks for the warm welcome!! Here is the desired code and many thanks for your prompt answer - that´s what I needed!!!!!!
P.S: sResult - shouldn´t it be Integer? That´s what represent the counts and they have to be added...but when I try to make sResult Integer I get an error message :(
$regfile "ATtiny13.DAT"
$crystal = 96000000
$hwstack = 32
$swstack = 8
$framesize = 16
'---------------------------------------------------------------------------
Dim Counts As Integer 'Declaration
Counts = 0 'of
Dim Sekunden As Integer 'the variables
Sekunden = 0 'all values to 0
Const Timervalue = 40 'equals 1 second at 9,6 MHz
'---------------------------------------------------------------------------
Led Alias Portb.0 ' Led connected to PORTB.0
Led = 0 ' for making the counts visible
Config Led = Output ' not needed for the actual program
'---------------------------------------------------------------------------
Config Portb.1 = Input 'PortB.1 (INT0) - where signal from geigercounter arrives
'---------------------------------------------------------------------------
On Int0 On_int0 'declaring Label "On_int0"
Config Int0 = Falling 'Interrupt at falling edge
Enable Int0
Enable Interrupts
'---------------------------------------------------------------------------
Config Timer0 = Timer , Prescale = 1024
Enable Timer0
Timer0 = Timervalue ' Timer0 is charged with "Timervalue" = 1 second
On Timer0 Timer_interrupt
'---------------------------------------------------------------------------
Open "comb.3:9600,8,n,1,INVERTED" For Output As #1
' activate Software-UART on Attiny13 - TX is PORTB.3
'---------------------------------------------------------------------------
Do

!nop 'mainprogram - no Action!

Loop

End
'---------------------------------------------------------------------------
On_int0: 'on interrupt jump to this label

Incr Counts ' Increment Counts on each Interrupt

Led = 1 ' LED is blinking on each Count
Waitms 10
Led = 0

Return 'back to the main program when done
'---------------------------------------------------------------------------
Timer_interrupt: 'jump to this label once in a second
'increment by 1 each second
Incr Sekunden

If Sekunden = 60 Then 'after 60 seconds counting of events is done
Then
Sekunden = 0
Print #1 , Counts 'send value to the serial as counts per minute (cpm)
Counts = 0
End If

Return
Last edited by phb on Monday 27th July 2020 8:49pm, edited 7 times in total.
phb
Posts: 4
Joined: Monday 27th July 2020 1:18pm

Re: GPIO on Raspberry Pi

Post by phb »

@stevedee
Thanks for your answer!! The pulses are needle like - falling edge type - from +5V to 0V. They are not exact standard but every controller could detect them easily so far. Background radiation is in average 37 pulses per minute in my case so that 100 ms is far enough for me - if 100 ms becomes too little to detect each pulse maybe than I don´t need a geigercounter anymore (atomic bomb, china syndrome, gamma ray burst :lol: :lol: :lol: )
You must know that the geigercounter is about to become a part of my private weatherstation. It was connected to a pc of mine before and ran only when th pc was turned on - now I intend to run the counter permanently on my Pi.
I had a program written in liberty basic that wrote the values to a logfile - I have quite a lot of them now. But that program is no longer working for unknown reasons - it´s a windows program of course :( :( :(
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: GPIO on Raspberry Pi

Post by cogier »

It looks as if the German Gambas Forum has given you most of what you need. I was going to point out various options but I think it's all covered now, but please feel free to ask more here any time.
phb
Posts: 4
Joined: Monday 27th July 2020 1:18pm

Re: GPIO on Raspberry Pi

Post by phb »

This is what I have so far....Iḿ trying to print the values to a label to make it visible in a window. The problem is that I get an error message - in "Public Function Label1_ShowCounts() As Integer". Itś only a small step to completing the program and then the spoonfeeding is over!!!
Public Function GPIO_Count(iPin As Integer, iTime As Integer) As Integer


Let iPin = 23

Let iTime = 60000

Dim iCounter As Integer
Dim sStatus As String
Dim tStart As Float = Timer
Dim tStop As Float

Shell "gpio -g mode " & iPin & " in"
Shell "gpio -g mode " & iPin & " up"

Repeat
Shell "gpio -g read " & iPin To sStatus
Wait
If Val(sStatus) = 0 Then Inc iCounter
tStop = Timer
Until (tStop - tStart) > iTime

Shell "gpio -g mode " & iPin & " down"

Return iCounter



Public Function Label1_ShowCounts() As Integer

Label1.text = GPIO_Count(23, 60000)

End
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: GPIO on Raspberry Pi

Post by PJBlack »

... also mir mißfällt die formulierung: "was ich bis jetzt habe" ...

but anyhow ... take a form put a (text)label and a (command)button on it ...

in the class file:
public sub button1_click()
    Label1.text = GPIO_Count(23, 60000)
end
something like that ...

p.s.: ich empfehle dir dringend einen blick in https://gambas-buch.de/ ;)
ocoquet
Posts: 36
Joined: Friday 18th December 2020 7:56am

Re: GPIO on Raspberry Pi

Post by ocoquet »

Hi,

Just a question, why you don't use Wiring Pi
GPIO Interface library for the Raspberry Pi

It's more suitable for your application.

Regards from France
Olivier
Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
User avatar
Matthew-Collins
Posts: 12
Joined: Wednesday 21st September 2016 7:59pm
Location: Guernsey, Channel Islands

Re: GPIO on Raspberry Pi

Post by Matthew-Collins »

Cheers
Matt
Post Reply