cmpt_401_portfolio

Lab Assignment 6

Oct. 30, 2023

Tutorial Media

Chapter 17.2

Chapter 18.1

Chapter 18.2

Chapter 19.1

Chapter 20.1

 

Back to top

Assignment project 1:

Description:

A system that, when a photoresistor perceives relative darkness, waves a hand, flashes a LED light, and sings a sad song.

List of hardware components:

Component Amount
ESP32-S3-WROOM 1
GPIO Extension Board 1
Project Board 1
LED Blue 1
Photoresistor 1
Servo Motor 1
Hand Attachement 1
Passive Buzzer 2
NPN 2
Wire M-M 6
Resistor - 220 Ω 1
Resistor - 1K Ω 2
Resistor - 10K Ω 1

Circuit diagram:

Photo of breadboard:

Breadboard view:

Source code:

from machine import Pin,PWM,ADC
from myservo import myServo
from notes import *
import time

servo=myServo(21)          # set servo pin
servo.myServoWriteAngle(0) # set initial servo angle
time.sleep_ms(1000)

led = Pin(1,Pin.OUT)       # set led pin
adc = ADC(Pin(2))          # set photoresistor pin
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT)

def remap(value,oldMin,oldMax,newMin,newMax):
    return int((value)*(newMax-newMin)/(oldMax-oldMin))

pB1 = PWM(Pin(13),2000)    # for treble
pB2 = PWM(Pin(14),1000)    # for bass
pB1.deinit()               # turn off initial sound
pB2.deinit()               #  "

