Raspberry Pi_Eng_25.6.2 WiringPi library


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


25.6.2  <WiringPi> library

 

25.6.2.1    Overview of <WiringPi> Library

 

<WiringPi> library is a GPIO tool developed by Gordon Henderson. This is a GPIO processing library developed in C language for use with the BCM2835 used in Raspberry Pi and is distributed under the terms of the GNU LGPLv3 license. This library was originally developed to make it easier for users familiar with Arduino's "wiring" system to easily use Raspberry Pi.

 

This library can not only be used in C and C ++, but also in other development languages such as Python, Java, and Perl by using an appropriate wrapper.

 

<WiringPi> library contains "gpio" command that can be used on the command-line, which allows you to program the GPIO pin to work to the way you want or to do any necessary preparation. You can also use this command to read signal inputs from a pin, send output signals, and control the GPIO pins in a Shell script.

 

<WiringPi> library is an extensible tool, which provides a module that expands the <WiringPi> library to use a analog interface device on a Gertboard or to use a MCP23x17 / MCP23x08 (I2C 7 SPI) GPIO expansion chip. It also provides a module that allows the ATmega (eg Arduino, or the Gertboard) device to be used as an additional GPIO expansion via a serial port of Raspberry Pi. In addition, users can also utilize the <WiringPi> library usefully to develop their own extension modules for them to interact with their own peripherals.

 

<WiringPi> library supports analog input/output, but since Raspberry Pi does not have an analog H/W, it provides a module that allows Gertboard analog chips, other analog/digital devices, or digital / analog devices to be installed relatively easily.

 

For more detailed information, please refer to the following.

    http://wiringpi.com  

 

 


 

25.6.2.2    Pin Map of <WiringPi> library

 

The GPIO pin number used in <WiringPi> library is different from the GPIO pin number of the original Raspberry Pi. Below is a diagram showing the mapping details between each other and the basic status of each GPIO pin.

 

Mode

V

Name

Wiring Pi

No

BCM

No

Physical

No

BCM

No

Wiring Pi

No

Name

V

Mode

3.3v

 

 

1

2

 

 

5v

ALT0

1

SDA.1

8

2

3

4

 

 

5v

ALT0

1

SCL.1

9

3

5

6

 

 

0v

IN

0

GPIO. 7

7

4

7

8

14

15

TxD

1

ALT0

0v

 

 

9

10

15

16

RxD

1

ALT0

IN

0

GPIO. 0

0

17

11

12

18

1

GPIO. 1

0

IN

IN

0

GPIO. 2

2

27

13

14

 

 

0v

IN

0

GPIO. 3

3

22

15

16

23

4

GPIO. 4

0

IN

3.3v

 

 

17

18

24

5

GPIO. 5

0

IN

ALT0

0

MOSI

12

10

19

20

 

 

0v

ALT0

0

MISO

13

9

21

22

25

6

GPIO. 6

0

IN

ALT0

0

SCLK

14

11

23

24

8

10

CE0

1

ALT0

0v

 

 

25

26

7

11

CE1

1

ALT0

IN

0

SDA.0

30

0

27

28

1

31

SCL.0

0

IN

IN

0

GPIO.21

21

5

29

30

 

 

0v

IN

0

GPIO.22

22

6

31

32

12

26

GPIO.26

0

IN

IN

0

GPIO.23

23

13

33

34

 

 

0v

IN

0

GPIO.24

24

19

35

36

16

27

GPIO.27

1

OUT

IN

0

GPIO.25

25

26

37

38

20

28

GPIO.28

0

IN

0v

 

 

39

40

21

29

GPIO.29

0

IN

Table 25‑4 Pin map of <WiringPi> library

 

Each item in the title has the following meanings

    Mode         -- Usage status of pin

IN              -- Input

OUT           -- Output

ALTn           -- ALTernative modes for each pin - ALT0 ~ ALT5

 

For more information on the Pin layout of Raspberry Pi, please see the following resources:

    http://pi.gadgetoid.com/pinout/pin31_gpio6  

 


 

25.6.2.3    Installing <WiringPi> Library

 

The <WiringPi> library must be downloaded and installed from GitHub. To download the material from GitHub, you need to install git-core, the source management tool. For more information, see [17.4.3 How to Download from Internet Using GitHub].

 

First, use "git" command as follows to download the <WiringPi> Library.

 

git clone  git://git.drogon.net/WiringPi

 

 

Next, move to <wiringPi> directory where the <WiringPi> library is downloaded and executes "build" script included there.

 

pi@raspberrypi ~/wiringPi $ ./build

wiringPi Build script

=====================

WiringPi Library

[UnInstall]

[Compile] wiringPi.c

[Compile] wiringSerial.c

~ Skip

~ Skip

 

All Done.

NOTE: To compile programs with wiringPi, you need to add:

    -lwiringPi

  to your compile line(s) To use the Gertboard, MaxDetect, etc.

  code (the devLib), you need to also add:

    -lwiringPiDev

  to your compile line(s).

 

To check whether the installation is successful, execute the following command to check whether it is normally processed.

 

pi@raspberrypi ~/wiringPi $ gpio -v

