Running Python on Raspberry Pi and blinking a Led and using GPIO pins.
Python is a widely used high-level programming language that one can use to develop his own program. It has wide application in tinkering Raspberry Pi and the best tool to program and use GPIO pins of Pi and to do almost everything. By using Raspberry pi libraries for Python, the program can be structured in a proper manner without any complexity. As an example OpenCV based on python, let image processing on raspberry pi simple and effective.
For opening Python Shell with full privileges, open the command terminal and type in: (idle refer to the name of python IDE)
Python shell is an very interactive interpreter where you can check and run python commands. When a script program is made to run then the corresponding results could be showed in a python shell and thereby you can verify it. In a Python shell a user can perform simple arithmetic calculations, printing a string, string concatenation, swapping, shorting of data etc.
1Basics of Python:
Printing a Text:
Concatenation of Strings:
Looping: For Loop
Python editor save the file with .py extension, which is nothing but a python file format. We can write the corresponding program in the editor file and save it in a suitable folder. The suitable indications can be viewed in the python shell itself.
In order to get an editor file open a python shell and click on File–>New Window
3Blink a LED by Python Programming:
The above schematics shows how an Led is to be connected with GPIO pins of Raspberry Pi . Here we select pin number 7 as the output pin for giving a positive supply. You can use the other GPIO pins too in the same way and for the same purpose. Pin 7 is connected to the positive terminal of the led through a 1k resistor which is for limiting the current to avoid the damage of led. Pin number 6 is the ground pin of the raspberry pi and it is connected directly to the negative terminal of the led.
Now, power up the Raspberry Pi and connect the led to the suitable pins as shown above. Open a python shell and a python script file to write a program for led blink.
Step1: Installing RPi.GPIO Python Library
Since, we are performing the operations based on the GPIO pins, so there is a need of installing RPi.GPIO Python library. This library will gives the access to read-write the input/output pins on the Pi’s GPIO header within a Python script. By using this library we can control the GPIO header with appropriate python programs.
This library could be installed by two methods with Internet connectivity:
- First way is to install using apt-get command i.e. install from the respiratory itself. For that you need to update the available package versions by entering the command given below in the Command Terminal.
sudo apt-get update
Then install the RPi.GPIO package with the suitable command:
sudo apt-get install rpi.gpio
The installation procedures are the same as we discussed in the previous section.
- The second method is done by manually entering the suitable commands in a step by step manner.
- First is to download the RPi.GPIO Python Library with wget command.
- As it persist in an archive manner we have to extract it in to a new folder.
tar -xvf RPi.GPIO-0.5.11.tar.gz
- Change the directory to the new path using cd command. During installation process all the necessary files in the library should be accessed, so it is better to change the directory.
- There is a setup.py file in the folder which is the installation file. Run that file using sudo command.
sudo python setup.py install
- Return to the home directory by entering cdin the terminal.
After installing the library open the python editor to write the program. Enter the program as shown below:
Step 2: Save the script in any folder of your choice. After the script is saved it appears in a suitable format i.e. .py as shown below:
Step 3: Run the program and you can see that led will turn ON for one second and then it will OFF. For blinking a led for a certain period of time insert a for loop in the on-off condition.
Step 4: After editing the code as show in the above figure you will observe that the led blinks for 10 times with one second delay. In order to run this script for infinite times insert a while loop in the code. Then the led will blink continuously and to terminate this operation press Ctrl+C, which is the keyboard interrupt command.
The corresponding indication could be viewed in the python shell. An alternate method to terminate or end this process is to close the python shell and there by the program is being killed.
A question dialog box will appear when we close the python shell, asking “Do you want to kill it?” and Press “Ok” to do so.
4Using Push Button with Raspberry Pi:
Connect the push button with Pi as shown below. When the push button is pressed, it will connect pin 7 configured as an input to ground. The input pin is pulled up to 3.3V by the command;
pull_up_down=GPIO.PUD_UP in GPIO.setup
By giving this command the pull up resistor is enabled, as each GPIO pin has software configurable pull-up and pull-down resistors. When the button is pressed the false state will be returned.
Figure below shows a sample code and the indication result in the python shell tab
With some addition of code you can perform many operation based on it.
Thus, in this tutorial you learned to use the basic python commands on Pi and also to use the GIPO pins as Input/Output and interface a Led and Button, which is like the first step for making electronics project with Pi.