Lecture GPIO - C (Wiringpi)
Lecture GPIO - C (Wiringpi)
IN
RASPBERRY PI USING C
(WIRINGPI)
sarwan@nielit
Sarwan Singh
Assistant Director(S)
NIELIT Chandigarh
1
The whole purpose of
education is to turn mirrors
into windows.
- Harris
RASPBERRRY PI
sarwan@nielit
– above all it runs full featured Linux
distribution.
Not only computer it is hardware
prototyping tool.
The Pi has bi-directional I/O pins, which
can be used to drive LEDs, spin motors, or
read button presses.
2
GPIO PINOUT
When referencing Pi pin
numbers, there are
two different
numbering schemes:
sarwan@nielit
Broadcom chip-specific
pin numbers (BCM)
P1 physical pin
numbers.
3
sarwan@nielit
4
C (WIRINGPI) SETUP
InstallWiring Pi
WiringPi is not included with Raspbian,
sarwan@nielit
steps to download
$ git clone git://git.drogon.net/wiringPi
$ cd wiringPi
$ git pull origin
$ cd wiringPi
$ ./build
5
TEST WIRING PI
sarwan@nielit
$ gpio -g write 18 1
$ gpio -g write 18 0
if LED is connected to pin 18 it should blink on
and off following the last two commands.
To test the button, type:
$ gpio -g mode 17 up
$ gpio -g read 17
Either 0 or 1 will be returned, depending on
whether the button is pressed or not. 6
C (WIRINGPI) PROGRAM
#include <wiringPi.h>
wiringPiSetup(); // Initializes wiringPi
using wiringPi's simlified number system.
sarwan@nielit
wiringPiSetupGpio(); // Initializes
wiringPi using the Broadcom GPIO pin
numbers
//pin mode selection
pinMode(17, INPUT);
pinMode(23, OUTPUT);
pinMode(18, PWM_OUTPUT);
7
C (WIRINGPI) PROGRAM …
Digital Output
The digitalWrite([pin], [HIGH/LOW]) function
digitalWrite(23, HIGH); //To set pin 23 as
sarwan@nielit
HIGH
Digital Input
To read the digital state of a pin function
is digitalRead([pin])
sarwan@nielit
if (digitalRead(17))
printf("Pin 17 is HIGH\n");
else
printf("Pin 17 is LOW\n");
9
C (WIRINGPI) PROGRAM …
Pull-up/pull-down resistors
pullUpDnControl([pin], [PUD_OFF,
PUD_DOWN, PUD_UP]) function is used
sarwan@nielit
to pull pin.
e.g. a button on pin 22 needs to be pulled
up
pullUpDnControl(17, PUD_UP);
easy to test if button pulls low when it is
pressed.
10
C (WIRINGPI) PROGRAM …
Delay
delay([milliseconds])
delayMicroseconds([microseconds]).
sarwan@nielit
The standard delay will halt the program flow
for a specified number of milliseconds.
delay(2000); // delay for 2 seconds
11
BLINK
blink.c:
#include <wiringPi.h>
int main (void)
sarwan@nielit
{ wiringPiSetup () ;
pinMode (0, OUTPUT) ;
for (;;) {
digitalWrite (0, HIGH) ; delay (500) ;
digitalWrite (0, LOW) ; delay (500) ;
} return 0 ;
} 12
CREATING BLINKER.C
$ mkdir c_example
$ cd c_example
$ touch blinker.c
$ leafpad blinker.c &
sarwan@nielit
13
COMPILE AND EXECUTE!
To compile our program, invoke gcc, at
terminal
$ gcc -o blinker blinker.c -l wiringPi
sarwan@nielit
Type this to execute your program:
$ sudo ./blinker
14
DOWNLOAD AND INSTALL GEANY IDE
$ sudo apt-get update
$ sudo apt-get install geany
sarwan@nielit
$ sudo geany blinker.c
15
RUNNING BLINK.C
Then to compile and run, enter:
gcc -Wall -o blink blink.c -lwiringPi
sudo ./blink
sarwan@nielit
examples directory of the
wiringPi distribution To use the make file
to compile them:
make blink
make blink8
make blink12
16
BUTTON INTERFACING
two LEDs are
connected to the Pi’s
GPIO 18 and GPIO
23, P1 connector pin
sarwan@nielit
numbers, that’d be
pins 12 and 16.
button is connected
to Broadcom GPIO
17, aka P1 pin 11
17
CODIFY
#include <stdio.h> // Used for printf() statements
#include <wiringPi.h> // Include WiringPi library!
sarwan@nielit
const int pwmPin = 18; // PWM LED - Broadcom pin 18,
P1 pin 12
const int ledPin = 23; // Regular LED - Broadcom pin 23,
P1 pin 16
const int butPin = 17; // Active-low button - Broadcom
pin 17, P1 pin 11
const int pwmValue = 75; // Use this to set an LED
brightness
18
int main(void)
{
// Setup stuff:
wiringPiSetupGpio(); // Initialize wiringPi -- using
Broadcom pin numbers
pinMode(pwmPin, PWM_OUTPUT);
sarwan@nielit
sarwan@nielit
{ pwmWrite(pwmPin, 1024 - pwmValue);
// PWM LED at dim setting
// Do some blinking on the ledPin:
digitalWrite(ledPin, HIGH); // Turn LED ON
delay(75); // Wait 75ms
digitalWrite(ledPin, LOW); // Turn LED OFF
delay(75); // Wait 75ms again
} 20
} return 0;