Recent

Author Topic: Reading GPIO pin as an INPUT on Raspberry PI  (Read 1811 times)

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 451
  • Programming is FUN only when it works :)
    • Cool Technology
Reading GPIO pin as an INPUT on Raspberry PI
« on: August 30, 2023, 03:18:03 pm »
Hi, I am working on Pi 3 and I want to be able to read GPIO pin 2 for its state or status every 3 seconds or so. I've been looking around for solution and I don't know which direction to take. I've tried their example or sample codes and I am either more confused or don't understand their code. I want to be able to get Direct Access to the pin state using BaseUnix, but I can't seem to understand their sample code; they are talking about setting the pin 2 as OUTPUT to read it as INPUT. If someone could give me a very simple sample code that only reads GPIO Pin 2 for its status and displays it or does writeln, it would be very helpful.

Thank you.

ccrause

  • Hero Member
  • *****
  • Posts: 892
Re: Reading GPIO pin as an INPUT on Raspberry PI
« Reply #1 on: August 31, 2023, 08:12:13 am »
It isn't clear what example you are referring to.

There is a wiki example that describes how to read a pin.  Perhaps test the wiki example, then ask here if you need further help/clarification.

Thaddy

  • Hero Member
  • *****
  • Posts: 14659
  • Sensorship about opinions does not belong here.
Re: Reading GPIO pin as an INPUT on Raspberry PI
« Reply #2 on: August 31, 2023, 11:43:23 am »
It is as easy as knowing the virtual pin starting address and using the type helpers for bit manipuation
That address varies among Raspberry versions but is documented on the Raspberry Pi website.
<address>.Setbit/togglebit etc. I can post a complete example for it if you want.
Actually, I wrote the helpers with my Raspberry Pie's in mind and it works quite good... ::)
(Note that Avra extended the bit manipulation helpers so you may want to examine his new options too )
(Also note I never tested this for AARCH64 on RP3, but I did for RP4, the code assumes a 32 bit value, I tested it on all 32 bit types since RP1 )
« Last Edit: August 31, 2023, 11:52:39 am by Thaddy »
bitrate is always calculated like this:sample rate * bitdepth * number of channels.

 

TinyPortal © 2005-2018