CMPT401_TWU

Lab Report 7

06-11-2023

Jonah Watts

Table Of Contents:

  1. Lab 21.1: Ultrasonic Ranging
  2. Lab 21.2: Ultrasonic Ranging
  3. Lab 22.1: Matrix Keypad
  4. Lab 22.2: Matrix Door Lock
  5. Lab 23.1: Infrared Remote Control
  6. Lab 23.2: LED Control Through Infrared Remote
  7. Lab 24.1: Hygrothermograph
  8. Lab 24.2: Hygrothermograph
  9. Assignment Project #1
  10. Assignment Project #2

Lab 21.1: Ultrasonic Ranging

Lab 21.2: Ultrasonic Ranging

Lab 22.1: Matrix Keypad

Lab 22.2: Matrix Door Lock

Lab 23.1: Infrared Remote Control

Lab 23.2: LED Control Through Infrared Remote

Lab 24.1: Hygrothermograph

Lab 24.2: Hygrothermograph


Assignment Project #1

Task Description:

The goal of this assignment is to make a distance reactive sound and light machine, where there is a ultrasonic sensor, and by holding your hand at different disatances from the sensor, you are able to trigger the different noises (do-re-mi-fa-so-la-ti-do) and will light up a light bar to indicate the distance.

Hardware Components:

Circut Diagrams

Photo(s) of the breadboard:

Source Code

The Source code can be downloaded here here

import time
from machine import Pin, PWM

pins=[15,2,0,4,5,18,19,21,22,23]

#setup the buzzer
passiveBuzzer=PWM(Pin(27),2000)
passiveBuzzer.init()
passiveBuzzer.duty(0)

#setup the ultrasonic sensor
trigPin=Pin(13,Pin.OUT,0)
echoPin=Pin(12,Pin.IN,0)
soundVelocity=340
distance=0

currentled = 16

#Init the notes used
notes={ 
        "B6":1975,
        "C":2093,
        "C#":2217,
        "D":2349,
        "D#":2489,
        "E":2637,
        "F":2793,
        "F#":2959,
        "G":3135,
        "G#":3322,
        "A":3520,
        "A#":3729,
        "B":3951,
        "C8":4186,
        "C#8":4435,
        "D8":4699,
        "D#8":4978,
        "E8":5274,
        "F8":5587,
        "F#8":5919,
        "G8":6271,
        "G#8":6644,
        "A8":7040,
        "A#8":7458,
        "B8":7902,
        }

#function to get the distance from the ultrasonic sensor
def getSonar():
    trigPin.value(1)
    time.sleep_us(10)
    trigPin.value(0)
    while not echoPin.value():
        pass
    pingStart=time.ticks_us()
    while echoPin.value():
        pass
    pingStop=time.ticks_us()
    pingTime=time.ticks_diff(pingStop,pingStart)
    distance=pingTime*soundVelocity//2//10000
    return int(distance)

#Show the correstponding LED 
def showled(led):                 
    length=len(pins)        
    currentled = led
    for i in range(0,length):
        leds=Pin(pins[i],Pin.OUT)
        leds.value(0)
    led=Pin(pins[led],Pin.OUT)
    led.value(1)
    

#Main program
time.sleep_ms(2000)
passiveBuzzer.duty(50)
while True:
    x = getSonar()
    time.sleep_ms(200)
    print('Distance: ',x,'cm' )
    #Depending on the distance, play the corresponding note, and change the LED if needed,
    if x<4:
        print("0")
        passiveBuzzer.freq(notes["C"])
        showled(0)
    elif x<8:
        print("1")
        showled(1)
        passiveBuzzer.freq(notes["D"])
    elif x<12:
        print("2")
        showled(2)
        passiveBuzzer.freq(notes["E"])
    elif x<16:
        print("3") 
        showled(3)
        passiveBuzzer.freq(notes["F"])
    elif x<20:
        print("4")
        showled(4)
        passiveBuzzer.freq(notes["G"])
    elif x<24:
        print("5")
        showled(5)
        passiveBuzzer.freq(notes["A"])
    elif x<28:
        print("6")
        showled(6)
        passiveBuzzer.freq(notes["B"])
    elif x<32:
        print("7")
        showled(7)
        passiveBuzzer.freq(notes["C8"])

