CMPT401_TWU

Lab Report 8

14-11-2023

Jonah Watts

Table Of Contents:

  1. Assignment Project #1
  2. Assignment Project #2

Assignment Project #1

Task Description:

The goal of this assignment is to make an anti-burglar device, by using the infrared motion detector to see when there is movement. When movement is detected, the buzzer will play a tone, and the LED bar will light up. When there is no movement, the system will return to an inactive status.

Hardware Components:

Circut Diagrams

Photo(s) of the breadboard:

Source Code

The Source code can be downloaded here here

from machine import Pin, PWM
from myservo import myServo
import neopixel
import time
import math 
pin = Pin(2, Pin.OUT)
np = neopixel.NeoPixel(pin, 8)
servo=myServo(15)

sensorPin=Pin(33,Pin.IN) #PIR sensor
PI=3.14
passiveBuzzer=PWM(Pin(12),2000)

brightness=200                                
colors=[[brightness,0,0],                    #red
        [0,brightness,0],                    #green
        [0,0,brightness],                    #blue
        [brightness,brightness,brightness],  #white
        [0,0,0]]                             #close
    

try:
    while True:
      if sensorPin.value():
        passiveBuzzer.duty(50)     
        servo.myServoWriteAngle(int(160))
        for i in range(0,4):
            for j in range(0,8):
                sinVal=math.sin(j*i*10*PI/180)
                toneVal=2000+int(sinVal*500)
                passiveBuzzer.freq(toneVal)
                np[j]=colors[i]
                np.write()
                time.sleep_ms(50)
        servo.myServoWriteAngle(int(140))
        time.sleep_ms(200)
        
      else:
        passiveBuzzer.duty(0)
        for j in range(0,8):
                np[j]=colors[4]
                np.write()
                time.sleep_ms(50)
except:
    pass

Video of program in action


Assignment Project #2:

Task Description:

The task of this assignment is to use the gyroscope to detect which way that someone is falling. Each direction emmits a tone, and displays a message on the LCD screen. When there is no movement, the system will return to an inactive status.

Hardware Components

Circut Diagrams

Photo(s) of the breadboard

Source Code

The Source code can be downloaded here here

from mpu6050 import MPU6050
import time
from machine import Pin, PWM, I2C
from I2C_LCD import I2cLcd
 
mpu=MPU6050(4,0) #attach the IIC pin(sclpin,sdapin)
mpu.MPU_Init()     #initialize the MPU6050
G = 9.8
time.sleep_ms(1000)#waiting for MPU6050 to work steadily

#setup the buzzer
notes={ 
        "C":2093,
        "D":2349,
        "E":2637,
        "F":2793
        }
#setup the buzzer
passiveBuzzer=PWM(Pin(12),2000)
passiveBuzzer.init()
passiveBuzzer.duty(0)

i2c = I2C(scl=Pin(14), sda=Pin(13), freq=400000)
devices = i2c.scan()
if len(devices) == 0:
    print("No i2c device !")
else:
    for device in devices:
        print("I2C addr: "+hex(device))
        lcd = I2cLcd(i2c, device, 2, 16)

try:
    while True:
        accel=mpu.MPU_Get_Accelerometer()#gain the values of Acceleration
        gyro=mpu.MPU_Get_Gyroscope()     #gain the values of Gyroscope
        time.sleep_ms(500)

        if accel[0]/16384 > 0.3:
            passiveBuzzer.freq(notes["C"])
            passiveBuzzer.duty(50)
            lcd.move_to(0, 0)
            lcd.putstr("Falling Left")
        elif accel[1]/16384 > 0.3:
            passiveBuzzer.freq(notes["D"])
            passiveBuzzer.duty(50)
            lcd.move_to(0, 0)
            lcd.putstr("Falling Backward")
        elif accel[0]/16384 < -0.3:
            passiveBuzzer.freq(notes["E"])
            passiveBuzzer.duty(50)
            lcd.move_to(0, 0)
            lcd.putstr("Falling Right")
        elif accel[1]/16384 < -0.3:
            passiveBuzzer.freq(notes["F"])
            passiveBuzzer.duty(50)
            lcd.move_to(0, 0)
            lcd.putstr("Falling Forward")
        else:
            passiveBuzzer.duty(0)
            print("no movement")
            lcd.move_to(0, 0)
            lcd.clear()
            lcd.putstr("no movement")    
except:
    pass

Video of program in action