You are on page 1of 26

In Partial Fulfilment of

The Requirements for the Course

IT Major Elective 3

Submitted by:

CRISANTO H PORNIA

Submitted to:

DARYLL A. CABAGAY
TURNING LED ON

(Activity #1)

Description

The above figure shows that the first led will turn on and last for 5 seconds, the
second led will turn on and last for 5 seconds while in the other hand the first led turns
off. The third led will turn on and last for 5 seconds while the second led turn off.
Circuit Design

import RPi.GPIO as GPIO GPIO.setup(24,GPIO.OUT)

import time print("Led ON")

GPIO.setmode(GPIO.BCM) GPIO.output(24,GPIO.HIGH)

GPIO.setwarnings(False) time.sleep(1)

print("Led off")

while(True): GPIO.output(24,GPIO.LOW)

GPIO.setup(18,GPIO.OUT)

print("Led ON") GPIO.setup(21,GPIO.OUT)

GPIO.output(18,GPIO.HIGH) print("Led ON")

time.sleep(1) GPIO.output(21,GPIO.HIGH)

print("Led off") time.sleep(1)

GPIO.output(18,GPIO.LOW) print("Led off")

GPIO.output(21,GPIO.LOW
ULTRASONIC SENSOR

(Activity #2)

Description

Above figure shows that when ultrasonic detects an object distance is less than 5
and greater than 2, led 1 will turn on while in the other hand if the ultrasonic sensor
detect an object distance less than 21 and greater than 6 the led 2 will turn on and led 1
will turn off. If the ultrasonic detects an object distance greater than 21 the led 3 will turn
on and led 2 will turn off.
Circuit Design

import RPi.GPIO as GPIO

import math TRIG = 14

import time ECHO = 18

GPIO.setmode(GPIO.BCM) GREEN = 17

GPIO.setwarnings(False) YELLOW = 27

GPIO.setup(4,GPIO.OUT) GPIO.setup(TRIG,GPIO.OUT)

GPIO.setup(18,GPIO.OUT) GPIO.output(TRIG,0)

GPIO.setup(24,GPIO.OUT) time.sleep(1)

while True: GPIO.setup(ECHO,GPIO.IN)


GPIO.output(TRIG,1) print(" " + "LED white ON")

time.sleep(0.00001) GPIO.output(4,GPIO.HIGH)

GPIO.output(TRIG,0) GPIO.output(18,GPIO.LOW)

while GPIO.input(ECHO) == 0: GPIO.output(24,GPIO.LOW)

pass time.sleep(2)

elif distance <= 21 and distance >=6:

pulse_start = time.time() print(" " + "LED green ON")

GPIO.output(4,GPIO.LOW)

while GPIO.input(ECHO) == 1: GPIO.output(18,GPIO.LOW)

pass GPIO.output(24,GPIO.HIGH)

stop = time.time(); time.sleep(2)

else:

stop = stop - pulse_start print(" " + "No LED Detection")

distance = stop * 17150 GPIO.output(4,GPIO.HIGH)

distance = int (float(distance)) GPIO.output(18,GPIO.LOW)

print("Distance:", distance, "cm") GPIO.output(24,GPIO.LOW)

GPIO.cleanup()

if distance <= 5 and distance >=2:


ULTRASONIC SENSOR WITH BUZZER

(ACTIVITY #3)

Description

The figure above shows that when ultrasonic sensor detects an object distance
greater than 0 and less than 7 the led 1 will turn on and the buzzer beeps while if the
distance is greater than 7 and less than 12 the led 2 will turn on and led 1 will turn off
and the buzzer will turn off. If the distance is greater than 13 led 3 turns on and led 2 will
turn off and buzzer will turn off.
Circuit Design

import RPi.GPIO as GPIO GPIO.setup(pin1, GPIO.OUT)


import time GPIO.setup(pin2, GPIO.OUT)
GPIO.setup(pin3, GPIO.OUT)
GPIO.setmode(GPIO.BCM) GPIO.setup(buzzer,GPIO.OUT)
GPIO.setwarnings(False)
while(True):
TRIG = 23 GPIO.output(TRIG, False)
ECHO = 24 print("Waiting For Sensor To Settle")
pin1 = 25 time.sleep(2)
pin2 = 12
pin3 = 16 GPIO.output(TRIG, True)
buzzer = 21 time.sleep(0.00001)
GPIO.output(TRIG, False)
print("Distance Measurement in progress")
GPIO.setup(TRIG, GPIO.OUT) while GPIO.input(ECHO)==0:
GPIO.setup(ECHO, GPIO.IN) pulse_start = time.time()
elif(distance > 7 and distance <= 12):
while GPIO.input(ECHO)==1: GPIO.output(pin2, GPIO.HIGH)
pulse_end = time.time() GPIO.output(pin1, GPIO.LOW)
GPIO.output(pin3, GPIO.LOW)
pulse_duration = pulse_end - pulse_start GPIO.output(buzzer,GPIO.LOW)
distance = pulse_duration * 17150 print("LED 2 ON")
distance = int(distance) elif(distance >= 13):
print("this is distance", distance) GPIO.output(pin3, GPIO.HIGH)
if (distance >=0 and distance <= 7): GPIO.output(pin1, GPIO.LOW)
GPIO.output(pin1, GPIO.HIGH) GPIO.output(pin2, GPIO.LOW)
GPIO.output(pin2, GPIO.LOW) GPIO.output(buzzer,GPIO.LOW)
GPIO.output(pin3, GPIO.LOW) print("LED 3 ON")
GPIO.output(buzzer,GPIO.HIGH) GPIO.cleanup()
print("LED 1 ON")

T-COBBLER
(Activity #4)

Description

Above figure shows that T-cobbler is connected to raspberry pi and to the breadboard.
All the pins are the connected to the pins of the breadboard where the T-cobbler is set. when
ultrasonic detects an object distance is less than 5 and greater than 2, led 1 will turn on
while in the other hand if the ultrasonic sensor detect an object distance less than 21
and greater than 6 the led 2 will turn on and led 1 will turn off. If the ultrasonic detects
an object distance greater than 21 the led 3 will turn on and led 2 will turn off.

Circuit Design
import RPi.GPIO as GPIO while True:

import math

import time TRIG = 14

ECHO = 18

GPIO.setmode(GPIO.BCM) GREEN = 17

GPIO.setwarnings(False) YELLOW = 27

GPIO.setup(4,GPIO.OUT)

GPIO.setup(18,GPIO.OUT) GPIO.setup(TRIG,GPIO.OUT)

GPIO.setup(24,GPIO.OUT) GPIO.output(TRIG,0)

time.sleep(1)

GPIO.setup(ECHO,GPIO.IN)

GPIO.output(TRIG,1)
time.sleep(0.00001) GPIO.output(4,GPIO.HIGH)

GPIO.output(TRIG,0) GPIO.output(18,GPIO.LOW)

GPIO.output(24,GPIO.LOW)

time.sleep(2)

while GPIO.input(ECHO) == 0: elif distance <= 21 and distance >=6:

pass print(" " + "LED green ON")

GPIO.output(4,GPIO.LOW)

pulse_start = time.time() GPIO.output(18,GPIO.LOW)

GPIO.output(24,GPIO.HIGH)

while GPIO.input(ECHO) == 1: time.sleep(2)

pass

stop = time.time(); else:

print(" " + "No LED Detection")

stop = stop - pulse_start GPIO.output(4,GPIO.HIGH)

distance = stop * 17150 GPIO.output(18,GPIO.LOW)

distance = int (float(distance)) GPIO.output(24,GPIO.LOW)

print("Distance:", distance, "cm")

if distance <= 5 and distance >=2: GPIO.cleanup()

print(" " + "LED white ON")

PULSE WIDTH MODULATION(PWM)


(Activity #5)

Description

Above figure shows that the three led’s turn on at the same time with different
intensity. The pins used are 2, 3 and 4. The first led Duty Cycle is 5, the second led Duty Cycle
10 and the third led Duty Cycle is 100.

Circuit Design
import RPi.GPIO as GPIO pwm_3=GPIO.PWM(23,50)

import time

pwm_1.start(5)

GPIO.setmode( GPIO.BCM) pwm_2.start(20)

GPIO.setwarnings(False) pwm_3.start(100)

GPIO.setup( 4,GPIO.OUT) try:

GPIO.setup( 18,GPIO.OUT) while True:

GPIO.setup( 23,GPIO.OUT) duty_s = raw_input("Enter")

duty = int(duty_s)

pwm_1=GPIO.PWM(4,50) pwm_1.ChangeDutyCycle(duty)

pwm_2=GPIO.PWM(18,50) time.sleep(0.5)
except KeyboardInterrupt: finally:

print "Error Occurs, Existing Program" GPIO.cleanup()

RASPBERRY PI – ARDUINO
(Activity #6)

Description

The figure above shows how to connect raspberry pi with arduino using serial
through this raspberry pi can send state to the arduino to turn on and off the led.

Circuit Design
Raspberry pi: print("LED Turn Off")

import serial Arduino Uno:


import time
int data;
a = serial.Serial('/dev/ttyACM1', 9600)
time.sleep(2) void setup(){
Serial.begin(9600);
print("Enter 1 or 0") pinMode(LED_BUILTIN,OUTPUT);
count = 0 digitalWrite(LED_BUILTIN, LOW);
while 1: }
var = input("Enter binary:") void loop(){
while(Serial.available()){
if(var == '1'): data = Serial.read();
a.write('1'.encode()) }
print("LED Turn On") if(data = '1')
time.sleep(1) digitalWrite(LED_BUILTIN, HIGH);
if(var == '2'): else if(data == '0')
a.write('0'.encode()) digitalWrite(LED_BUILTIN, LOW);

DC MOTOR
(Activity #7)

Description

The figure above shows that the DC spins forward for 5 seconds after that it spins
backwards then the DC stops.

Circuit Design
import RPi.GPIO as GPIO GPIO.setup(Motor1E,GPIO.OUT)

from time import sleep

def loop():

# Pins for Motor Driver Inputs # Going forwards

Motor1A = 24 GPIO.output(Motor1A,GPIO.HIGH)

Motor1B = 23 GPIO.output(Motor1B,GPIO.LOW)

Motor1E = 25 GPIO.output(Motor1E,GPIO.HIGH)

def setup(): sleep(5)

GPIO.setmode(GPIO.BCM) # Going backwards


# GPIO Numbering
GPIO.output(Motor1A,GPIO.LOW)
GPIO.setup(Motor1A,GPIO.OUT) # All
pins as Outputs GPIO.output(Motor1B,GPIO.HIGH)

GPIO.output(Motor1E,GPIO.HIGH)
GPIO.setup(Motor1B,GPIO.OUT)
sleep(5) if __name__ == '__main__': # Program start
from here
# Stop
setup()
GPIO.output(Motor1E,GPIO.LOW)
try:

loop()
def destroy():
except KeyboardInterrupt:
GPIO.cleanup()
destroy()

DC MOTOR WITH ULTRASONIC SENSOR


(Activity #8)

Description

The figure above shows that the program starts the DC Motor spins forward and when
the ultrasonic sensor detects a distance of greater than 10 the DC Motor spins backwards.

Circuit Design
import RPi.GPIO as GPIO # Forwards
import time GPIO.output(Motor1A, GPIO.HIGH)
GPIO.output(Motor1B, GPIO.LOW)
GPIO.setwarnings(False) GPIO.output(Motor1E, GPIO.HIGH)

TRIG = 20 while(True):
ECHO = 21 GPIO.output(TRIG, False)
Motor1A = 24 print("Waiting For Sensor To Settle")
Motor1B = 23 time.sleep(2)
Motor1E = 25
GPIO.output(TRIG, True)
def setup(): time.sleep(0.00001)
GPIO.setmode(GPIO.BCM) GPIO.output(TRIG, False)
GPIO.setup(Motor1A, GPIO.OUT)
GPIO.setup(Motor1B, GPIO.OUT) while GPIO.input(ECHO)==0:
GPIO.setup(Motor1E, GPIO.OUT) pulse_start = time.time()
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN) while GPIO.input(ECHO)==1:
pulse_end = time.time()
def loop():
pulse_duration = pulse_end - pulse_start GPIO.output(Motor1E, GPIO.HIGH)

distance = pulse_duration * 17150 #sleep(5)


# stop
distance = int(distance) #GPIO.output(Motor1E, GPIO.LOW)
print("this is distance", distance)
def destroy():
if distance > 10: GPIO.cleanup()
# Backwards
GPIO.output(Motor1A, GPIO.LOW) if _name_ == '_main_':
GPIO.output(Motor1B, GPIO.HIGH) setup()
GPIO.output(Motor1E, GPIO.HIGH) try:
else: loop()
# Forwards destroy()
GPIO.output(Motor1A, GPIO.HIGH) except KeyboardInterrupt:
GPIO.output(Motor1B, GPIO.LOW) destroy()

MIDTERM LABORATORY EXAM


(Activity #9)

Description

Above figure shows that when the program starts the cars DC Motor spins forward and
when the ultrasonic sensor detects a distance of greater than 5 the DC Motor spins backwards.
So, the car can avoid an obstacle in its way.

Circuit Design
import RPi.GPIO as GPIO GPIO.setup(TRIG, GPIO.OUT)
import time GPIO.setup(ECHO, GPIO.IN)

GPIO.setwarnings(False) def loop():


# Forwards
TRIG = 20 GPIO.output(Motor1A, GPIO.HIGH)
ECHO = 21 GPIO.output(Motor1B, GPIO.LOW)
Motor1A = 24 GPIO.output(Motor1E, GPIO.HIGH)
Motor1B = 23
Motor1E = 25
GPIO.output(Motor2A, GPIO.HIGH)
Motor2A = 14 GPIO.output(Motor2B, GPIO.LOW)
Motor2B = 15 GPIO.output(Motor2E, GPIO.HIGH)
Motor2E = 18
while(True):
def setup(): GPIO.output(TRIG, False)
GPIO.setmode(GPIO.BCM) print("Waiting For Sensor To Settle")
GPIO.setup(Motor1A, GPIO.OUT) time.sleep(2)
GPIO.setup(Motor1B, GPIO.OUT)
GPIO.setup(Motor1E, GPIO.OUT) GPIO.output(TRIG, True)
GPIO.setup(Motor2A, GPIO.OUT) time.sleep(0.00001)
GPIO.setup(Motor2B, GPIO.OUT) GPIO.output(TRIG, False)
GPIO.setup(Motor2E, GPIO.OUT)
while GPIO.input(ECHO)==0: # Forwards
pulse_start = time.time() GPIO.output(Motor1A, GPIO.HIGH)
GPIO.output(Motor1B, GPIO.LOW)
while GPIO.input(ECHO)==1: GPIO.output(Motor1E, GPIO.HIGH)
pulse_end = time.time() GPIO.output(Motor2A, GPIO.HIGH)
GPIO.output(Motor2B, GPIO.LOW)
pulse_duration = pulse_end - pulse_start GPIO.output(Motor2E, GPIO.HIGH)

distance = pulse_duration * 17150 #sleep(5)


# stop
distance = int(distance) #GPIO.output(Motor1E, GPIO.LOW)
print("this is distance", distance)
def destroy():
if distance > 10: GPIO.cleanup()
# Backwards
GPIO.output(Motor1A, GPIO.LOW) if _name_ == '_main_':
GPIO.output(Motor1B, GPIO.HIGH) setup()
GPIO.output(Motor1E, GPIO.HIGH) try:
GPIO.output(Motor2A, GPIO.LOW) loop()
GPIO.output(Motor2B, GPIO.HIGH) destroy()
GPIO.output(Motor2E, GPIO.HIGH) except KeyboardInterrupt:
else: destroy()

You might also like