Raspberry Pi GPIO  Python,Raspberry Pi General Purpose Input/Output Pins,

Raspberry Pi GPIO and Python 1

[otw_is sidebar=otw-sidebar-1]Raspberry Pi GPIO  Python,Raspberry Pi General Purpose Input/Output Pins,In this post I will discuss the ” Raspberry Pi GPIO and Python “General Purpose Input/Output pins of the Raspberry Pi and how to program the Raspberry Pi to use these General Purpose Input/Output pins in the systems where there is need to control the servo motors, stepper motors, DC motors, LEDs, Relays etc. We will be considering the Python language and the Python IDLE in the Raspberry Pi in order to program the General Purpose Input/Output pins

[otw_is sidebar=otw-sidebar-3]

.

Raspberry Pi GPIO and Python

In this post you will learn the basic General Purpose input/output (GPIO) programming and learn how to control the LED using the Raspberry PI and the push button. So sit back keep reading and enjoy learning.

Raspberry Pi General Purpose Input/Output Pins:

The Raspberry Pi is a very popular platform in the Internet of Things (IoT) and robotics applications. It can also handle large computations because of its very strong processor and due to the large number of General Purpose Input/Output pins the task of controlling the actuators and reading the values of the sensors become very handy and easy. So in order to use the General Purpose Input/Output (GPIO) pins it needs to be programmed obviously to serve either as input or output.

[otw_is sidebar=otw-sidebar-2]

Python which is very strong platform for programming come integrated with the Raspbian which is the recommended operating system for the Raspberry Pi. So we can use the Python shell already available in the Raspbian to program the Raspberry Pi. But before performing any task we need to make sure that the required libraries needed for supporting the Python programming are installed or not. As we have seen in the I2C and SPI serial communication

 

Getting Started With Raspberry Pi,Basics of Raspberry Pi,Raspberry Pi pinout,Raspberry Pi data sheetwe need to install additional library for Python in the Raspbian in the same way we need the GPIO library for Python to let the Python program the Raspberry. Fortunately the modern versions of the Raspbian come with readily installed GPIO library for the Python. However I will take you people through the steps for installing the Python library for GPIO programming. Before proceeding let us look at the Pin configuration of the Raspberry Pi

Raspberry Pi GPIO  Python

So in the above image you can see there are a total of 26 General Putpose Input/Output pins in the Raspberry Pi. Those represented with green colors are the standard GPIO pins. These are the pins which we use in our basic beginners project. As told earlier these pins can be configured as either Input or an Output. When configured as an input various sensors can be connected to the Raspberry Pi but it should be kept in mind that voltage greater than 3.3V will fry up your Raspberry Pi. Raspberry Pi standard GPIO pins works at 3.3V. Similarly when configured as an output Raspberry Pi delivers 3.3V to the actuators from its GPIO pins. Before we discuss the special purpose GPIO pins remember all the GPIO pins except pins 27 and 28 can be used as regular GPIO pins.

[otw_is sidebar=otw-sidebar-3]

The Raspberry Pi can support almost all of the sensors supported by other Micro-controllers as long as they work at 3.3 volts. Unlike the Arduino Raspberry Pi has only digital pins and analog pins are not present on the Raspberry Pi so you need to design the code so that your Raspberry Pi can work with analog sensors. So you can do almost anything using the Raspberry Pi. Now we will see the Python library for the GPIO programming and then we will see the code for GPIO pins of the Raspberry Pi. The following figure shows the simple circuit for reading the values from the light sensors.

Python Library for the GPIO Programming:

As mentioned in the above paragraphs that the Python library for the GPIO programming are installed by default in the modern versions of the Raspbian. In case you do not have the GPIO library follow the following steps to install the Python library for GPIO programming.

Step1:

Open the terminal of the Raspberry Pi.

Step2:

