Nota

Hola, ¡bienvenido a la Comunidad de Entusiastas de SunFounder para Raspberry Pi, Arduino y ESP32 en Facebook! Profundiza tus conocimientos sobre Raspberry Pi, Arduino y ESP32 junto a otros entusiastas.

¿Por qué unirse?

  • Soporte Experto: Resuelve problemas postventa y desafíos técnicos con la ayuda de nuestra comunidad y equipo.

  • Aprender y Compartir: Intercambia consejos y tutoriales para mejorar tus habilidades.

  • Vistas Previas Exclusivas: Obtén acceso anticipado a anuncios de nuevos productos y adelantos exclusivos.

  • Descuentos Especiales: Disfruta de descuentos exclusivos en nuestros productos más recientes.

  • Promociones Festivas y Sorteos: Participa en sorteos y promociones especiales en días festivos.

👉 ¿Listo para explorar y crear con nosotros? Haz clic en [Aquí] y únete hoy mismo.

3.1.10 Alarma

Introducción

En este proyecto, crearemos un dispositivo de alarma manual. Puedes reemplazar el interruptor basculante con un termistor o un sensor fotosensible para crear una alarma de temperatura o una alarma de luz.

Componentes Necesarios

En este proyecto, necesitamos los siguientes componentes.

../_images/4.1.15_alarm_bell_list.png

Diagrama de Circuito

T-Board Name

physical

wiringPi

BCM

GPIO17

Pin 11

0

17

GPIO18

Pin 12

1

18

GPIO27

Pin 13

2

27

GPIO22

Pin 15

3

22

../_images/4.1.15_alarm_bell_schematic.png

Procedimientos Experimentales

Paso 1: Construir el circuito.

../_images/4.1.15_alarm_bell_circuit.png

Paso 2: Cambiar de directorio.

cd ~/davinci-kit-for-raspberry-pi/python-pi5

Paso 3: Ejecutar.

sudo python3 3.1.10_AlarmBell.py

Después de que el programa se inicie, el interruptor se moverá hacia la derecha y el buzzer emitirá un sonido de alarma. Al mismo tiempo, los LEDs rojo y verde parpadearán a una frecuencia determinada.

Advertencia

Si aparece el mensaje de error RuntimeError: Cannot determine SOC peripheral base address, consulta Si gpiozero no funciona.

Código

Nota

Puedes Modificar/Restablecer/Copiar/Ejecutar/Detener el código a continuación. Pero antes de eso, necesitas ir a la ruta del código fuente como raphael-kit/python-pi5.

#!/usr/bin/env python3

from gpiozero import LED, Button, TonalBuzzer
import time
import threading

# Initialize TonalBuzzer on GPIO pin 22
BeepPin = TonalBuzzer(22)

# Initialize LEDs on GPIO pins 17 and 27
ALedPin = LED(17)
BLedPin = LED(27)

# Initialize Button on GPIO pin 18
switchPin = Button(18)

# Global flag to control the buzzer and LED states
flag = 0

def ledWork():
    """
    Control LED blinking pattern based on the flag state.
    When flag is set, alternately blink ALedPin and BLedPin.
    """
    while True:
        if flag:
            # Alternate blinking of LEDs
            ALedPin.on()
            time.sleep(0.5)
            ALedPin.off()
            BLedPin.on()
            time.sleep(0.5)
            BLedPin.off()
        else:
            # Turn off both LEDs if flag is not set
            ALedPin.off()
            BLedPin.off()

# Define the musical tune as a list of notes and their durations
tune = [
    ('C4', 0.1), ('E4', 0.1), ('G4', 0.1),
    (None, 0.1),
    ('E4', 0.1), ('G4', 0.1), ('C5', 0.1),
    (None, 0.1),
    ('C5', 0.1), ('G4', 0.1), ('E4', 0.1),
    (None, 0.1),
    ('G4', 0.1), ('E4', 0.1), ('C4', 0.1),
    (None, 0.1)
]

def buzzerWork():
    """
    Play a tune using the buzzer based on the flag state.
    The tune is played only when the flag is set.
    """
    while True:
        for note, duration in tune:
            if flag == 0:
                break
            print(note)  # Output the current note to the console
            BeepPin.play(note)  # Play the current note
            time.sleep(duration)  # Pause for the duration of the note
        BeepPin.stop()  # Stop the buzzer after playing the tune