myWay = [ # song progression from "My Way" by Frank Sinatra
    #1
    [0,F(2)],[],[0,0],[0,C(3)],[],[0,0],
    [0,F(3)],[],[0,0],[0,C(3)],[],[0,0],
    [0,A(3)],[],[0,0],[0,C(3)],[],[0,0],
    [0,F(3)],[],[0,0],[0,C(3)],[],[0,0],
    #2
    [0,F(2)],[],[0,0],[0,C(3)],[],[0,0],
    [0,F(3)],[],[0,0],[0,C(3)],[],[0,0],
    [0,A(3)],[],[0,0],[0,C(3)],[],[0,0],
    [0,F(3)],[],[0,0],[C(4),C(3)],[],[0,0],
    #3
    [A(4),F(2)],[],[1,0],[1,C(3)],[],[1,0],
    [1,F(3)],[],[1,0],[1,C(3)],[],[1,0],
    [1,A(3)],[],[0,0],[C(4),C(3)],[],[0,0],
    [A(4),F(3)],[],[0,0],[C(4),C(3)],[],[0,0],
    #4
    [A(4),E(2)],[],[1,0],[1,C(3)],[],[1,0],
    [1,F(3)],[],[1,0],[1,C(3)],[],[1,0],
    [1,A(3)],[],[0,0],[C(4),C(3)],[],[0,0],
    [A(4),E(3)],[],[0,0],[G(4),C(3)],[],[0,0],
    #5
    [A(4),D_(2)],[],[1,0],[1,C(3)],[],[1,0],
    [1,D_(3)],[],[1,0],[1,C(3)],[],[0,0],
    [C(4),G(3)],[],[1,0],[0,C(3)],[A(4),1],[1,0],
    [1,C(3)],[0,1],[G(4),0],[1,C(3)],[],[0,0],
    #6
    [G(4),D(2)],[],[0,0],[F_(4),C(3)],[],[1,0],
    [1,F_(3)],[],[1,0],[1,C(3)],[],[1,0],
    [1,A(3)],[],[1,0],[1,C(3)],[],[0,0],
    [0,F(3)],[],[0,0],[D(4),C(3)],[],[0,0],
    #7
    [A_(4),G(2)],[],[1,0],[1,A_(2)],[],[1,0],
    [1,F(3)],[],[1,0],[1,D(3)],[],[1,0],
    [1,B(3)],[],[0,0],[D(4),D(3)],[],[0,0],
    [A_(4),G(3)],[],[0,0],[A(4),A_(2)],[],[0,0],
    #8
    [A_(4),F(2)],[],[1,0],[1,D(3)],[],[1,0],
    [1,G(3)],[],[1,0],[1,D(3)],[],[1,0],
    [1,B(3)],[],[0,0],[D(4),D(3)],[],[0,0],
    [A_(4),G(3)],[],[0,0],[A(4),A_(2)],[],[0,0],
    #9
    [A_(4),E(2)],[],[1,0],[1,A_(2)],[],[1,0],
    [1,E(3)],[],[1,0],[1,C(3)],[],[1,0],
    [1,G(3)],[],[0,0],[A_(4),C(3)],[],[0,0],
    [C(5),E(3)],[],[0,0],[G(4),A_(2)],[],[0,0],
    #10
    [A_(4),F(2)],[],[0,0],[A(4),C(3)],[],[1,0],
    [1,F(3)],[],[1,0],[1,C(3)],[],[1,0],
    [1,A(3)],[],[1,0],[1,C(3)],[],[0,0],
    [0,F(3)],[],[0,0],[F(4),C(3)],[],[0,0],
    #11
    [C(5),F(2)],[],[1,0],[1,C(3)],[],[1,0],
    [1,F(3)],[],[1,0],[1,C(3)],[],[1,0],
    [1,E(3)],[],[0,0],[B(4),C(3)],[],[0,0],
    [C(5),A(3)],[],[0,0],[A_(4),C(3)],[],[0,0],
    #12
    [C(5),F(2)],[],[1,0],[1,C(3)],[],[1,0],
    [1,D_(3)],[],[1,0],[1,C(3)],[],[1,0],
    [1,A(3)],[],[0,0],[D(5),C(3)],[],[0,0],
    [A(4),E(3)],[],[0,0],[C(5),C(3)],[],[0,0],
    #13
    [A_(4),B(1)],[],[1,0],[1,D(3)],[],[1,0],
    [1,F(3)],[],[1,0],[1,D(3)],[],[1,0],
    [1,B(3)],[],[0,0],[A(4),D(3)],[],[0,0],
    [A_(4),F(3)],[],[0,0],[A(4),D(3)],[],[0,0],
    #14
    [C(5),G(2)],[],[0,0],[A_(4),D(3)],[],[1,0],
    [1,F(3)],[],[1,0],[1,D(3)],[],[1,0],
    [1,B(3)],[],[1,0],[1,D(3)],[],[0,0],
    [C(5),F(3)],[],[1,0],[1,D(3)],[],[0,0],
    #15
    [A(4),F(2)],[],[1,0],[1,C(3)],[],[1,0],
    [1,F(3)],[],[1,0],[1,C(3)],[],[1,0],
    [1,A(3)],[],[0,0],[C(4),C(3)],[],[0,0],
    [A_(4),F(3)],[],[0,0],[A(4),C(3)],[],[0,0],
    #16
    [G(4),C(2)],[],[1,0],[1,A_(2)],[],[1,0],
    [1,F(3)],[],[1,0],[1,B(2)],[],[0,0],
    [E(4),G(3)],[],[1,0],[1,A_(2)],[],[0,0],
    [F(4),E(3)],[],[0,0,],[G(4),A_(2)],[],[0,0],
    #17
    [G(4),F(2)],[],[1,0],[1,A_(2)],[],[1,0],
    [1,D(3)],[A_(4),1],[D(5),0],[G(5),A_(2)],[B(5),1],[D(6),0],
    [G(6),F(3)],[],[1,0],[1,A_(2)],[],[1,0],
    [1,D(3)],[],[1,0],[1,A_(2)],[],[0,0],
    #18
    [F(4),F(2)],[],[1,0],[1,A(2)],[],[1,0],
    [1,C(3)],[],[1,0],[1,A(2)],[],[1,0],
    [1,F(3)],[],[1,0],[1,A(2)],[],[0,0],
    [0,C(3)],[],[0,0],[C(4),A(2)],[],[0,0]
]

