You are on page 1of 11

Lab Handout #8: Introduction to Raspberry Pi and accessing GPIOs

Instructor: Dr. Afaque Manzoor Soomro

Name:UNSA JAN
CMS ID: 033-19-0048
Semester: VII

Department of Electrical Engineering


TEL-413: Introduction to Robotics
Rubrics for Lab Task Assessment

Rubrics for Marks Breakdown


Demonstration
Design Manual Total (Out of 100)
Manual

Criteria 100% 70% 30% 0%


Design (40%) Designed as per Partially designed Not designed as Code not
instructions. as per instructions. per instructions. submitted.
Demonstration Successfully Successfully Demonstrated Failed to
(40%) demonstrated the demonstrated with with Major demonstrate the
task as intended. minor mistakes. mistakes. project.
Manual (20%) The manual The manual The manual not The manual not
submitted on time submitted on time submitted on time submitted.
with proper without proper but with proper
solution code. solution code. solution.
Lab Objective: Install OS on Raspberry Pi and Write a program to access the GPIOs

Required Components:
1. Raspberry Pi
2. Micro SD card
3. USB Card reader
4. HDMI Cable
5. Monitor with HDMI port
6. Keyboard
7. Mouse
8. USB-Micro USB cable
9. Mobile Robot Chassis with motors
10. Battery
11. Motor Driver circuit board
12. LED and a resistor
13. Connecting wires

Raspberry Pi is low-cost single-board Linux computer developed by Raspberry foundation(UK). It


is a ARM11 (Broadcom 2835) based development board or a computer which is able to perform
operations similar to a PC as well as a controller. The graphics capabilities and HDMI video output
make it suitable for multimedia applications. With access to the internet, through Ethernet or Wi-Fi
(with a USB dongle in some cases) and a high definition output, the Raspberry Pi is an incredibly
versatile piece of computing kit. The Raspberry Pi 3 is based on a Broadcom BCM2835 chip that is
based on ARMv6 architecture. It does not have built-in hard disk or solid-state drive, instead a
micro SD card is used booting and long-term storage. This single board computer also comes with
digital I/O ports and enough CPU power to accomplish different projects. Programming language
Python can be used to access the digital I/O pins, referred to as General Purpose Input Output Pins
(GPIOs).
GPIOs:
Raspberry Pi GPIOs are digital pins that can be used for input and output purpose. It should be
noted that these GPIO deliver logic “HIGH “ at 3.3V rather than 5V. Similarly the GPIOs accept
3.3V as input logic “HIGH”. Apart from that, different pins have various functions as described
under.

 PWM (pulse-width modulation)


◦ Software PWM available on all pins
◦ Hardware PWM available on GPIO12, GPIO13, GPIO18, GPIO19
 SPI
◦ SPI0: MOSI (GPIO10); MISO (GPIO9); SCLK (GPIO11); CE0 (GPIO8), CE1 (GPIO7)
◦ SPI1: MOSI (GPIO20); MISO (GPIO19); SCLK (GPIO21); CE0 (GPIO18); CE1
(GPIO17); CE2 (GPIO16)
 I2C
◦ Data: (GPIO2); Clock (GPIO3)
◦ EEPROM Data: (GPIO0); EEPROM Clock (GPIO1)
 Serial
◦ TX (GPIO14); RX (GPIO15)

Operating System (OS) Installation:

As mention, Raspberry Pi is a single-board computer so it needs OS. To install an OS we need to


follow the steps given below:

1. Download NOOBs (New Out Of the Box Software) an straightforward way to install an
operating system on Pi from https://www.raspberrypi.org/downloads/noobs/.
2. Insert the USB card reader (with SD card) in the USB port of the PC.
3. Copy extracted contents of “NOOB.zip” file on SD card.
4. Insert the SD card in Raspberry Pi.
5. Connect HDMI cable between a monitor and the Raspberry Pi. Also connect a mouse and
keyboard with the Pi.
6. Power on Pi using USB-Micro USB cable.
7. Follow the installation process on Monitor to install the Raspbian OS.

