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,336
  • 6
  • 66
  • 105
sumith
  • 623
  • 2
  • 9
  • 17

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,605
  • 1
  • 38
  • 53
3

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

recantha
  • 4,489
  • 21
  • 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
  • 623
  • 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