Getting Started With Raspberry Pi's Programming


So far, we have seen:
  1. Introduction To IoT And Supported Devices
  2. Prepare Your Raspberry PI for Your Great Ideas.
  3. Installing Raspbian Jessie On Your Raspberry Pi
In the last article, we installed Raspbian Jessie on Raspberry Pi. We will continue using this version of an operating system for this article, instead of Windows 10 IOT installation. As per the last article, we successfully logged into Jessie’s desktop. For this article, we don't need to configure anything else on Raspberry Pi. We can start the programming and play with Raspberry Pi.
In this article, we will see, how to write a basic code to control Raspberry Pi’s pins. For this article, we will use the components, given below-
  1. Raspberry Pi running Raspbian Jessie and connected to the monitor, keyboard, and mouse.
  2. Solderless Breadboard. You can find one here.
  3. Jumper cables. You can find the cables here.
  4. 5mm Led. You can find LED here.
  5. Push-button. You can find the buttons here.
  6. PIR sensor. You can find a PIR sensor here.
Let’s bring all toys and start play.
Prerequisite to start programming,
  1. We need only basic programming skills. We will use Python for our demo.
  2. Basic idea of electronic components. Basic knowledge of breadboard, how to connect components to create a circuit, using a breadboard.
  3. Raspberry Pi’s pin diagram.
None of the above skills are required, but if you have knowledge of these, it will be good.
First things first, let’s start building Hello World on Raspberry Pi. We are working with electronic components, so Hello World will be achieved by turning LED ON and OFF. For this, we need to know the pin layout of Raspberry Pi, what kind of different pins are available on Raspberry Pi’s board, and the purpose of those pins. Like some pins are used to provide +5v, +3.3 v, some pins are used only for ground purposes, some are used for input and output, etc. Layout of these pins are-
In this image, we can see two types of numbers other than 5v, 3.3v. Some reserved pins are-
  1. 1 – 40 total pins available on RPi2, 3, and zero.
  2. GPIO 2, GPIO 3 etc.
We will discuss the difference between these two types of numbers later on, during the programming.
To get started, let’s create a basic circuit, which doesn’t require any code.
Here to glow a LED, we just simply provide it a source of electricity (3.3v). This can be taken from either Pin 1 or Pin 17. In this image, you can see Blue cable is connected to Pin 1 of the RPi (Raspberry Pi), which is further connected to a resistor. You can use any resistor 1k, 50k, 10k, etc., which is connected to the positive leg of the LED, and other leg is connected to the ground pin of RPi (Pin 6). If everything is correctly connected, your LED will glow.
Controlling LED using GPIO and PYTHON code
For this, we need to connect our components in the manner, given below-
Here, the green wire is connected with any GPIO Pin. In our example, we will connect to Pin 11 and the purple wire is connected to ground pin 6. Here, the question is what is GPIO?
GPIO means general-purpose input-output. We can set (input) and get (output) values on these pins. Values will be in terms of the voltage. Now we know, we need to glow the LED and we have to set the voltage on Pin 11 to glow the LED.
To start programming with Python, we have two options-
  1. Command-line development.
  2. Python IDE.
Here, we will use the command line (terminal) to start programming with Python. For this, open terminal, type sudo nano and enter the code, given below-
  1. import RPi.GPIO as GPIO  
  2. GPIO.setmode(GPIO.BOARD)  
  3. GPIO.setup(11, GPIO.OUT)  
  4. GPIO.output(11, GPIO.HIGH)  
  5. #GPIO.cleanup()  
Things to note here are-  
  1. RPi.GPIO is a Python package, which offers a class to control GPIO on a Raspberry Pi.
  2. GPIO.setmode method is used to set the way GPIO package decides to target the pin numbers. Remember, in the above image, where we read about GPIO pins and its numbers, we had two types of Pin numbers 1-40 and the other one was GPIO2, GPIO3, etc. While calling setmode method, we are asking GPIO library to either pick pin number from 1-40 option or pick pin number from GPIO2 or GPIO3, etc. GPIO.BOARD option, referring to the pins by the number. They are available onboard (1-40) and GPIO.BCM refers to the pins with GPIO and number afterward.
  3. GPIO.setup method is used to set the mode (input or output) of the pin number or GPIO pin. In this method, pin number will be dependent on the GPIO mode we set in the previous set. If mode is GPIO.BOARD, then pin number 11 will be represented with the physical layout of the pins otherwise pin number 11 will be represented as GPIO11 (pin# 23). GPIO.OUT means, we want to set this pin to send a signal only. If we want to read the signal, then we have to set GPIO.IN.
  4. GPIO.output method is responsible for glowing our LED. To achieve this, we have to call GPIO.HIGH, which means we want to send +ve signal on pin# 11. As a result, it will glow our LED. GPIO.LOW will stop sending the signal on this pin and LED will stop glowing.
  5. RPi.GPIO provides a built-in function GPIO.cleanup() to clean up all the ports, you’ve used. Be very clear, what this does? It only affects any ports, you have set in the current program. It resets any ports, you have used in this program back to input mode. For more details about the cleanup, you can click here.
Now, click Ctrl + X, click y to confirm, and click enter. This will save the new content. Now, you are back to the terminal. Now, type Python, LED will glow.
Congratulations! You have successfully completed your first interaction with RPi GPIO.