raspberrypi gpio examples with c language (some arduino projects) – introduction and led blink

Aim of this tutorials is about raspberryp pi gpis with c codes. writing some arduino examples with c language.

my test board  is raspberrypi B model rev.P2 

I have installed raspian to my machine. I will test on it. raspian is easy to install, and a full linux distrubition. preparing a test on raspian is so fast. at the end, I have to say that, raspian is a big embedded installation. starting a raspian is long about 5 seconds.  using raspian at a commercial project, is not a good decision. compiling a custom kernel,and create a custom linux can be better. of course I must say that, linux is not a RTOS. some linux RTOS distribution exits. search them for better results.

as you know raspberry have some different configured boards. all boards have a model name and revision number. because of this you must correct your pin number from internet.

I will use B model rev P2.

my raspi has 26 total pins. 2 of them 5V, 2 of them 3.3V, 5 of them GROUND, at the end 26-9=17 gpio pins I have.


as you see above, 17 gpio pins.

of course some of them can be use for different functionalities. sample

for serial connection  RxD ve Txd, for I2C ……

as you see below some of them can be assigned for different functionalities.


I will change above table a little. I will give from left to right a number. And I will define pins with that names


with linux it is so easy to test this pins. for testing you don’t need to code.

at linux console

cd /sys/class/gpio   

if you cannot enter , kernel does not support  gpio sysfs. compile kernel with gpio sysfs.

in linux there is lots of file systems like devpts, sysfs, proc  etc….

with these files and directiories you can get lots of information about linux,working processes, devices etc… these file systems are a bridge with kernel space and user space. (search kernel space, user space)


kernel documentation about gpio sysfs 

if everything is good, lets continues

ls     enter

you will see 3 files below

export  unexport gpiochip0

export    you can open a pin

unexport you can close a pin

opening pin 2

echo 2 > export

for closing

echo 2 > unexport

after that

echo 2 >export


you will see a directory -> gpio2

cd gpio2

ls -> you will see below

active_low  device  direction  edge  power  subsystem  uevent  value

some of them file some of directories

direction-> values  accepted “in”  or “out”

echo “in” > direction    pin is input mode

echo “out”> direction  pin is output mode


value->  0 (low) or 1 (high)

echo “1” >value 

active_low-> default  0 low 1 high  means

edge->“none”, “rising”, “falling”, or  “both”  getting information about pin. expecialy using with poll or select  etc…

uevent-> ? I could not find any information, Anyone knows?




I have added linux pin numbers to table.

example GPIO_3 pin is 14 linux pin number

you can control GPIO_3 with

echo 14 >export

cd gpio etc ………

lets create a simple led circuit below




restart your raspi for a fresh start

cd /sys/class/gpio 

echo 14 > export

cd gpio14

echo “out” > direction

echo 1 >value

echo 0>value

echo 1 >value