def main():
    """
    Monitor button press to update the flag state.
    Sets the flag when the button is pressed.
    """
    global flag
    while True:
        flag = 1 if switchPin.is_pressed else 0

try:
    # Initialize and start threads for buzzer and LED control
    tBuzz = threading.Thread(target=buzzerWork)
    tBuzz.start()
    tLed = threading.Thread(target=ledWork)
    tLed.start()
    main()

except KeyboardInterrupt:
    # Stop the buzzer and turn off LEDs on program interruption
    BeepPin.stop()
    ALedPin.off()
    BLedPin.off()

Explicación del Código

  1. Este segmento incluye la importación de bibliotecas esenciales para implementar retardos y manejo de hilos (threading). También importa las clases LED, Button y TonalBuzzer de la biblioteca gpiozero, que son cruciales para controlar dispositivos GPIO en una Raspberry Pi.

    #!/usr/bin/env python3
    
    from gpiozero import LED, Button, TonalBuzzer
    import time
    import threading
    
  2. Configura un buzzer en el pin GPIO 22, dos LEDs en los pines GPIO 17 y 27, e inicializa un botón en el pin GPIO 18. También se define una bandera global para gestionar el estado del buzzer y los LEDs.

    # Inicializar TonalBuzzer en el pin GPIO 22
    BeepPin = TonalBuzzer(22)
    
    # Inicializar LEDs en los pines GPIO 17 y 27
    ALedPin = LED(17)
    BLedPin = LED(27)
    
    # Inicializar botón en el pin GPIO 18
    switchPin = Button(18)
    
    # Bandera global para controlar los estados del buzzer y LEDs
    flag = 0
    
  3. Esta función controla el parpadeo de los LEDs basado en el estado de la bandera. Si la bandera está activada (1), alterna el encendido y apagado de cada LED. Si está desactivada (0), ambos LEDs se apagan.

    def ledWork():
        """
        Control LED blinking pattern based on the flag state.
        When flag is set, alternately blink ALedPin and BLedPin.
        """
        while True:
            if flag:
                # Parpadeo alterno de los LEDs
                ALedPin.on()
                time.sleep(0.5)
                ALedPin.off()
                BLedPin.on()
                time.sleep(0.5)
                BLedPin.off()
            else:
                # Apagar ambos LEDs si la bandera está desactivada
                ALedPin.off()
                BLedPin.off()
    
  4. La melodía está definida como una secuencia de notas (frecuencia) y duraciones (segundos).

    # Definir la melodía como una lista de notas y sus duraciones
    tune = [
        ('C4', 0.1), ('E4', 0.1), ('G4', 0.1),
        (None, 0.1),
        ('E4', 0.1), ('G4', 0.1), ('C5', 0.1),
        (None, 0.1),
        ('C5', 0.1), ('G4', 0.1), ('E4', 0.1),
        (None, 0.1),
        ('G4', 0.1), ('E4', 0.1), ('C4', 0.1),
        (None, 0.1)
    ]
    
  5. Reproduce una melodía predefinida cuando la bandera está activada. La melodía se detiene si la bandera se desactiva durante la reproducción.

    def buzzerWork():
        """
        Play a tune using the buzzer based on the flag state.
        The tune is played only when the flag is set.
        """
        while True:
            for note, duration in tune:
                if flag == 0:
                    break
                print(note)  # Mostrar la nota actual en la consola
                BeepPin.play(note)  # Reproducir la nota actual
                time.sleep(duration)  # Pausar según la duración de la nota
            BeepPin.stop()  # Detener el buzzer después de la melodía
    
  6. Verifica continuamente el estado del botón para activar o desactivar la bandera.

    def main():
        """
        Monitor button press to update the flag state.
        Sets the flag when the button is pressed.
        """
        global flag
        while True:
            flag = 1 if switchPin.is_pressed else 0
    
  7. Los hilos para buzzerWork y ledWork se inician, permitiéndoles ejecutarse concurrentemente con la función principal.

    try:
        # Inicializar e iniciar hilos para el control del buzzer y los LEDs
        tBuzz = threading.Thread(target=buzzerWork)
        tBuzz.start()
        tLed = threading.Thread(target=ledWork)
        tLed.start()
        main()
    
  8. Detiene el buzzer y apaga los LEDs cuando el programa es interrumpido, asegurando una salida limpia.

    except KeyboardInterrupt:
        # Detener el buzzer y apagar los LEDs al interrumpir el programa
        BeepPin.stop()
        ALedPin.off()
        BLedPin.off()