incre = 0
def Play(song, inc):  # play song in array format
    print(inc,song[inc])
    if len(song[inc]) == 2:
        if not song[inc][0] == 0 and not song[inc][0] == 1:
            pB1.init()
            pB1.freq(song[inc][0])
        elif not song[inc][0] == 1:
            pB1.deinit()
        if not song[inc][1] == 0 and not song[inc][1] == 1:
            pB2.init()
            pB2.freq(song[inc][1])
        elif not song[inc][1] == 1:
            pB2.deinit()

speed = 9

try:
    while True:
        if adc.read() > 2200:                # listen light changes
            for i in range(60,120,1):        # move hand one way
                Play(myWay, incre)           # play song
                if i%speed==0:
                    incre += 1
                servo.myServoWriteAngle(i)
                time.sleep_ms(15)
                led.value(1)                 # led on
                if adc.read() < 2200:        # listen light changes
                    incre = 0
                    break
            for i in range(120,60,-1):       # move hand other way
                Play(myWay, incre)           # play song
                if i%speed==0:
                    incre += 1
                servo.myServoWriteAngle(i)
                time.sleep_ms(15)
                led.value(0)                 # led off
                if adc.read() < 2200:        # listen light changes
                    incre = 0
                    break
            pB1.deinit()
            pB2.deinit()

except Exception as e:
    print("Error:", e)
    servo.deinit()
    pB1.deinit()
    pB2.deinit()

Additional file: notes.py

Video:

Follow this youtube link for the video: https://youtu.be/13jHvRl1cuc

 

Back to top

Assignment project 2:

Description:

When a thermistor reads relative heat, change an LCD from a positive temperature message to an advice message and turn on a fan with a DC motor. Once the termistor cools off again, return message to default and turn off fan.

List of hardware components:

Component Amount
ESP32-S3-WROOM 1
GPIO Extension Board 1
Project Board 1
L293D chip 1
LCD 1602 Display 1
Thermistor 1
DC Motor 1
Resistor - 10k Ω 1
Wire M-F 4
Wire M-M 11

Special: Borrowed fan component from Jonah Watts

Circuit diagram:

Photo of breadboard:

Breadboard view:

Source code:

from machine import Pin,PWM,I2C,ADC
from I2C_LCD import I2cLcd
import time
import math

i2c = I2C(scl=Pin(10), sda=Pin(11), freq=400000)        # Test for I2C devices (LCD)
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)

in1Pin=Pin(13, Pin.OUT)                                 # Set in-pins for L293D chip
in2Pin=Pin(14, Pin.OUT)

enablePin=Pin(12, Pin.OUT)                              # Set enabling pin for L293D chip
pwm=PWM(enablePin,10000)                                # set motor pin for L293D chip

adc=ADC(Pin(1))                                         # Set Thermistor analog reader
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT)

def readTemp(adc):                                      # Read and return Thermistor value
    adcValue=adc.read()
    voltage=adcValue/4095*3.3
    Rt=10*voltage/(3.3-voltage)
    tempK=(1/(1/(273.15+25)+(math.log(Rt/10))/3950))
    tempC=tempK-273.15
    print("Temperature :",tempC)
    return tempC
    
hot = False
tempChange = 29.9
    
try:
    in1Pin.value(1)                                     # set motor spin direction
    in2Pin.value(0)
    while True:
        if readTemp(adc) > tempChange:                  # if thermistor heats up
            hot = True
        time.sleep_ms(10)
        if not hot:                                     # while not hot, display:
            lcd.move_to(0, 0)                           # "This is a good temperature."
            lcd.putstr("This is a good")
            lcd.move_to(0, 1)
            lcd.putstr("temperature.  ")
        while hot:
            lcd.move_to(0, 0)                           # while hot, display:
            lcd.putstr("Cool          ")                # "Cool yourself down."
            lcd.move_to(0, 1)
            lcd.putstr("yourself down.")
            pwm.duty(800)                               # turn on fan
            if readTemp(adc) <= tempChange:             # if thermistor cools down
                hot = False
                pwm.duty(0)                             # turn off fan
            time.sleep_ms(10)
        print(hot)
except Exception as e:
    pwm.deinit()
    print("Error:", e)

Video: