Hello friends in this article I will discuss about Raspberry Pi 3 GPIO programming. This article is intended to be used as tutorial for beginners in guiding how to write the first program in Raspberry Pi. I will discuss about the programming language used for programming the Raspberry Pi, integrated development environment for writing and running the code, GPIO library for enabling the Raspberry Pi to control servo motors, leds, etc. and also for reading data from the sensors.
[otw_is sidebar=otw-sidebar-2]Raspberry Pi 3 GPIO programming
I will be using Python programming language in this tutorial as this is the most popular language used in programming the Raspberry Pi. In addition to it the integrated development environment for Python called IDLE is already installed in the Raspbian so you do not need to download any IDE to get started and another advantage of using Python is easily available packages for more advanced applications.
The latest Raspberry Pi has many features such as 1.2 GHz 64-bit Quad-Core ARM processor, 1 Giga Bit RAM, built in Bluetooth and Wi-Fi I and 40 GPIO pins that enable it to be used in the number of useful applications. For example a Raspberry Pi can be used
- For reading the data from the sensors and display.
- For controlling the electronics, motors etc thus found many applications in Robotics.
- In Internet of Things (IoT) based projects.
- As a web server
[otw_is sidebar=otw-sidebar-1]
Here only the few applications are mentioned in short Raspberry Pi can do any feats that a full-fledged computer is able to do.
Follow the following steps to program the Raspberry Pi.
1 Loading the Python IDE:
To begin the programming of the Raspberry Pi using the Python language you first need to launch the Python Programming platform where you can write the program in Python and then save it to perform the task in hand. Fortunately the Raspbian which is the default operating system for Raspberry Pi comes with already installed platform for Python that is called IDLE (integrated development and learning environment). You can launch this IDE from the Raspberry desktop. Simply follow the following steps to launch the IDE for Python.
(This article assumes that you have already installed Raspbian in raspberry Pi and had setup the Raspberry Pi by connecting the basic peripherals as keyboard, mouse and an HDMI display)
- Go at the desktop and click the Raspberry Pi button at the top left corner. (Menu)
- Click on the Programming. (Marked orange in the Picture).
- Select the Python 3 (IDE). (Marked orange in the Picture).
- When you click on the Python 3 (IDLE) the python console will open in the new window which looks like this.
[otw_is sidebar=otw-sidebar-3]
This window provides you the interface for programming the Raspberry Pi using the Python programming language. You can write your program here in the blank window and save the file in the Raspberry Pi. The program you saved in the Raspberry Pi can be run from the terminal (discussed later) directly or from this window. The figure shows you some example commands. Let’s discuss the GPIO programming in some detail.
2 Opening the new Window:
This is the first step to begin writing the code. Simple click on the file in the above image and then click on the “NEW” to launch the platform and then begin programming.
As I said earlier in this article I will be focused only on the GPIO programming.
3 Python Package for GPIO programming:
The python programming language is very powerful in that you can use it to perform various tasks.
To perform a particular task the required Python package should be installed.
The Python package for GPIO programming of the Raspberry Pi is called RPi.GPIO.
This python package is already installed in the Raspbian which is the recommended OS for Raspberry Pi. So you do not need to install the package if you are using Raspbian.
In case you are using any other OS with RPi.GPIO not installed you have to install the package first to begin programming, the details of it will be discussed in the next article.
So now I am assuming that you have the GPIO package already installed. Follow the following steps.
4 Import the Package:
In the Python console import the GPIO package to begin programming the GPIO of the Raspberry Pi. The package is imported by typing the following command.
import RPi.GPIO as GPIO
5 Specify the Numbering Style for GPIO pins:
This step can be confusing for the beginners. So bypassing the technicality just remember that there are two numbering systems used for specifying the Raspberry Pi GPIO pins. Any numbering system can be used without any problem. The name of these numbering system are
Physical numbering.
Broadcom numbering system.
In the first case i.e. Physical numbering system the GPIO pins are numbered as they appear on the board. This numbering system is identified by PIN# in the above image. We will be using this technique so let’s focus on this only. To use this numbering system you need to type the following line in the Python console.
GPIO.setmode(GPIO.BOARD)
GPIO.setmode() is used to specify either of the numbering system. If you want to use Physical numbering system type GPIO.BOARD in the parenthesis as shown above or if you want to use BCM type GPIO.BCM in the parenthesis
6) Set the GPIO as Output:
[otw_is sidebar=otw-sidebar-3]The GPIO pins of the Raspberry Pi can be used either as an input or output. You need to specify in the program whether you want to use a particular pin as an input or as an output. To set any pin as an output you need call the method by means of which you can specify a pin as an OUTPUT. To do this type the following command.
GPIO.setup(8, GPIO.OUT)
The above command will set the pin 8 as an output.
7) Set The State of pins as HIGH and LOW:
Now to change the state of the Pin 8 to high and low type the following command.
GPIO.output(8, GPIO.HIGH)
And
GPIO.output(8, GPIO.LOW)
As can be seen GPIO.output(8,GPIO.HIGH) will set the pin 8 to high and the other will set it to low.
Raspberry Pi LED BLINKING CODE:
THIS IS FIRST LED BLINKING CODE WITH Raspberry PI With the knowledge of these commands we are now ready to make our first program (Raspberry Pi LED BLINKING CODE)that will blink the led on and off. Consider the following code:
import RPi.GPIO as GPIO #for the sleep method import time led = 8 #set numbering mode for the program GPIO.setmode(GPIO.BOARD) #setup led(pin 8) as output pin GPIO.setup(led, GPIO.OUT,initial=0) #turn on and off the led in intervals of 1 second while(True): #turn on, set as HIGH or 1 GPIO.output(led,GPIO.HIGH) print(“ON”) time.sleep(1) #turn off, set as LOW or 0 GPIO.output(led, GPIO.LOW) print(“OFF”) time.sleep(1)
Here I have assumed that you have already the required wiring.
As you can see that in this code I have imported the Python package for time in the second line. This is required to enter the delay. “#” are used for adding the comments.
I hope this article will be helpful for you. In the next article I will