gpio version: 2.26

Copyright (c) 2012-2015 Gordon Henderson

This is free software with ABSOLUTELY NO WARRANTY.

For details type: gpio -warranty

 

Raspberry Pi Details:

  Type: Model B+, Revision: 1.2, Memory: 512MB, Maker: Sony


 

25.6.2.4    "gpio" Command

 

<WiringPi> library contains "gpio" command that you can use on the command-line, which allows you to program the GPIO pin to work the way you want, or you can do any necessary preparation. You can also use this command to read signal inputs from pins, send output signals to pins, and control the GPIO pins in Shell script.

 

[Command Format]

gpio   [option]

 

[Command Overview]                

    It is a tool to control GPIO pin installed in Raspberry Pi, to receive input signal from GPIO pin, or to send output signal to GPIO pin.

    User privilege           -- Normal user.

 

[Detail Description]

    This command is a command tool that allows easy access to GPIO pins in Raspberry Pi or SPI A/D and D/A converters in Gertboard.

    With this command, you controls IO on the PiFace IO board and loads SPI and I2C kernel module.

    It is basically intended for simple test and cause analysis, but can be used for general purposes in Shell script if it is ok that the GPIO adjustment rate is a little slower.

 

[Main option]

-v

Output the current version including the board revision of the Raspberry Pi.

-g

Use the BCM_GPIO pins numbers rather than wiringPi pin numbers. Note: The BCM_GPIO pin numbers are always used with the export and edge commands.

-1

Use the physical pin numbers rather than wiringPi pin numbers. Note: that this applies to the P1 connector only. It is not possible to use pins on the Revision 2 P5 connector this way, and as with -g the BCM_GPIO pin numbers are always used with the export and edge commands.

-x extension

 

This causes the named extension to be initialised. Extensions comprise of a name (e.g. mcp23017) followed by a colon, then the pin-base, then more optional parameters depending on the extension type.  See the web page on http://wiringpi.com/the-gpio-utility/

-p

Use the PiFace interface board and its corresponding pin numbers. The PiFace will always appear at pin number 200 in the gpio command. You can assign any pin numbers you like in your own programs though.

read <pin>

Read the digital value of the given pin and print 0 or 1 to represent the respective logic levels.

write <pin> <value>

Write the given value (0 or 1) to the pin. You need to set the pin to output mode first.

readall

 

Output a table of all GPIO pins values. The values represent the actual values read if the pin is in input mode, or the last value written if the pin is in output mode. The readall command is usable with an extension module (via the -x parameter), but it's unable to determine pin modes or states, so will perform both a digital and analog read on each pin in-turn.

pwm <pin> <value>

 

Write a PWM value (0-1023) to the given pin. The pin needs to be put into PWM mode first.

clock <pin> <frequency>

Set the output frequency on the given pin. The pin needs to be put into clock mode first.

mode <pin> <mode>

 

Set a pin into input, output or pwm mode. Can also use the literals up, down or tri to set the internal pull-up, pull-down or tristate (off) controls.

The ALT modes can also be set using alt0, alt1, ... alt5.

 

[Used Example]

The following shows the current state of the GPIO pins. It shows the state of each pin on the left/right according to the arrangement of GPIO pins.

 

pi@raspberrypi ~ $ gpio readall

+-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+

 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |

 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+

 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |

 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |

 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |

 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |

 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |

 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 1 | IN   | GPIO. 1 | 1   | 18  |

 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |

 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |

 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |

 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |

 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |

 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |

 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |

 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |

 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |

 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |

 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |

 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |

 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |

 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |

 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+

 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |

 +-----+-----+---------+------+---+--B Plus--+---+------+---------+-----+-----+


 

25.6.2.5    Basic usage of <WiringPi> Library

 

The following is a brief description of how to use <WiringPi> library in C language. For other languages, see the related resources..

 

 

   port number

 

You should specify the port number based on the <WiringPi> library in the port number unless otherwise specified.

 

 

   include

 

To use GPIO library, you have to include header. Specify it at the beginning of the program.

 

#include <wiringPi.h>

 

 

   Setup

 

<WiringPi> library must be initialized before use.

 

wiringPiSetup ()

 

 

   Setting pin Mode

 

The purpose of GPIO ports should be set to input or output before use.

 

pinMode (<port> , <pin-mode> )

 

 

 

 

Pin mode specifies whether to use the port as input or output.

    INPUT         -- Input

    OUTPUT      -- Output

 

Example) pinMode (0, OUTPUT) ;  

 

 

   Output

 

For the port specified as the output port, the specified value can be output.

 

digitalWrite (<port> , <output> )

 

The output has the following meanings.

    HIGH         -- Current On

    LOW           -- Current Off

 

Example)digitalWrite (0, HIGH)

 

 

   Input

 

For the port specified as the input port, the value coming from the port can be read.

 

variable = digitalRead ( <port> )

 

Intput has the following meanings

    HIGH          -- Current On

    LOW           -- Current Off

 

Example) variable = digitalRead ( 0 )

 

For an example of interface processing using <WiringPi> library, see [25.7.1.2 Example of Using <WiringPi> Library].