Accessing GPIOs:
In order to access (read/write) GPIOs we ned to write a program file in Python within the Raspberry
and execute it. Perform following steps to make an LED blink using Raspberry Pi.
1. Turn on the Raspberry Pi and make a connection with an LED as shown in figure.
Note that the Anode of the LED is connected with Pin no. 8 through a resistor.
2. Create a new file on Desktop with name “LED_blink.py”
3. Copy and Paste following code in the file.
4. Open terminal and write this command “chmod +x /Desktop/LED_blink.py”
This make the file executable.
5. Run this command on terminal. “./Desktop/LED_blink.py”
________________________________________________________________________________
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from time import sleep # Import the sleep function from the time module
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(12, GPIO.OUT, initial=GPIO.LOW) # Set pin 12 to be an output pin and set initial
value to low (off)

while True: # Run forever


GPIO.output(12, GPIO.HIGH) # Turn on
sleep(1) # Sleep for 1 second
GPIO.output(12, GPIO.LOW) # Turn off
sleep(1) # Sleep for 1 second
________________________________________________________________________________
PWM can be used to control the speed of the robot and brightness of the LED. Following lines of
code are need to be added to use PWM and follow the steps as above.
________________________________________________________________________________
.
p = GPIO.PWM(12,100) #GPIO12 as PWM output, with 100Hz frequency
p.start(0) #Generate PWM signal with 0% duty cycle
.
.
while 1: #execute loop forever
for x in range (50): #execute loop for 50 times, x being incremented from 0 to 49.
p.ChangeDutyCycle(x) #change duty cycle for varying the brightness of LED.
time.sleep(0.1) #sleep for 100m second
________________________________________________________________________________
Practice Task: Write an program to run the mobile robot Forward and Backward direction for
20 seconds each.
Code
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library from time import sleep
#Import the sleep function from the time module
GPIO.setwarnings(False)# ignore warning for now

GPIO.setmode(GPIO.BOARD)# use physical pin numbering


