9

I am using raspberry pi with Debian Wheezy 18-09-2012 image. I can access gpio pins with Wiring pi tool. But I would like to know is any other method that we can access gpio pins directly without wiring pi?

When I run the following command on terminal

echo 0 >/sys/class/gpio/export

it shows

-bash: /sys/class/gpio/export: Permission denied

Is there any solution for this?

I also tried the following script

#!/bin/sh

echo "4" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio4/direction

while true
do
        echo 1 > /sys/class/gpio/gpio4/value
        echo 0 > /sys/class/gpio/gpio4/value
done

from here and i get the same error.

I would like to run the code on C because right now I am not familiar with both Java and Python.

Piotr Kula
  • 17,307
  • 6
  • 65
  • 104
sumith
  • 613
  • 2
  • 9
  • 17
  • Hi,I have done such gpio operations in beagleboard by accessing /sys/class/gpio/gpio(portpin)/value with file open/write/read methods using c. But here it shows permission denied... – sumith Oct 25 '12 at 15:14
  • You should add the group of these to your user, or run as root with sudo. Notice that redirection is done before sudo run, so it will try to open it as your user, not root. – Anders Jul 04 '13 at 19:49

4 Answers4

10

Yes, there are at least two of them:

GPIO kernel module

The full documentation of this subsystem can be found here (look for Sysfs Interface for Userspace subsection). Basically, kernel exports some files in /sys/class/gpio/ directory. Here's an example usage (using pin 0 as an input and then switching it as output and setting to 1):

# echo 0 >/sys/class/gpio/export
# echo in >/sys/class/gpio/gpio0/direction
# cat /sys/class/gpio/gpio0/value
# echo out >/sys/class/gpio/gpio0/direction
# echo 1 >/sys/class/gpio/gpio0/value

Note, that in normal case, you need root permissions to do this.

Since all this is just a file operations, it can be easily done in C program with open()/read()/write() functions. Be careful when using fopen()/fread()/fwrite functions since they use buffered I/O.

Directly using registers

This one, I believe, is being used by wiringPi itself. It uses /dev/mem device to access physical RaspberryPi memory and to operate on BCM2835 memory mapped registers directly. In order to use it, you would have to read BCM2835 datasheet (General Purpose I/O (GPIO) section). It is not that trivial however. Note, that you need root permissions to do this.

There's also one more C library that uses this way, you can find it here. It does much more than only manipulating GPIO but since it's quite simple, it should be easy to extract only the GPIO code and understand how it's working.

Krzysztof Adamski
  • 9,615
  • 1
  • 37
  • 53
  • Thank you.But when tried to run echo 0 >/sys/class/gpio/export it shows -bash: /sys/class/gpio/export: Permission denied Why,? tell me please – sumith Oct 25 '12 at 13:15
  • 1
    @Sumith: As mentioned on the answer, you need root user permissions. Try using sudo -i to become root user and then type the example commands. – Krzysztof Adamski Oct 25 '12 at 13:19
  • Hi Thank you. I am going to try again. I hope this time I will catch it – sumith Oct 25 '12 at 13:22
  • Sorry,failed. When I run the command sudo -i it jumps to system settings,like updating pi,expanding sd card. etc.. So I run the command with sudo ..But still the same error. I doubt there must be some serious problems with my image . – sumith Oct 25 '12 at 13:27
  • 1
    @sumith what does ls -l /sys/class/gpio/export display? @KrysztofAdamski Can you edit your answer to bold the fact that you need to be root to access these files? I didn't see this comment in your answer at first. – HeatfanJohn Oct 25 '12 at 18:08
  • 1
    chmoding or chowning the files (as root) is another option to permit subsequent unprivileged access, but you will have to do it on every boot. Also try sudo -s to get a shell running as root from which to run tests – Chris Stratton Oct 25 '12 at 21:37
  • @HeatfanJohn after entering the command ls -l /sys/class/gpio/export it shows --w------- 1 root root 4096 Oct 26 04:39 /sys/class/gpio/export .Thank you – sumith Oct 26 '12 at 04:41
  • @Chris Stratton Thank you. I run sudo -s first and run the above commands,it is working. Nice help. – sumith Oct 26 '12 at 04:46
  • @Chris Stratton ,can I ask you one more doubt? Now I can run commands like echo 0 >/sys/class/gpio/export in terminal and working well. But I want to run a c program for accessing gpio pins by performing file open/write/read on /sys/class/gpio/gpio(pin) ? – sumith Oct 26 '12 at 05:28
  • 2
    @sumith: Yes, your program should just open those files and read/write from/to them with normal read/write operations. It's much better to use unbuffered read()/write() functions instead of buffered fread()/fwrite(). Also, make sure your program is going to be run with root privileges. – Krzysztof Adamski Oct 26 '12 at 07:44
  • @KrzysztofAdamski Hi, Thank you very much for your reply. I can run the c code now. But Before running the c code I had to run sudo -s,otherwise it will not work. I wish to run the code on startup. Can I? – sumith Oct 26 '12 at 10:32
  • @sumith: Using sudo 'switches' you to root user. This is because you need root permissions to do this. If you wrote your own program you can make it always run with root permissions, no matter which user run it using setuid technique (you should be able to easily find a lot of information on how to do this), you need to be aware of security implication on doing so, however. about running the application on start up, I assume you mean running this on system start up. If so, it should be possible since in normal situation all start up scripts runs with root permissions. – Krzysztof Adamski Oct 26 '12 at 10:51
  • @KrzysztofAdamski Hi Thank you. I am working on it. – sumith Oct 26 '12 at 11:13
  • @KrzysztofAdamski , It is working on system start up. I entered the path of exe file which I want to run in the /etc/rc.local file and then restart.It worked well,but I needed to run ps aux to find the running process and sudo kill (process id) to stop the process. I am trying to run the same by editing in the inittab file because I think it is more better. Because now, if I killed the process I need to start it again after reboot. Thank you – sumith Oct 26 '12 at 12:04
  • @KrzysztofAdamski Hi,I could run the program by adding bes:2345:respawn:/home/pi/GPIO/exe in /etc/inittab .It is working well.Thank you – sumith Oct 26 '12 at 12:57
3

Or, if you don't mean strictly 'directly', you could use Python or even Java

recantha
  • 4,489
  • 20
  • 26
0

I can access gpio pins using a method suggested by Chris Stratton.

In /etc/rc.local file I add the commands

echo 17(gpio pin) > /sys/class/gpio/export

and

chmod 777 -R /sys/class/gpio/gpio17 .

I can run my c program without entering sudo -s first.

Thank you all for supporting me..

sumith
  • 613
  • 2
  • 9
  • 17
0

You can gain access to all the Raspberry Pi peripherals (including GPIO, C, ...) with C by writing to the respective registers on the Pi.

I wrote a tutorial about this here: http://www.pieter-jan.com/node/15

This way, you won't have to use any libraries, just a basic knowledge of C. I hope this helps!

As said above, you will need to execute all your programs as sudo user.

Pieter-Jan
  • 101
  • 2