"""
Escuela: Instituto Tecnologico De León
Materia: Sistemas Programables
Carrera: Ingeniería en Sistemas Computacionales
Maestra: Ing. Ma Veronica Tapia
Integrantes:
Aldana Perez Jose Samuel [20240446]
Cesar Antonio Guerrero Duran [19240367]
Manrique Galvan Omar Manuel [19240851]
Práctica 3: Icono Tec Leon en OLED.
Objetivo: Desarrollar un programa en Micro Python que permita
producir sonidos con zumbadores en la práctica del PIR
y ultrasónico
Fecha De Entrega: 4/10/2023
"""
import machine
import ssd1306 # Biblioteca para la pantalla OLED
import utime
from hcsr04 import HCSR04 # Biblioteca para el sensor ultrasónico
from machine import Pin, PWM
import time
from time import sleep
import framebuf
# Configuración de pines
pir_pin = Pin(33, Pin.IN) # El pin 14 es donde está conectado el sensor PIR
trig_pin = 18 # Pin del sensor ultrasónico TRIG
echo_pin = 19 # Pin del sensor ultrasónico ECHO
# Configuración de la pantalla OLED
i2c = machine.I2C(scl=machine.Pin(13), sda=machine.Pin(14))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Configurar el sensor ultrasónico
sensor_ultrasonico = HCSR04(trigger_pin=trig_pin, echo_pin=echo_pin)
# Configura el pin GPIO al que está conectado el buzzer
pin_buzzer = machine.Pin(12, machine.Pin.OUT)
# Configura el pin del buzzer y el objeto PWM
buzzer_pin = Pin(32, Pin.OUT)
buzzer_pwm = PWM(buzzer_pin)
buzzer_pwm.duty(0)
motion_detected = False
# Define las notas y las duraciones para la Marcha Imperial
melodia = [
(392, 250), (392, 250), (392, 250), (311, 125), (466, 125),
(392, 250), (311, 125), (466, 125), (392, 500), (587, 250),
(587, 250), (587, 250), (622, 125), (466, 125), (369, 250),
(311, 125), (466, 125), (392, 500), (784, 250), (392, 250),
(392, 250), (784, 250), (739, 125), (698, 125), (659, 250),
(622, 125), (659, 125), (698, 500), (784, 250), (392, 250),
(392, 250), (784, 250), (739, 125), (698, 125), (659, 250),
(622, 125), (659, 125), (698, 500), (392, 250), (988, 250),
(784, 500), (392, 250), (392, 250), (784, 250), (739, 125), (698, 125),
(659, 250), (622, 125), (659, 125), (698, 500), (392, 250),
(784, 250), (392, 500)
]
#Matriz en la que se desarrolla el icono que se muestra en la interrupcion
ICONO = [
[0, 0, 1, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 0, 0, 1, 0, 0, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 1, 0, 0, 0, 1, 0, 1],
[1, 0, 1, 1, 1, 1, 1, 0, 1],
[0, 0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 1, 1, 1, 0],
]
#Matriz en la que se desarrolla el logotipo del Tec de León.
LOGO = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0x25, 0xff, 0xdf, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0x97, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xd7, 0xf7, 0x9f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0x8f, 0x5f, 0xfd, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xef, 0x17, 0xbb, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xef, 0xbb, 0xe7, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xf0, 0x03, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0x83, 0xf0, 0x7f, 0xf3, 0xfe, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7e, 0x3f, 0xff, 0x1f, 0xf3, 0xfe, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x78, 0xef, 0xff, 0xc7, 0xeb, 0x79, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x73, 0xef, 0xf7, 0xf3, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x67, 0xff, 0xe7, 0xf9, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x4f, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1f, 0xff, 0xff, 0xf2, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x3f, 0xff, 0xff, 0xe5, 0x3f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xf1, 0xfd, 0x3f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0x80, 0xff, 0x1f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xf7, 0xfa, 0x00, 0xff, 0x8f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xff, 0xf8, 0x00, 0xff, 0x8f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0xff, 0xf0, 0x00, 0xf7, 0xef, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0xff, 0xf0, 0x00, 0x7f, 0xe7, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0xf0, 0x00, 0x1f, 0x37, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0xf0, 0x00, 0x1f, 0xf7, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xfb, 0x00, 0x00, 0x1f, 0xf3, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0xc0, 0x00, 0x1f, 0xf1, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0x80, 0x00, 0x1f, 0xf0, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0x00, 0x00, 0x0f, 0xf0, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0x00, 0x00, 0x1f, 0xf0, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0x00, 0x00, 0x1f, 0xf0, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xff, 0x00, 0x00, 0x1f, 0xf0, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x3b, 0xff, 0x00, 0x00, 0x1f, 0xf2, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7b, 0xff, 0x00, 0x00, 0x01, 0xf7, 0xbf, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x6b, 0xff, 0x00, 0x00, 0x00, 0xf5, 0xbf, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0xff, 0x00, 0x00, 0x00, 0xe8, 0xbf, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x15, 0xff, 0x00, 0x00, 0x00, 0xe8, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x22, 0xff, 0x00, 0x00, 0x00, 0xc8, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0xff, 0x80, 0x00, 0x01, 0xd1, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x7f, 0x3f, 0xfc, 0xe7, 0xb0, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x3f, 0x7f, 0xff, 0xf7, 0xa2, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0xbf, 0xff, 0xff, 0xf3, 0x44, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xdf, 0xff, 0xff, 0xfe, 0xc5, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x6f, 0xff, 0xff, 0xfd, 0x8b, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x33, 0xff, 0xff, 0xf3, 0x03, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x39, 0xff, 0xff, 0xe6, 0x07, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xbe, 0x7f, 0xff, 0x9e, 0x0d, 0x9b, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7b, 0x8f, 0xfc, 0x38, 0x5f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x18, 0xe0, 0x01, 0xc4, 0x3f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x54, 0x1f, 0xf8, 0x26, 0x7f, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x64, 0x40, 0xc0, 0x39, 0xed, 0xcd, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x79, 0x00, 0x00, 0x67, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7e, 0x30, 0x0b, 0x9f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xc1, 0x70, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
# Función en la que se muestran los nombres de los integrantes del equipo
def mostrar_integrantes():
print("Nombres de los integrantes")
#Limpia la pantalla oled
oled.fill(0)
oled.text('Sistemas Programables',0,10)
oled.text('Cesar', 0, 20)
oled.text('Omar', 0, 30)
oled.text('Samuel', 0, 40)
#Se muestra el contenido de la pantalla.
oled.show()
#Se hace una espera de 2 segundos.
sleep(2)
#Se manda llamar la función.
#mostrar_integrantes()
#Función que construye y muestra el logotipo del Tec.
def mostrar_logo(LOGO):
print("Mostrando Logo Tec")
buffer = bytearray(LOGO)
#Se utiliza la libreria framebuf para construir el logo, usando los parametros de la pantalla oled.
fb = framebuf.FrameBuffer(buffer, oled.width, oled.height, framebuf.MONO_HLSB)
oled.fill(0)
oled.blit(fb, 0, 0)
oled.show()
sleep(2)
oled.fill(0)
oled.show()
#Se manda llamar la funcion.
#mostrar_logo(LOGO)
# Función en la que se describe el objetivo de la practica dentro de la pantalla oled.
def mostrar_objetivo():
print("Objetivo")
oled.fill(0)
oled.text('Medicion distancia HCSR04',0,10)
oled.text('Sensor PIR Interrupcion', 0, 20)
oled.text('Mostrar rutina OLED', 0, 30)
oled.text('de Alerta', 0, 40)
oled.show()
sleep(4)
#Se manda llamar la función.
#mostrar_objetivo()
# Función para mostrar el mensaje de alerta en la pantalla OLED
def mostrar_alerta(oled, icono, ampliacion):
oled.fill(0) # Limpia la pantalla OLED
#El siguiente código se utiliza para la construcción del icono extraterrestre que aparece en la interrupción.
for y, fila in enumerate(icono):
for x, c in enumerate(fila):
for _ in range(ampliacion):
for _ in range(ampliacion):
oled.pixel(x * ampliacion + _, y * ampliacion, c) # Se dibuja el pixel ampliado.
#Se muestran los datos de la medicion y la alerta.
oled.text("Alerta", 0, 30)
oled.text("Fuente de calor", 0, 40)
oled.text("Distancia: %.2f cm" % distancia, 0, 50)
oled.show()
sleep(3)
# Función que se ejecutará cuando se detecte movimiento
def interrupcion(pin):
global motion_detected
motion_detected = True
# Configuración de pines
pir_pin.irq(trigger=Pin.IRQ_RISING, handler=interrupcion) # Configura una interrupción de flanco de subida
def buzzer_enc():
# Enciende el buzzer
pin_buzzer.on()
def buzzer_apag():
# Apaga el buzzer
pin_buzzer.off()
def play_tone(frequency, duration_ms):
# Genera un tono con la frecuencia y duración especificadas
buzzer_enc()
time.sleep_ms(duration_ms)
buzzer_apag()
# Función para reproducir un tono con frecuencia variable según la distancia
def reproducir_sonido(distancia):
if distancia < 10:
# Reproduce un tono de alta frecuencia si el objeto está muy cerca
play_tone(3000, 25) # Frecuencia alta
elif distancia < 20:
# Reproduce un tono de frecuencia media si el objeto está a una distancia intermedia
play_tone(2000, 100) # Frecuencia media
elif distancia < 30:
# Reproduce un tono de frecuencia media si el objeto está a una distancia intermedia
play_tone(1000, 300) # Frecuencia media
elif distancia > 31:
# Reproduce un tono de baja frecuencia si el objeto está lejos
play_tone(500, 500) # Frecuencia baja
# Función para tocar la melodía
def tocar_melodia(melodia):
for nota, duracion in melodia:
buzzer_pwm.freq(nota)
buzzer_pwm.duty(512) # Intensidad media
time.sleep_ms(duracion)
buzzer_pwm.duty(0)
time.sleep_ms(30) # Pausa entre notas
# Función para medir la distancia con el sensor ultrasónico
def medir_distancia():
#Se utilia un try except para casos de que falle la medición.
try:
distancia = sensor_ultrasonico.distance_cm()
print("Distancia: %.2f cm" % distancia)
return distancia
except OSError:
return None
# Ciclo principal
while True:
#Se guarda el valor que da la función "medir_distancia" dentro de la variable.
distancia = medir_distancia()
#Si no está vacía el valor de "distancia".
if distancia is not None:
#Si no, se detecta de manera indefinida la distancia del sensor.
oled.fill(0)
oled.text("Distancia: %.2f cm" % distancia, 0, 30)
oled.show()
# Llama a la función para reproducir el sonido con la frecuencia adecuada
reproducir_sonido(distancia)
if motion_detected:
mostrar_alerta(oled, ICONO, 3)
# Reproduce la Marcha Imperial
tocar_melodia(melodia)
# Detén el sonido al final
buzzer_pwm.duty(0)
motion_detected = False # Reinicia la variable de detección de movimiento
utime.sleep(1) # Pausa para evitar lecturas muy frecuentes