Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

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.

share|improve this question
    
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 4 '13 at 19:49

4 Answers 4

up vote 8 down vote accepted

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.

share|improve this answer
    
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
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
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 7:44

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

share|improve this answer
    
Thank you,but I am not familiar with both java and python.sorry. –  sumith Oct 25 '12 at 13:19

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..

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.