raspberrypi gpio examples with c – part4- pull up/down

hi, again.

when you put a pin to mode INPUT.  you can set pull up/down registers.

because when you booted raspberry pi ,all gpio’s have default PULL UP or PULL DOWN registers. previus article sample is working because when we put pin to INPUT, it’is LOW. if you use another pin that is HIGH it will not work.

look at below picture. it is taken from bcm2835 sheet page 102

raspberry pi pull up/down

raspberry pi pull up/down

now changing pull up/down

in document page 91, I have taken this address’s

0x 7E20 0094  GPIO Pin Pull-up/down Enable 32

0x 7E20 0098 GPIO Pin Pull-up/down Enable Clock 0

0x 7E20 009C GPPUDCLK1 GPIO Pin Pull-up/down Enable Clock 1

#define PERIPHERALS_GPIO_PULL_PHYSICAL (0x94/sizeof(int))

as you see we defined two address, why ?

because we will set PULL value to PERIPHERALS_GPIO_PULL_PHYSICAL address


but procedure of changing pull value is more than this.

from document:

1. Write to GPPUD to set the required control signal (i.e. Pull-up or Pull-Down or neither
to remove the current Pull-up/down)
2. Wait 150 cycles – this provides the required set-up time for the control signal
3. Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads you wish to
modify – NOTE only the pads which receive a clock will be modified, all others will
retain their previous state.
4. Wait 150 cycles – this provides the required hold time for the control signal
5. Write to GPPUD to remove the control signal
6. Write to GPPUDCLK0/1 to remove the clock

lets start:

write correct bit values to (gpio+PERIPHERALS_GPIO_PULL_PHYSICAL)

correct values:

00 = Off – disable pull-up/down
01 = Enable Pull Down control
10 = Enable Pull Up control


wait 5 microseconds


set correct pin bit to 1 at (gpio+PERIPHERALS_GPIO_PULL_CLOCK_PHYSICAL) adress


wait 5 microsecond


write zero to (gpio+PERIPHERALS_GPIO_PULL_PHYSICAL) address


wait 5 microsecond




below is the algorithm.

now create a template board above. and test it


2 cables (yellow is output, and orange is input)

1 10K resistor

1 push button


as you see, orange cable is at 7 pin, which is HIGH when rebooted raspberry.

if you disable


code it will now work as expected.

reboot your raspberry before testing. when you change a pin PULL_DOWN, until you change again it will be PULL_DOWN.

here is test code pull.c

gcc -o test pull.c











Bir Cevap Yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir


Şu HTML etiketlerini ve özelliklerini kullanabilirsiniz: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>