Practical Programs - Writeup
Practical Programs - Writeup
PRACTICAL DOCUMENTATION:
This is the Broadcom chip used in the Raspberry Pi 3, and in later models of the
Raspberry Pi 2.
The only significant difference is the replacement of the ARMv7 quad core cluster
with a quad-core ARM Cortex A53 (ARMv8) cluster.
The ARM cores run at 1.2GHz, making the device about 50% faster than the
Raspberry Pi 2. The VideocoreIV runs at 400Mhz.
The Raspberry Pi 2's chip BCM2836 and the Raspberry Pi 1's chip BCM2835
PIN DETAILS OF RPI
Practical No#1:
Linux commands : <<Write commands using the following link: >>
https://www.raspberrypi.org/documentation/linux/usage/commands.md
Practical No.#2:
Write a program to blink LED connected to GPIO 21
General Diagram:
SOLUTION:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(20,GPIO.OUT)
while True:
GPIO.output(20,1)
time.sleep(0.2)
GPIO.output(20,0)
time.sleep(0.2)
SOLUTION:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(20,GPIO.OUT)
GPIO.setup(21,GPIO.OUT)
while True:
GPIO.output(20,1)
time.sleep(0.2)
GPIO.output(20,0)
time.sleep(0.2)
GPIO.output(21,1)
time.sleep(0.2)
GPIO.output(21,0)
time.sleep(0.2)
Practicals No.#4:
Write a program to detect switch closure , connected to RPI GPIO 16
& GPIO 19
GPIO 16 GPIO 19
SOLUTION:
reading1= GPIO.input(16)
if reading1 == 0:
print "SECOND BUTTON Pressed"
time.sleep(0.2)
Practicals No.#5:
Write a program to detect switches closure ,connected to RPI pins
GPIO 16 & GPIO 19
& also BLINK LEDS CONNECTED TO GPIO 20 & GPIO 21.
reading1= GPIO.input(16)
if reading1 == 0:
print "SECOND BUTTON Pressed"
GPIO.output(20,1)
time.sleep(0.2)
GPIO.output(20,0)
time.sleep(0.2)
GPIO.output(21,1)
time.sleep(0.2)
GPIO.output(21,0)
time.sleep(0.2)
Practicals No.#6:
Write a program to generate Sound of different pitch and duration
using buzzer
#NOTE: Use GPIO 13 ---- FOR BUZZER
Practicals No.#7:
Write a program to CAPTURE AN IMAGE using camera module interface.
Settings:
1. Install the Raspberry Pi Camera module by inserting the cable into the Raspberry Pi.
2. The cable slots into the connector situated between the Ethernet and HDMI ports, with
the silver connectors facing the HDMI port.
3. boot up raspberry-pi
4. From the prompt, run "sudo raspi-config". If the "camera" option is not listed, you will
need to run a few commands to update your Raspberry Pi. Run "sudo apt-get update" and
"sudo apt-get upgrade"
5.
6.
7. Run "sudo raspi-config" again - you should now see the "camera" option
Navigate to the "camera" option, and enable it. Select “Finish” and reboot your Raspberry Pi.
from picamera import PiCamera
from time import sleep
camera =PiCamera()
camera.resolution =(1024,768)
camera.start_preview()
sleep(2)
camera.capture('test_photo.jpg')
Practicals No.#8:
Write a program to RECORD a video using camera module interface.
import picamera
camera =picamera.PiCamera()
camera.resolution =(640,480)
camera.start_recording('test_video.h264')
camera.wait_recording(5)
camera.stop_recording()
print('finished')
To view the video (Created in the folder in which the program is run), use the following
command at the Pi command-prompt.
Omxplayer ‘\pi\home\progs\test_video.h264’
Practicals No.#9:
Write a program to change intensity of LED using Pulse Width
Modulation (PWM)
Practical No#10
Glowing the LED connected to pin no#21 using Node-Red
i.
j. Double-click on the inject node. Use the drop down next
to Payload to change the data type to string and type 1 in the
Payload box - this will be the message. Type On in the Name box.
Press Done.
k. Similarly, create one for OFF.
l. Create flows and it should look something like shown above : with
both on and off connected to the GPIO pin interface node labeled
here as Green LED.
m. Our flow is finished, so we can deploy it. Click on the big
red Deploy button on the top right of the screen. A message
should pop up at the top saying “Successfully deployed”. This is
similar to pressing the green flag in Scratch or F5 to run your code
in Python.
n. Now click on the blue square on the left of the On node to inject
the message 1. The Green LED node receives the message and
your LED should light up. You should be able to turn the LED off
by clicking the blue square on the Off node, which injects the
message 0.
Practicals No.#11:
Explain i2c protocol and detect address map of i2c devices
Command to detect i2c devices in RPI
Reboot your Pi
$ sudo i2cdetect –y 1
Stepper Motor
Used In Printers
Practicals No.#10:
Write a program to rotate stepper motor in clockwise direction
Windings are connected to # GPIO17,GPIO18 ,GPIO22 ,GPIO23
import time
import RPi.GPIO as GPIO
GPIO.cleanup()
#cleaning up in case GPIOS have been preactivated
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# be sure you are setting pins accordingly
# GPIO17,GPIO18,GPIO22,GPIO23
StepPins = [17,18,22,23]
# Set all pins as output
for pin in StepPins:
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)
#wait some time to start
time.sleep(0.5)
#Full torque
StepCount3 = 4
Seq3 = []
Seq3 = [3,2,1,0]
Seq3[0] = [0,0,1,1]
Seq3[1] = [1,0,0,1]
Seq3[2] = [1,1,0,0]
Seq3[3] = [0,1,1,0]
# set
Seq = Seq2
StepCount = StepCount2
StepCounter += 1
# If we reach the end of the sequence
# start again
if (StepCounter==StepCount):
StepCounter = 0
if (StepCounter<0):
StepCounter = StepCount
# Wait before moving on
time.sleep(WaitTime)
except:
GPIO.cleanup();
finally: #cleaning up and setting pins to low again (motors can get hot if you wont)
GPIO.cleanup();
for pin in StepPins:
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin, False)
-------------------------------------------------------------------------------------------------------------------