Controlling GPIO with wiringPi
The wiringPi libraries are a set of functions written in C that make it easy to control the Raspberry Pi's GPIO pins. You can use the functions in this library to control GPIO pins in your own programs.
You can also use wiringPi on the command line. This is very useful for debugging problems with circuits connected to GPIO pins. It's also useful for controlling GPIO with Bash scripts.
In order to install the libraries, you have to install the 'git' source code management system. Make sure your Pi can access the internet. You can install git by opening a terminal and typing these commands:
$ sudo apt-get install git-core
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get update
$ sudo apt-get upgrade
Get the wiringPi project using this command:
$ git clone git://git.drogon.net/wiringPi
Change to the new directory, and get the code from the repository at drogon.net:
$ cd wiringPi
$ git pull origin
$ git pull origin
Build the code:
$ ./build
Test wiringPi by typing this command:
$ gpio readall +----------+-Rev2-+------+--------+------+-------+ | wiringPi | GPIO | Phys | Name | Mode | Value | +----------+------+------+--------+------+-------+ | 0 | 17 | 11 | GPIO 0 | IN | Low | | 1 | 18 | 12 | GPIO 1 | IN | Low | | 2 | 27 | 13 | GPIO 2 | IN | Low | | 3 | 22 | 15 | GPIO 3 | IN | Low | | 4 | 23 | 16 | GPIO 4 | IN | Low | | 5 | 24 | 18 | GPIO 5 | IN | Low | | 6 | 25 | 22 | GPIO 6 | OUT | High | | 7 | 4 | 7 | GPIO 7 | IN | Low | | 8 | 2 | 3 | SDA | IN | High | | 9 | 3 | 5 | SCL | IN | High | | 10 | 8 | 24 | CE0 | ALT0 | High | | 11 | 7 | 26 | CE1 | ALT0 | High | | 12 | 10 | 19 | MOSI | ALT0 | Low | | 13 | 9 | 21 | MISO | ALT0 | Low | | 14 | 11 | 23 | SCLK | ALT0 | Low | | 15 | 14 | 8 | TxD | ALT0 | High | | 16 | 15 | 10 | RxD | ALT0 | High | | 17 | 28 | 3 | GPIO 8 | IN | Low | | 18 | 29 | 4 | GPIO 9 | IN | Low | | 19 | 30 | 5 | GPIO10 | IN | Low | | 20 | 31 | 6 | GPIO11 | IN | Low | +----------+------+------+--------+------+-------+
You should see a table listing the state of the GPIO pins. Before you can use any of the pins, you must configure them as either outputs or inputs. The following command sets up pin 17 for use as an output:
$ gpio -g mode 17 out
This command sets pin 17 to logic 1:
$ gpio -g write 17 1
The -g option tells the gpio command to use the standard pin numbering scheme rather than the numbering scheme invented for wiringPi. You can test the voltage of pin 17 with a multimeter to see if it changes when you execute the command above.
See also: wiringPi.com
Use GPIO to turn on an LED
The picture on the right shows a breadboard. Cables and component leads can be inserted into the holes to build circuits. The holes are linked by copper strips underneath the surface of the breadboard.
The holes at the edge are connected lengthways in two lines along the breadboard. The line of holes nearest the blue line is used as the negative supply line, and the line nearest the red line is used for the positive supply line. The rest of the holes are connected to each other in rows of five.
Connect the socket built in the previous post to the breadboard.
Many LEDs only need about 20mA to light up. If too much current flows through them, they burn out. A 330 Ohm resistor is needed to limit the current flowing through the LED.
Take a 330 Ohm resistor, and connect one lead to a hole in line with pin 25 on the GPIO cable socket. The other resistor lead should be placed in a hole past the end of the socket. This page on Wikipediashows how to read resistor colour codes.
Connect a jumper cable from one of the GND pins to the negative supply rail.
Next you need to place the LED in the breadboard. LEDs only allow current to flow in one direction, so you have to get it the right way round. The negative lead is slightly shorter than the positive lead. One side of the circular rim of the LED may be flattened to show that it is the negative side. The negative lead should be inserted into the negative supply line. The LED's positive lead should be inserted into a hole in the same row as the resistor lead.
Now all you need to do is turn on pin 25 of the GPIO header. If you have installed wiringPi, you can use the 'gpio' command to control GPIO pins from the command line.
Run these commands in a terminal to test your LED:
$ gpio -g mode 25 out
$ gpio -g write 25 1
$ gpio -g write 25 0
$ gpio -g write 25 1
$ gpio -g write 25 0
You should see the LED light up when you write a '1' to pin 25, and it should go off when you write a '0'.
See also: Using Python to control GPIO
No comments:
Post a Comment