GPIO.setup(10,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(12,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(22,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(16,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(32,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(33,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
El = GPIO.PWM(32, 100) # PWM
Er = GPIO.PWM(33, 100) # PWM
El.start(0)
Er.start(0)
#p.start(100) #Generate PWM signal with 0% duty cycle
while True:#Run forever
#Drive Farword for 5 seconds at full speed
El.ChangeDutyCycle(100)
Er.ChangeDutyCycle(100)
GPIO.output(10, GPIO.HIGH);
GPIO.output(12, GPIO.LOW);
GPIO.output(22, GPIO.HIGH);
GPIO.output(16, GPIO.LOW);
sleep(20)
GPIO.output(10, GPIO.LOW);
GPIO.output(12, GPIO.HIGH);
GPIO.output(22, GPIO.LOW);
GPIO.output(16, GPIO.HIGH);
sleep(20)
Exercise Tasks:
1. Program the robot to move straight for 5 seconds in full speed then slow downs, makes u-turn
and then moves straight for 5 seconds in full speed.
Code
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library from time import sleep
#Import the sleep function from the time module
GPIO.setwarnings(False)# ignore warning for now

GPIO.setmode(GPIO.BOARD)# use physical pin numbering


GPIO.setup(10,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(12,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(22,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(16,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(32,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(33,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
El = GPIO.PWM(32, 100) # PWM
Er = GPIO.PWM(33, 100) # PWM
El.start(0)
Er.start(0)
#p.start(100) #Generate PWM signal with 0% duty cycle
while True:#Run forever
#Drive Farword for 5 seconds at full speed
El.ChangeDutyCycle(100)
Er.ChangeDutyCycle(100)
GPIO.output(10, GPIO.HIGH);
GPIO.output(12, GPIO.LOW);
GPIO.output(22, GPIO.HIGH);
GPIO.output(16, GPIO.LOW);
#slow Down
sleep(5)
for x in range (100, 25, -1):
El.ChangeDutyCycle(x)
Er.ChangeDutyCycle(x)
sleep(0.1)

#U-Turn
El.ChangeDutyCycle(25)
Er.ChangeDutyCycle(100)
GPIO.output(10, GPIO.HIGH);
GPIO.output(12, GPIO.LOW);
GPIO.output(22, GPIO.HIGH);
GPIO.output(16, GPIO.LOW);
sleep(2)
2. Make the robot move forward for 2 seconds and then make a random radial turn slowly.
Code
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import random
#from time import sleep Import the sleep function from the time module
GPIO.setwarnings(False)# ignore warning for now

GPIO.setmode(GPIO.BOARD)# use physical pin numbering


GPIO.setup(10,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set
initial value to low (OFF)
GPIO.setup(12,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set
initial value to low (OFF)
GPIO.setup(22,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set
initial value to low (OFF)
GPIO.setup(16,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set
initial value to low (OFF)
GPIO.setup(32,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set
initial value to low (OFF)
GPIO.setup(33,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set
initial value to low (OFF)
El = GPIO.PWM(32, 100) # PWM
Er = GPIO.PWM(33, 100) # PWM
El.start(0)
Er.start(0)
#p.start(100) #Generate PWM signal with 0% duty cycle
while True:#Run forever
#Drive Farword for 5 seconds at full speed
El.ChangeDutyCycle(100)
Er.ChangeDutyCycle(100)
GPIO.output(10, GPIO.HIGH);
GPIO.output(12, GPIO.LOW);
GPIO.output(22, GPIO.HIGH); #Left Wheel
GPIO.output(16, GPIO.LOW); #Left Wheel
sleep(2)

num = random.randint(0,9)
print(num)

#Random Right
if num<=5:
print('Random RIGHT')

El.ChangeDutyCycle(30)

GPIO.output(10, GPIO.LOW);
GPIO.output(12, GPIO.LOW);
sleep(2)

#Random Left
if num>=6:
print('Random LEFT')
Er.ChangeDutyCycle(30)

GPIO.output(22, GPIO.LOW); #Left Wheel


GPIO.output(16, GPIO.LOW); #Left Wheel

sleep(2)
3. Make the robot move forward for 2 seconds and then make a random axial turn slowly.
Code
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import random
#from time import sleep Import the sleep function from the time module
GPIO.setwarnings(False)# ignore warning for now

GPIO.setmode(GPIO.BOARD)# use physical pin numbering


GPIO.setup(10,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(12,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(22,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(16,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(32,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
GPIO.setup(33,GPIO.OUT,initial=GPIO.LOW)# set pin 12 to be an output pin and set initial
value to low (OFF)
El = GPIO.PWM(32, 100) # PWM
Er = GPIO.PWM(33, 100) # PWM
El.start(0)
Er.start(0)
#p.start(100) #Generate PWM signal with 0% duty cycle
while True:#Run forever
#Drive Farword for 5 seconds at full speed
El.ChangeDutyCycle(100)
Er.ChangeDutyCycle(100)
GPIO.output(10, GPIO.HIGH);
GPIO.output(12, GPIO.LOW);
GPIO.output(22, GPIO.HIGH); #Left Wheel
GPIO.output(16, GPIO.LOW); #Left Wheel
sleep(2)

num = random.randint(0,9)
print(num)

#Random Right AXIAL


if num<=5:
print('Random RIGHT')

El.ChangeDutyCycle(30)
Er.ChangeDutyCycle(30)

GPIO.output(22, GPIO.HIGH); #Left Wheel


GPIO.output(16, GPIO.LOW); #Left Wheel
GPIO.output(10, GPIO.LOW); #Right Wheel
GPIO.output(12, GPIO.HIGH); #Right Wheel
sleep(2)

#Random Left AXIAL


if num>=6:
print('Random LEFT')
Er.ChangeDutyCycle(30)
El.ChangeDutyCycle(30)

GPIO.output(22, GPIO.LOW); #Left Wheel


GPIO.output(16, GPIO.HIGH); #Left Wheel
GPIO.output(10, GPIO.HIGH); #Right Wheel
GPIO.output(12, GPIO.LOW); #Right Wheel
sleep(2)
Exercise Question: How easy/difficult it is to work with a Raspberry Pi than an ESP board?
Answer: ESP board is easy and simpler than Raspberry Pi because Raspberry Pi based on Linux
operating system and it is a bit complex. For working on Raspberry Pi one should learn the interface
of Linux and python language.

Note: Use H-Bridge to interface Pi GPIOs with the robot chassis motors. Raspberry Pi 4 has
different GPIO configuration than Pi 3. Please refer Raspberry Pi 4 manual for Pin configuration.
Rubrics for Lab Task Assessment

Rubrics for Marks Breakdown


Task Completion Manual Submission Total (Out of 10)

80% 20 % 100 %

Criteria Accurately Partially None


Design (40%) Designed as per Designed as per Not designed as per
instructions instructions instructions
Demonstration (40%) Successfully Successfully Failed to demonstrate
demonstrated and demonstrated but failed the project.
answered the asked to answer the asked
questions during the questions during the
demonstration. demonstration.
Manual (20%) The manual submitted The manual submitted The manual not
on time with the on time without the submitted.
solution code. solution code.

You might also like