Forum > Embedded

Reading GPIO pin as an INPUT on Raspberry PI


Awesome Programmer:
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.

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.

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 )


[0] Message Index

Go to full version