Type the following command in the terminal window:

  • sudo apt-get update
    sudo apt-get -y install python-dev python3-rpi.gpio
  • This will install the Python library for the GPIO programming. Notice that the name of our library is the RPi.GPIO. Now you will be able to write the code in the Python for GPIO programming.
  • After you have installed the Python library for the GPIO programming it is now time to write the code for blinking the LED using the Raspberry Pi GPIO pin. After this we will write the code for reading the values from the sensors through the GPIO pins.

[otw_is sidebar=otw-sidebar-3]

Raspberry Pi Python Code for LED Blinking:

In this post I will discuss the  Raspberry Pi Python Code for LED Blinking .Follow the following steps to be able to write the code for the GPIO programming of the Raspberry Pi.

Step1:

Open the Python Shell in the Raspberry Pi.

Raspberry Pi Python Code for LED Blinking:

Raspberry Pi Python Code for LED PROJECT Step2:

Now you can copy and paste the following code which I have written in the Python IDLE.

import RPi.GPIO as GPIO
import time
LedPin = 11 # pin11
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output
GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to turn on led
def blink():
while True:
GPIO.output(LedPin, GPIO.HIGH) # led on
time.sleep(1)
GPIO.output(LedPin, GPIO.LOW) # led off
time.sleep(1)
def destroy():
GPIO.output(LedPin, GPIO.LOW) # led off
GPIO.cleanup() # Release resource

 

Notice in the code above that we have imported the library that we have just installed. After this save the file remember to put the extension of “.py” at the end of the name of your file. Now you can run the file directly from the Python shell or by typing the name of your file in the terminal window of the Raspberry Pi.
Hardware:
Following is the circuit diagram for led blinking using the Raspberry Pi.

Raspberry Pi GPIO  Python,Raspberry Pi General Purpose Input/Output Pins,Raspberry Pi Python code for Reading Analog Values:

Let us now write the code for reading the values of the sensors via GPIO pins. The sensors used by the Raspberry Pi to measure the Physical Quantities can be of various types. In this I am considering the light sensor to measure the intensity of light using the Raspberry Pi.
Step1:

Copy and paste the following in the Python shell and in the same way did before save the file and run it either through the Python shell or by typing the name of your file in the Terminal Window of the Raspberry Pi.

import RPi.GPIO as GPIO
import time
pin_to_circuit = 7
GPIO.setmode(GPIO.BOARD)
def rc_time (pin_to_circuit):
count = 0

#Output on the pin for
GPIO.setup(pin_to_circuit, GPIO.OUT)
GPIO.output(pin_to_circuit, GPIO.LOW)
time.sleep(0.1)

#Change the pin back to input
GPIO.setup(pin_to_circuit, GPIO.IN)

#Count until the pin goes high
while (GPIO.input(pin_to_circuit) == GPIO.LOW):
count += 1

return count

#Catch when script is interrupted, cleanup correctly
try:
# Main loop
while True:
print rc_time(pin_to_circuit)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()

 

Hardware:

Raspberry Pi GPIO and Python,Raspberry Pi GPIO Python PROJECTThat is all for now. I hope this article would be helpful for you. In the next post I will discuss the Ultrasonic sensor interfacing with the Raspberry Pi. Till then stay connected keep reading and enjoy learning.

One Comment

  1. Hi, It has come to our attention that you are using our client’s photographs on your site without a valid licence. We have already posted out all supporting documents to the address of your office. Please confirm once you have received them. In the meantime, we would like to invite you to settle this dispute by making the below payment of £500. Visual Rights Group Ltd, KBC Bank London, IBAN: GB39 KRED 1654 8703, 1135 11, Account Number: 03113511, Sort Code: 16-54-87 Once you have made the payment, please email us with your payment reference number. Please note that a failure to settle at this stage will only accrue greater costs once the matter is referred to court. I thank you for your cooperation and look forward to your reply. Yours sincerely, Visual Rights Group Ltd, Company No. 11747843, Polhill Business Centre, London Road, Polhill, TN14 7AA, Registered Address: 42-44 Clarendon Road, Watford WD17 1JJ

Leave a Reply

Your email address will not be published. Required fields are marked *