[otw_is sidebar=otw-sidebar-1]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
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.
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
we 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.
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.
Open the terminal of the Raspberry Pi.
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.
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.
Open the Python Shell in the Raspberry Pi.
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.
Following is the circuit diagram for led blinking using the Raspberry Pi.
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.
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()
That 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.