[vc_row][vc_column width=”1/1″][vc_column_text]Raspberry Pi has many GPIOs that can be used to control and sense the physical world in a digital way. In order to use these GPIOs, first we need to get ourselves familiar with the pin number definitions, mainly, WiringPi Pin and BCM GPIO conventions. We will also show how to program GPIOs in two great languages. Two modules from Linker kit will be used as samples to show how to connect things. In this article, we will cover two experiments: Blink a LED, and use button to control a LED.
[/vc_column_text][vc_tour][vc_tab title=”RPi GPIO Pin Definitions ” tab_id=”1388605411-1-92″][vc_column_text]Pin definition of RPi:
Two different naming conventions:
[/vc_column_text][/vc_tab][vc_tab title=”Install GPIO Library” tab_id=”1388605411-2-98″][vc_column_text]1. Update source
$ sudo apt-get update
2. Install Python
$ sudo apt-get install python-dev
3. Install python-pip (python-pip is a package that can be used to install and manage python software)
$ sudo apt-get install python-pip
4. Install rpi.gpio using pip:
$ sudo pip install rpi.gpio
To test if the installation is successful or not by:
5. Install library wringpi:
Install git-core :
$ sudo apt-get install git-core
Download winringPi:
$ git clone git://git.drogon.net/wiringPi
Build and install (There are some examples under directory /wiringPi/examples)
$ cd wiringPi
$ ./build
We can also update the library using the following commands:
$ cd wiringPi
$ git pull origin
[/vc_column_text][/vc_tab][vc_tab title=”Parts List” tab_id=”1388607025328-2-2″][vc_column_text]To do the experiments in this tutorial, we need the following parts:
- 1 x Raspberry Pi
- 1 x Linker LED
- 1 x Linker Button
- 5 x Jumper wires
[/vc_column_text][/vc_tab][vc_tab title=”Experiment 1: Blinking LED” tab_id=”1388607091029-3-2″][vc_text_separator title=”Wire Diagram” title_align=”separator_align_center”][vc_column_text]
[/vc_column_text][vc_text_separator title=”Programming with C” title_align=”separator_align_center”][vc_column_text]We are going to use library wiringpi, which has a coding style similar to that of Arduino sketch. By default, the GPIO naming convention is wiring Pi pin. If you would like to use BCM GPIO naming convention, you can replace wiringPiSetup() with wiringPiSetupGpio() in the following code. That way, you need to change led_pin to the numbering in BCM GPIO too, which is GPIO 24.
Sample Code:
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
int led_pin = 5;
int main ()
{
if (wiringPiSetup() == -1)
{
printf(“Setup wiringPi failed!”);
return 1;
}
printf(“linker_led pin : GPIO%d (wiringPi pin)\n”,led_pin);
pinMode(led_pin, OUTPUT); // set mode to output
while(1)
{
digitalWrite(led_pin, 1); // output a high level
delay(200);
digitalWrite(led_pin, 0); // output a low level
delay(200);
}
return 0;
}
1. We first use a editor to create a file and copy and paste the above content to it. We are going to use nano, and do “$nano linker_led.c”, then copy, paste and save to it.
2. Compile the code:
$ gcc linker_led.c -o linker_led -lwiringPi
3. Execute the code:
$sudo ./linker_led[/vc_column_text][vc_text_separator title=”Programming with Python” title_align=”separator_align_center”][vc_column_text]Note: Here we are going to use library RPi.GPIO. By default, the naming convention is BCM GPIO. If you would like to use WiringPi pin, we can replace GPIO.setmode(GPIO.BCM) with GPIO.setmode(GPIO.BOARD). Remember to change led_pin accordingly.
Sample Code:
import RPi.GPIO as GPIO
import time
led_pin = 24
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin,GPIO.OUT)
print “linker led pin 24 (BCM GPIO)”
while True:
GPIO.output(led_pin,True)
time.sleep(0.2)
GPIO.output(led_pin,False)
time.sleep(0.2)
We create a new file using an editor, copy and paste the sample code to it, and save.
$nano linker_led.py
Execute the code:
$sudo python linker_led.py
[/vc_column_text][/vc_tab][vc_tab title=”Results of Experiment 1″ tab_id=”1388607194922-4-8″][vc_column_text]LED OFF:
LED ON:
[/vc_column_text][/vc_tab][vc_tab title=”Experiment 2: Use button to control LED” tab_id=”1388607322336-5-2″][vc_text_separator title=”Wire Diagram” title_align=”separator_align_center”][vc_column_text]
[/vc_column_text][vc_text_separator title=”Programming with C” title_align=”separator_align_center”][vc_column_text]Note: In this experiment, we use an additional GPIO pin to read the status of a button. The function digitalRead(pin_value) is used.
WiringPin pin naming convention is used as we use wiringPiSetup() to do the setup.
Sample Code:
#include <stdlib.h>
int button_pin = 4;
int led_pin = 5;
int main ()
{
printf(“wiringPi pin\n”);
printf(“linker_led pin : GPIO%d\n”,led_pin);
printf(“linker_button pin : GPIO%d\n”,button_pin);
if (wiringPiSetup() == -1)
{
printf(“Setup wiringPi failed!”);
return 1;
}
pinMode(led_pin, OUTPUT); // set linker_led mode to output
pinMode(button_pin, INPUT); // set linker_button mode to input
while(1)
{
if( digitalRead(button_pin) )
digitalWrite(led_pin, HIGH); // output a high level
else
digitalWrite(led_pin, LOW); // output a low level
}
return 0;
}
1. Create a c file and copy and paste the above content to it, and save:
$nano linker_button.c
2: Build the code:
$ gcc linker_button.c -o linker_button -lwiringPi
3.Execute the code:
$sudo ./linker_button
[/vc_column_text][vc_text_separator title=”Progamming with Python” title_align=”separator_align_center”][vc_column_text]Note: An additional GPIO pin is used to read the status of a button. We use function GPIO.input(pin_value) to read the status.
Sample Code:
import RPi.GPIO as GPIO
led_pin = 24
button_pin = 23
GPIO.setwarnings(False)
GPIO.setmode( GPIO.BCM )
GPIO.setup( led_pin,GPIO.OUT )
GPIO.setup( button_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print (“linke r_led pin 24 , linker_button pin 23 (BCM GPIO)\n”)
while True:
if GPIO.input(button_pin):
GPIO.output(led_pin,True)
else :
GPIO.output(led_pin,False)
1. Create a py file, copy and paste the sample code above to it, and save:
$nano linker_button.py
2.Execute the code:
$sudo python linker_button.py
[/vc_column_text][/vc_tab][vc_tab title=”Results of Experiment 2″ tab_id=”1388607339869-6-8″][vc_column_text]The LED is off when the button is not pressed, and the LED will turn on when the button is pressed.
[/vc_column_text][/vc_tab][vc_tab title=”Download Sample Codes” tab_id=”1388607352072-7-8″][vc_column_text]The sample code used above can be downloaded below:
[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.