Video of program in action


Assignment Project #2:

Task Description:

The task of this assignment is to control a variety of different devices attached to the ESP32 by using the infrared remote, where different buttons on the remote will trigger different events, such as a motor moving, a LCD screen changing message, a buzzer playing a tone, and a neo pixel changing color.

Hardware Components

Circut Diagrams

Photo(s) of the breadboard

Source Code

The Source code can be downloaded here here

from irrecvdata import irGetCMD
from machine import I2C, Pin, PWM
from I2C_LCD import I2cLcd
from myservo import myServo
import neopixel
import time

#setup the buzzer
notes={ 
        "B6":1975,
        "C":2093,
        "C#":2217,
        "D":2349,
        "D#":2489,
        "E":2637,
        "F":2793,
        "F#":2959,
        "G":3135,
        "G#":3322,
        "A":3520,
        "A#":3729,
        "B":3951,
        "C8":4186,
        "C#8":4435,
        "D8":4699,
        "D#8":4978,
        "E8":5274,
        "F8":5587,
        "F#8":5919,
        "G8":6271,
        "G#8":6644,
        "A8":7040,
        "A#8":7458,
        "B8":7902,
        }
#setup the buzzer
passiveBuzzer=PWM(Pin(27),2000)
passiveBuzzer.init()
passiveBuzzer.duty(0)


#setup neopixel
pin = Pin(2, Pin.OUT)
np = neopixel.NeoPixel(pin, 8)
#brightness :0-255
brightness=10                                

#10 differnt colors for the neopixel
colors=[
    [200,200,200], #white
    [200,0,0],     #red
    [0,200,0],     #green
    [0,0,200],     #blue
    [200,200,0],   #yellow
    [200,0,200],   #magenta
    [0,200,200],   #cyan
    [200,100,0],   #orange
    [200,0,100],   #pink
    [100,0,200],   #purple
    [0,0,0],       #off
]   

#setup the screen
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)

