GPIO on Raspberry Pi

Post

Posted
Rating:
#1 (In Topic #440)
phb
Trainee
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?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
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: -

Code (gambas)

  1. sResult As String
  2.  
  3. Public Sub Form_Open()
  4.  
  5.    Timer1.Delay = 100
  6.    Timer1.Start    ' Same as Timer1.Enabled = True
  7.  
  8.  
  9. Public Sub Timer1_Timer()
  10.  
  11.   Shell "gpio -g mode 23 in up " To sResult  
  12.  

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
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
stevedee is in the usergroup ‘Regular’

phb said

…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: Captain Bodgit: RaspberryPi inputs: to poll or to interrupt?).

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.
Online now: No Back to the top

Post

Posted
Rating:
#4
phb
Trainee
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
Online now: No Back to the top

Post

Posted
Rating:
#5
phb
Trainee
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  :(  :(  :(
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.
Online now: No Back to the top

Post

Posted
Rating:
#7
phb
Trainee
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!!!

Code

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

Code

Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
… 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:

Code (gambas)

  1. public sub button1_click()
  2.     Label1.text = GPIO_Count(23, 60000)
  3.  

something like that …

p.s.: ich empfehle dir dringend einen blick in https://gambas-buch.de/ ;)
Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
ocoquet is in the usergroup ‘Regular’
 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
Online now: No Back to the top

Post

Posted
Rating:
#10
Trainee

 Cheers
Matt
Online now: No Back to the top

Post

Posted
Rating:
#11
Regular
ocoquet is in the usergroup ‘Regular’
Sorry, don't understand what is this  :?:

Regards

Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
Online now: No Back to the top

Post

Posted
Rating:
#12
Trainee
Hi,

This is a Gambas class, so create a new class file in your project called "GPIO_Pin" and copy and paste the code into it…
https://raw.githubuser…aster/.src/GPIO_Pin.class

Then in your code:

Code (gambas)

  1. Dim Pin As New GPIO_Pin(16)
  2. Pin.SetDirection(False)
  3.  
  4. For i as Integer = 0 to 1000
  5.     Print str(i) & "-" & str(Pin.GetValue())
  6.  
  7. Print "Done"
  8.  


Let me know how you get on / if this is helpful ?

Happy Christmas

 Cheers
Matt
Online now: No Back to the top

Post

Posted
Rating:
#13
Regular
ocoquet is in the usergroup ‘Regular’
Thank's but…..

Just, I've no question  :P  :lol:
I was just answering to the forum topic, personally i use wiringpi with a real success  :D

Regards and Happy Christmas
Olivier

Olivier Coquet
Gambas Dev
Le Forum développeur Gambas
Online now: No Back to the top

Post

Posted
Rating:
#14
Trainee
Hi, can you post a WiringPi example/demo project ?

 Cheers
Matt
Online now: No Back to the top

Post

Posted
Rating:
#15
Avatar
Regular
stevedee is in the usergroup ‘Regular’

Matthew-Collins said

…can you post a WiringPi example…

RPi GPIO Code Samples - eLinux.org

Note: 1) I don't think you need root for latest wiringPi,
2) Gordon got the 'ump with abuse, so stopped updating wiringPi some time ago: Captain Bodgit: Never look a gift horse in the mouth
Online now: No Back to the top

Post

Posted
Rating:
#16
Trainee
Hi…the RPI4 has precisely the same GPIO pinout as the RPI3.
However, on the off chance that you need them, there are various new equipment fringe drivers that can be alloted to GPIO's, relatively few individuals use them yet, so there isn't a lot of information about them, however
Notwithstanding the heritage peripherals we have:

4 new PL011 UARTs with fair measured FIFOs

4 new I2C regulators with fixed (or diversely broken) clock extending

4 duplicates of the old SPI regulator

Turnkey PCB Assembly - Bittele Electronics
Online now: No Back to the top
1 guest and 0 members have just viewed this.