#include <wiringPi.h>
After you've included the library, your first steps should be to initialize it.
This step also determines which pin numbering scheme you'll be using
throughout the rest of your program. Pick one of these function calls to
initialize the library:
wiringPiSetup(); // Initializes wiringPi using wiringPi's siml
ified number system.
wiringPiSetupGpio(); // Initializes wiringPi using the Broadco
m GPIO pin numbers
WiringPi's simplified number system introduces a third pin-numbering
scheme. We didn't show it in the table earlier, if you want to use this
scheme, check out their pins page for an overview.
Pin Mode Declaration
To set a pin as either an input or output, use the
function. Mode can be either
INPUT
For example, to set pin 22 as an input, 23 as an output, and 18 as a PWM,
write:
wiringPiSetupGpio()
pinMode(17, INPUT);
pinMode(23, OUTPUT);
pinMode(18, PWM_OUTPUT);
Keep in mind that the above example uses the Broadcom GPIO pin-
numbering scheme.
Digital Output
The
digitalWrite([pin], [HIGH/LOW])
output pin either HIGH or LOW. Easy enough, if you're an Arduino user.
To set pin 23 as HIGH, for example, simply call:
digitalWrite(23, HIGH);
PWM ("Analog") Output
For the lone PWM pin, you can use
a value between 0 and 1024. As an example...
pwmWrite(18, 723);
...will set pin 18 to a duty cycle around 70%.
Digital Input
If you're an Arduino veteran, you probably know what comes next. To read
the digital state of a pin,
digitalRead([pin])
example...
if (digitalRead(17))
printf("Pin 17 is HIGH\n");
else
printf("Pin 17 is LOW\n");
...will print the status of pin 22. The
pin is HIGH and 0 if it's LOW.
pinMode([pin], [mode])
,
, or
.
OUTPUT
PWM_OUTPUT
function can be used to set an
to set it to
pwmWrite([pin], [01023])
is your function. For
function returns 1 if the
digitalRead()
Page 12 of 17
Need help?
Do you have a question about the Raspberry gPIo and is the answer not in the manual?
Questions and answers