#Setup the servao
servo=myServo(0)#set servo pin
servo.myServoWriteAngle(0)#Set Servo Angle
time.sleep_ms(1000)

    
recvPin = irGetCMD(15)
try:
    lcd.move_to(0, 0)
    lcd.putstr("Lab 7 Project 2")
    count = 0
    while True:
        irValue = recvPin.ir_read()
        if irValue:
            print(irValue)
        if irValue == "0xffa25d":
            lcd.move_to(0, 1)
            lcd.putstr("Power")
            servo.myServoWriteAngle(0)
            for j in range(0,8):
                np[j]= [0,0,0]
                np.write()
                time.sleep_ms(50)
        if irValue == "0xffe21d":
            lcd.move_to(0, 1)
            lcd.putstr("Menu       ")
        elif irValue == "0xff22dd":
            lcd.move_to(0, 1)
            lcd.putstr("Test        ")
        elif irValue == "0xff02fd":
            lcd.move_to(0, 1)
            lcd.putstr("Plus          ")
        elif irValue == "0xffc23d":
            lcd.move_to(0, 1)
            lcd.putstr("Return          ")
        elif irValue == "0xffe01f":
            lcd.move_to(0, 1)
            lcd.putstr("Back         ")
        elif irValue == "0xffa857":
            lcd.move_to(0, 1)
            lcd.putstr("Play         ")
        elif irValue == "0xff906f":
            lcd.move_to(0, 1)
            lcd.putstr("Forward        ")
        elif irValue == "0xff9867":
            lcd.move_to(0, 1)
            lcd.putstr("Minus          ")
        elif irValue == "0xff6897":
            lcd.move_to(0, 1)
            lcd.putstr("Key Zero            ")
            for j in range(0,8):
                np[j]= colors[0]
                np.write()
                time.sleep_ms(50)
            passiveBuzzer.freq(notes["C"])
            passiveBuzzer.duty(50)
            time.sleep_ms(500)
            passiveBuzzer.duty(0)
            servo.myServoWriteAngle(0)
        elif irValue == "0xff30cf":
            lcd.move_to(0, 1)
            lcd.putstr("Key One        ")
            for j in range(0,8):
                np[j]= colors[1]
                np.write()
                time.sleep_ms(50)
            passiveBuzzer.freq(notes["D"])
            passiveBuzzer.duty(50)
            time.sleep_ms(500)
            passiveBuzzer.duty(0)
            servo.myServoWriteAngle(18)
        elif irValue == "0xff18e7":
            lcd.move_to(0, 1)
            lcd.putstr("Key Two          ")
            for j in range(0,8):
                np[j]= colors[2]
                np.write()
                time.sleep_ms(50)
            passiveBuzzer.freq(notes["E"])
            passiveBuzzer.duty(50)
            time.sleep_ms(500)
            passiveBuzzer.duty(0)
            servo.myServoWriteAngle(36)
        elif irValue == "0xff7a85":
            lcd.move_to(0, 1)
            lcd.putstr("Key Three           ")
            for j in range(0,8):
                np[j]= colors[3]
                np.write()
                time.sleep_ms(50)
            passiveBuzzer.freq(notes["F"])
            passiveBuzzer.duty(50)
            time.sleep_ms(500)
            passiveBuzzer.duty(0)
            servo.myServoWriteAngle(54)
        elif irValue == "0xff10ef":
            lcd.move_to(0, 1)
            lcd.putstr("Key Four           ")
            for j in range(0,8):
                np[j]= colors[4]
                np.write()
                time.sleep_ms(50)
            passiveBuzzer.freq(notes["G"])
            passiveBuzzer.duty(50)
            time.sleep_ms(500)
            passiveBuzzer.duty(0)
            servo.myServoWriteAngle(72)
        elif irValue == "0xff38c7":
            lcd.move_to(0, 1)
            lcd.putstr("Key Five           ")
            for j in range(0,8):
                np[j]= colors[5]
                np.write()
                time.sleep_ms(50)
            passiveBuzzer.freq(notes["A"])
            passiveBuzzer.duty(50)
            time.sleep_ms(500)
            passiveBuzzer.duty(0)
            servo.myServoWriteAngle(90)
        elif irValue == "0xff5aa5":
            lcd.move_to(0, 1)
            lcd.putstr("Key Six              ")
            for j in range(0,8):
                np[j]= colors[6]
                np.write()
                time.sleep_ms(50)
            passiveBuzzer.freq(notes["B"])
            passiveBuzzer.duty(50)
            time.sleep_ms(500)
            passiveBuzzer.duty(0)
            servo.myServoWriteAngle(108)
        elif irValue == "0xff42bd":
            lcd.move_to(0, 1)
            lcd.putstr("Key Seven               ")
            for j in range(0,8):
                np[j]= colors[7]
                np.write()
                time.sleep_ms(50)
            passiveBuzzer.freq(notes["C8"])
            passiveBuzzer.duty(50)
            time.sleep_ms(500)
            passiveBuzzer.duty(0)
            servo.myServoWriteAngle(126)
        elif irValue == "0xff4ab5":
            lcd.move_to(0, 1)
            lcd.putstr("Key Eight           ")
            for j in range(0,8):
                np[j]= colors[8]
                np.write()
                time.sleep_ms(50)
            passiveBuzzer.freq(notes["D8"])
            passiveBuzzer.duty(50)
            time.sleep_ms(500)
            passiveBuzzer.duty(0)
            servo.myServoWriteAngle(144)
        elif irValue == "0xff52ad":
            lcd.move_to(0, 1)
            lcd.putstr("Key Nine          ")
            for j in range(0,8):
                np[j]= colors[9]
                np.write()
                time.sleep_ms(50)
            passiveBuzzer.freq(notes["E8"])
            passiveBuzzer.duty(50)
            time.sleep_ms(500)
            passiveBuzzer.duty(0)
            servo.myServoWriteAngle(162)

except Exception as e:
    print("Error:", e)
    servo.deinit()
    lcd.clear()
    passiveBuzzer.deinit()


Video of program in action