Programming in Raspberry Pi 2 using Python

Python is an advanced programming language that a user can develop his own program in a Raspberry Pi. By using suitable Raspberry Pi libraries the programs can structured in a proper manner without any complexity. For example, python based OPEN CV for raspberry pi lets a user to make the project in image processing.

For getting a python shell with full privileges, open the LX terminal and type:

sudo idle

Python shell is an interactive interpreter where a user can check python commands. When a script program is made to run then the corresponding results could be showed in a python shell and there by a user can verify it. In a Python shell a user can perform simple calculations, printing a string, string concatenation etc.

Printing a Text

String Concatenation

For Loop

Python Scripting

Python editor files will persist in the form of .py 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.

To get an editor file open a python shell and click on File — New Window

Blink a LED Using Python Script

 

The figure shown above gives a correct picture of how an Led is connected to the pins of Raspberry Pi 2. Here we select pin number 7 as the output pin for giving a positive supply. There are also other pins shows in the GPIO pinout diagram which could be used for the same purpose. Pin number 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 pi2 and it is connected directly to the negative terminal of the led.

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.

Installing Steps For  RPi.GPIO Python Library

As we are performing the operations based on the GPIO pins, 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 a proper internet connection:

  • First is to install using apt-get command ie install from the respiratory itself. For that you need to update the available package versions by entering the command given below in the LX 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.

wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.11.tar.gz

  • 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. 

cd RPi.GPIO-0.5.11

  • 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 cd in the terminal.

After installing the library open the python editor to write the program. Enter the program as shown below:


Save the script in a suitable folder. When the script is saved it appears in a suitable format as shown below:

Run the program and we can see the led will light up 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.

As the code shown above is made to run, the led will blinks for ten times with one second delay. For running this script for infinite times insert a while loop. 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 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.

By clicking on ok the program will be terminated and the python shell will be closed.

Push Button Interface With Raspberry Pi 2

Make the suitable connection as shown above.  When the switch 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. the figure given below has a sample code and the indication result in the python shell.

We can perform many operations based on this button by writing the program after reading the input state of the button.

Shop With Us 

Click here to buy Raspberry Pi 2 from RhydoLabz.

Resources

  • An open source website by Raspberry Pi foundation.
  • Get interfaced with Python programming.

FAQ

Q: Does Raspberry Pi only support Python as its programming language ?
Ans: No, any programming language which will compile for ARMv7 could be used as a programming language.

Q: Why we are entering sudo idle for opening a python shell ? Shall we open it directly ?
Ans: Yes we can. But it is recommended to open a python shell using sudo command to get full privileges to the written code.  

Q: Does your company provides any short term course for Raspberry Pi ?
Ans: Sure, we are performing short term courses and seminars in many companies and in colleges also. The Raspberry Pi basics, RPi based python programming, hardware interfaces etc will be covered.

Leave a Reply