Authorized Online Retailers:
In previous lesson Introduction Of Raspberry Pi GPIO , we learned what is GPIO pins ,GPIO naming and location map in Raspberry Pi. Now we start to learn how to program GPIO pins and control outside device through GPIO.
If you want to use Raspberry Pi to control other devices such as sensors, motors, LEDs etc, you need to exchange signal with these devices. This is called Input/Output(I/O). That is the job of GPIO pins.
In order to send/get data from GPIO pins, theoretically people can use ARM Assembly or C language to directly access the I/O address, registers, memories or trigger related I/O interrupt. However, such programming practice is super complicated, inconvenient and requires deep understanding about ARM CPU hardware detail.
Fortunately, experts invented a developing tool called “utility” or “library”. With the help of utility/library, complicated I/O detail programming are sealed into some black boxes called “Functions” which makes GPIO programming simple and easy.
There are many free Open source GPIO libraries in the Internet. In this lesson, we will teach you how to install “wiringPi” library which is very popular among C programmers.
We don’t want to talk much of the technical detail about wiringPi. If you want to get more wiringPi information , please read following link:
Test wiringPi exist or not
You can test whether the wiringPi library has already been installed by running following command in terminal：
If the message appears in your terminal, the wiringPi is installed in your Pi.
If the message appears as follow, the wiringPi has not been installed yet.
Install wiringPi through git
Step 1) Update your pi package repository (these 2 commands are recommended to do regularly)
sudo apt-get update
sudo apt-get upgrade
Step 2)Install git if you don’t have it
Please run the following command to install git (if you have installed git, please skip this step):
sudo apt-get install git-core
Step 3)Obtain WiringPi using GIT:
cd ~ git clone git://git.drogon.net/wiringPi
If you have already used the clone operation, then
cd ~/wiringPi git pull origin
Will fetch an updated version then you can re-run the build script below.
To build/install there is a new simplified script:
cd ~/wiringPi ./build
The new build script will compile and install it all for you – it does use the sudo command at one point, so you may wish to inspect the script before running it.
Note that the actual filename will be different – you will have to check the name and adjust accordingly.
Sample project for WiringPi
After installation wirngPi, you can test a project with LED on your raspberry Pi via wiringPi.
1) Experimental Principle
Connect LED with raspberry Pi GPIO, and then the program output high or low level to this GPIO to control the LED blink.
B17 in above graph means BCM GPIO#17 or Physical pin#11 or wiringPi#0(zero).
Note: B means BCM(Broadcom pin number). If you don’t know what is BCM pin#, Physical pin#, wiringPi#, please review our lesson 2: Introduction Of Raspberry Pi GPIO
3)Create a new file testwiringpi.c (you can name the file as you like) at the direction of /home/pi
cd ~ sudo nano testwiringpi.c
Copy the code from https://osoyoo.com/driver/pi3_start_learning_kit_lesson_3/testwiringpi.c to testwiringpi.c
Add wiringPi and stdio file which is used in this project at the beginning of the code. the LED connect with wPi#0 (physical pin#11 or GPIO/BCM 17). there are three ways of naming including physical, wiringPi and BCM. To learn more information about raspberry Pi GPIO, please visit: https://osoyoo.com/2017/06/26/raspberry-pi-basic-tutorial-introduction-of-raspberry-pi-gpio/. You can enter the following command to see the GPIO layout
Main function main() invokes print_message() function first, and the terminal will show the print message, and then set LEDPIN as output mode, at last, go on while loop statement and conditionals. The program will change Led pin level times and times to make the LED blink and send print message to terminal.
Note: Use the following command to download /testwiringpi.c code(replace the step of copying the code to nano editor )
gcc -Wall -o app testwiringpi.c -lwiringPi
Explanation: C language is high level language. Before running the project, the code need to compile as an executable file.
gcc: is GNU Compiler Collection. If you want to write your own C code and compile to run it, you need to master gcc.
-Wall: to get more error when compile the code
-o: to name the compiled file. You can name the file as your like. here we name as app
testwiringpi.c: means the original file which is compiled
-lwiringPi: is to load the library wiringPi (l is short for library)
4)Run Project, enter the following command and press “enter” to run the project
5)After running the code, your terminal will show print message as codef firstly, and then the LED blink and the ternimal will show LED status.
DownLoad Url osoyoo.com