0% found this document useful (0 votes)
113 views

Lecture GPIO - C (Wiringpi)

This document provides instructions for programming GPIO pins on the Raspberry Pi using C and the WiringPi library. It discusses setting up WiringPi, testing GPIO functionality from the command line, writing a basic blinking LED C program, and compiling and running the program. It also provides an example of a more advanced C program that interfaces an LED, PWM LED, and button, and changes the LED behavior based on the button state.

Uploaded by

Smart Classe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Lecture GPIO - C (Wiringpi)

This document provides instructions for programming GPIO pins on the Raspberry Pi using C and the WiringPi library. It discusses setting up WiringPi, testing GPIO functionality from the command line, writing a basic blinking LED C program, and compiling and running the program. It also provides an example of a more advanced C program that interfaces an LED, PWM LED, and button, and changes the LED behavior based on the button state.

Uploaded by

Smart Classe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

GPIO PROGRAMMING

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

 Irrespective of its size, Raspberry Pi is a


powerhorse of a computer. It can drive
HDMI displays, mouse, keyboard, camera

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

 WiringPi is a C library, it includes


a command-line utility as well. It can be
tested from command line:
$ gpio -g mode 18 output

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

PWM (“Analog”) Output


For PWM function is pwmWrite([pin], [0-
1023])
pwmWrite(18, 723);
8
C (WIRINGPI) PROGRAM …

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");

will print the status of pin 22.

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!

 // Pin number declarations. We're using the Broadcom


chip pin numbers.

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

// Set PWM LED as PWM output


 pinMode(ledPin, OUTPUT); // Set regular LED as
output
 pinMode(butPin, INPUT); // Set button as INPUT
 pullUpDnControl(butPin, PUD_UP);
// Enable pull-up resistor on button
 printf("Blinker is running! Press CTRL+C to quit.\n");
19
 // Loop (while(1)):
 while(1)
 { if (digitalRead(butPin)) // Button is released if this returns 1
 { pwmWrite(pwmPin, pwmValue); // PWM LED at bright setting
 digitalWrite(ledPin, LOW); // Regular LED off
 }else // If digitalRead returns 0, button is pressed

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;

You might also like