Raspberry Pi
Raspberry Pi
Raspberry Pi 3 Board
Raspberry Pi is popularly used for real time Image/Video Processing, IoT based applications
and Robotics applications.
Raspberry Pi is slower than laptop or desktop but is still a computer which can provide all the
expected features or abilities, at a low power consumption.
Raspberry Pi Foundation officially provides Debian based Raspbian OS. Also, they provide
NOOBS OS for Raspberry Pi. We can install several Third-Party versions of OS like Ubuntu,
Archlinux, RISC OS, Windows 10 IOT Core, etc.
Raspbian OS is official Operating System available for free to use. This OS is efficiently
optimized to use with Raspberry Pi. Raspbian have GUI which includes tools for Browsing,
Python programming, office, games, etc.
Raspberry Pi is more than computer as it provides access to the on-chip hardware i.e. GPIOs
for developing an application. By accessing GPIO, we can connect devices like LED, motors,
sensors, etc and can control them too.
It has ARM based Broadcom Processor SoC along with on-chip GPU (Graphics Processing
Unit).
The CPU speed of Raspberry Pi varies from 700 MHz to 1.2 GHz. Also, it has on-board SDRAM
that ranges from 256 MB to 1 GB.
Raspberry Pi also provides on-chip SPI, I2C, I2S and UART modules.
1. Raspberry Pi 1 Model A
2. Raspberry Pi 1 Model A+
3. Raspberry Pi 1 Model B
4. Raspberry Pi 1 Model B+
5. Raspberry Pi 2 Model B
6. Raspberry Pi 3 Model B
7. Raspberry Pi Zero
Out of the above versions of Raspberry Pi, more prominently use Raspberry Pi and their
features are as follows:
Raspberry Pi Zero
Raspberry Pi Board
HDMI Cable
● But, if we have a computer monitor (VGA Display), then we need an HDMI to VGA
converter along with a VGA cable for connecting Raspberry Pi with monitors. HDMI to
VGA converter and VGA cable is shown below.
HDMI to VGA Converter
VGA Cable
Now, connect the Raspberry Pi to the Display/monitor and Power-On Raspberry Pi. We will get
a Black command window asking for Login and Password as shown below
● Then, use the following login name and password
raspberrypi Login: pi
Password: raspberry
● This is the default user name and password. You can change the password after the first
login.
startx
● On display, there is a symbol of raspberry to the top-left corner of display. After clicking
on it, we will get menu as shown below,
● As we can see, the Raspbian OS has installed Python 2 & 3. It also has different
programming IDE like Geany, BlueJ Java IDE, etc. As raspberry pi 3 has On-chip WiFi,
we can connect it to the network and will get access over Internet.
● We can also change password of “Pi” user.
● To change password, click on preferences and then select Raspberry Pi
Configuration which will provide a pop-up window.
● Then, click on change password option shown below.
Now, we are quite familiar with Raspberry Pi OS.
● So, let’s write our First C code on Raspbian and execute it.
● First Create Empty file and label it with .c extension.
● Now write a small program to print “Hello World”
Program
#include<stdio.h>
int main(){
printf(“Hello World”);
return 0;
}
After writing the code, open terminal (ctrl+alt+t) to execute it. Then, type following commands for
compiling and execution.
GPIO (General Purpose Input Output) pins can be used as input or output and allows raspberry
pi to connect with general purpose I/O devices.
Some of the GPIO pins are multiplexed with alternate functions like I2C, SPI, UART etc.
Pin Numbering
We should define GPIO pin which we want to use as an output or input. But Raspberry Pi has
two ways of defining pin number which are as follows:
● GPIO Numbering
● Physical Numbering
In GPIO Numbering, pin number refers to number on Broadcom SoC (System on Chip). So, we
should always consider the pin mapping for using GPIO pin.
While in Physical Numbering, pin number refers to the pin of 40-pin P1 header on Raspberry
Pi Board. The above physical numbering is simple as we can count pin number on P1 header
and assign it as GPIO.
But, still we should consider the pin configuration diagram shown above to know which are
GPIO pins and which are VCC and GND.
Interfacing Diagram
Example
Now, let’s control LED using switch connected to the Raspberry Pi. Here, we are using Python
and C (WiringPi) for LED ON-OFF control.
Control LED using Python
Now, let’s turn ON and OFF an LED using Python on Raspberry Pi. Switch is used to control the
LED ON-OFF.
Python Program
while True:
if(GPIO.input(Switch_input)):
GPIO.output(LED,GPIO.LOW)
else:
GPIO.output(LED,GPIO.HIGH)
Functions Used:
RPi.GPIO
To use Raspberry Pi GPIO pins in Python, we need to import RPi.GPIO package which has
class to control GPIO. This RPi.GPIO Python package is already installed on Raspbian OS. So,
we don’t need to install it externally. Just, we should include library in our program to use
functions for GPIO access using Python. This is given as follows.
This function is used to define Pin numbering system i.e. GPIO numbering or Physical
numbering.
In BCM,
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
In BOARD,
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.OUT)
● GPIO as Output
GPIO.setup(channel, GPIO.OUT)
● GPIO as Input
GPIO.setup(channel, GPIO.IN)
GPIO.output(channel, state)
e.g.
GPIO.output(7, GPIO.HIGH)
GPIO.input(channel)
e.g.
GPIO.input(9)
Program
#include <wiringPi.h>
#include <stdio.h>
int LED = 26; /* GPIO26 as per wiringPi, GPIO12 as per BCM, pin no.32 */
int switch_input = 21; /* GPIO21 as per WiringPi, GPIO5 as per BCM, pin no.29 */
int main(){
wiringPiSetup(); /* initialize wiringPi setup */
pinMode(LED,OUTPUT); /* set GPIO as output */
pullUpDnControl(switch_input, PUD_UP);
while (1){
if(digitalRead(switch_input))
digitalWrite(LED,LOW); /* write LOW on GPIO */
else
digitalWrite(LED, HIGH); /* write HIGH on GPIO */
}
}