raspberrypi gpio examples with c – part3- reading input gpio

I will explain in this article , reading from input gpio

if you did not read previous articles, some definitions can become to you undefined.

first reading from gpio

gpio pin levels address are below

0x 7E20 0034 GPLEV0 GPIO Pin Level 0 32 R
0x 7E20 0038 GPLEV1 GPIO Pin Level 1 32 R

we have  32 bit+32 bit =64 bit registers. as you know bcm cpu has 54 pins. these 64 bits are enough for us.

#define PIN_LEVEL_START  (13)

calculation of address 0x34/(sizeof(int)=13

int value= *(gpio+PIN_LEVEL_START+PIN_WRITE_POSITION(pin));

we get all 32 bits, now we will lookat, pin bit

int pin= 1 << PIN_WRITE_SHIFT(pin)

if( value & pin) return HIGH else return LOW

now we will create a breadboard test environment;


1 push button

1 count 10k resistor

2 cables.

attach them as below

yellow cable is output, orange cable is input


and the code is here read.c








raspberrypi gpio examples with c language – part 2- blink led

now we are going deep.

how to reach gpio pins on raspberrypi. as I sad before

one of them /sys/class/gpio file system. if you use it, it will be slow. you can find performance tests

one of them is writing a linux driver. it will be fast

and the last one is, user space driver. it will be also fast.

I will explain in this article. writing a user space driver.

I will write this code explanation with my codes.

also we need the arm bcm2835 peripherals document.

we will map raspberrypi memory. and change values in memory.

first of all we are getting peripherals start phsical address from document(page 6)


then we are getting gpio register physical start address from document(page 90)

this document says that gpio peripherals start from 0x7e20 0000, but this address are not physical address. we can easily convert them. 0x7e20 0000 means PERIPHERALS_BASE_PHYSICAL+ 0x20 0000 pyhsical address.address


raspberry pi has 54 GPIOS pin in SOC.   now we will map memory to our program. you must be root to do this.

creating some variables

//lenght of registers end to top

#define REGISTERS_SIZE (0x7E2000B0-0x7E200000)

int mem_fd;
volatile int * gpio;

opening memory device

mem_fd = open(“/dev/mem”, O_RDWR | O_SYNC);

mapping to our space

gpio = (volatile int *) mmap(NULL, REGISTERS_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd,

as above picture, there is “GPIO Function Select 0″,”GPIO Function Select 1” etc… there is 6 count. every them is 32 bits. this is important!

you can use this registers to manage functions of  pins.  you can set to OUTPUT,INPUT,TxD or etc.

you can manage pins with 3 bits

000 = GPIO Pin  is an input
001 = GPIO Pin  is an output
100 = GPIO Pin  takes alternate function 0
101 = GPIO Pin  takes alternate function 1
110 = GPIO Pin  takes alternate function 2
111 = GPIO Pin  takes alternate function 3
011 = GPIO Pin  takes alternate function 4
010 = GPIO Pin  takes alternate function 5

alternate function means, you can set some bits for using different functions. suppose I2C,RxD,PWM etc…

you can find which pin can have an alternate  function from page 102. a sample image below


lets continue,every 32  function  select  bits can manage only 10 pins. ( 3×10=30).

if you want to manage pin 21, you can calculate its position like this

24/10=2  this means that your pin is in “GPIO Function Select 3” bits.

#define PIN_MODE_POSITION(number) (number/10)

every 3 bits is assigned to a pin,

24%10=4 you are the 5th pin in “GPIO Function Select 3” bits. and every pin has 3 bit than 12-13-14 is your pin

#define PIN_MODE_SHIFT(number)   ((number%10)*3)

if you set 000  then pin 24 is input

if you set 001 then pin 24 is output

if you set 100 then pin 24 is ALT_FUNC_0 from above picture.

for setting pin INPUT

*(gpio + PIN_MODE_POSITION(number)) &= ~(0x0000007<< PIN_MODE_SHIFT(number));

for setting pin OUTPUT

first setting all zero (000)

*(gpio + PIN_MODE_POSITION(number)) &= ~(0x0000007<< PIN_MODE_SHIFT(number));

then setting 001
*(gpio + PIN_MODE_POSITION(number)) |=  0x01<< PIN_MODE_SHIFT(number);

lets continue to set LOW and  set HIGH

First set HIGH

you must use below addresses

0x 7E20 001C      GPSET0 GPIO Pin Output Set 0
0x 7E20 0020       GPSET1 GPIO Pin Output Set 1

you have got 2 registers 32 bit

as I said, there is 54 pin on raspi SOC , then you need 64 bit

if pin number is 24 then where your address start


#define PIN_WRITE_POSITION(number)  ((number/32))

what is your address

gpio + 7 +PIN_WRITE_POSITION(24) is your address

what will you write that address  1<< number%32

#define PIN_WRITE_SHIFT(number)  (number%32)

to set HIGH

*(gpio + 7 + PIN_WRITE_POSITION(number) ) = 1 << PIN_WRITE_SHIFT(number);


Second set LOW

find output clear parts from document as above,

0x 7E20 0028 GPCLR0 GPIO Pin Output Clear 0
0x 7E20 002C GPCLR1 GPIO Pin Output Clear 1

you have got 64 bits again

to set low

*(gpio + 10 + PIN_WRITE_POSITION(number) ) = 1 << PIN_WRITE_SHIFT(number);


and this is the main.c file main

download and

gcc -o blink main.c





















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
























hamza kılıç

writing since 1997.

knowing languages

excellent c, c++, c#

pure java,php,html,javascript,css

interested in


linux system programming,

image processing,

embedded systems,

artifical inteligence,