monochromaticmau
Posts: 4
Joined: Thu Apr 25, 2019 10:20 am

Buzzer and LED Light Independent Simultaneous Output

Mon Mar 09, 2020 11:02 pm

Hello,

I'm trying to have a buzzer sound and an LED light blink at the same time with independent duty cycles, and frequency. I tried to do this with two different pins, but it seems to glitch (buzzer crackling and LED visually crackling)


Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
from multiprocessing import Process


def setup():
	global BuzzerPin
	global LEDPin

	
	BuzzerPin = 11      #BCM17  GPIO17
	LEDPin = 12         #BCM18  GPIO18 Controlling transistor
	
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	
	#Buzzer Setup
	GPIO.setup(BuzzerPin, GPIO.OUT)
	GPIO.output(BuzzerPin, GPIO.LOW)
	
	#LED Setup
	GPIO.setup(LEDPin, GPIO.OUT)
	GPIO.output(LEDPin, GPIO.LOW)
	



def beep():
    for y in range(3):
        GPIO.output(BuzzerPin, GPIO.HIGH)
        time.sleep(1)
        GPIO.output(BuzzerPin, GPIO.LOW)
        time.sleep(1)
    time.sleep(1.5)
    
    
def light():
    for y in range(10):
        GPIO.output(LEDPin, GPIO.HIGH)
        time.sleep(0.25)
        GPIO.output(LEDPin, GPIO.LOW)
 
    
        
	

def loop():
	while True:
            buzzer = Process(target=beep)
            buzzer.start()
            LED = Process(target= light)
            LED.start()

            

def destroy():
	GPIO.output(BuzzerPin, GPIO.HIGH)
	GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()

How do I get an LED light to blink (continuous beacon-like) and a buzzer to beep (fire alarm style ) simultaneously?

monochromaticmau
Posts: 4
Joined: Thu Apr 25, 2019 10:20 am

Re: Buzzer and LED Light Independent Simultaneous Output

Mon Mar 30, 2020 1:26 am

I used threading as a solution

Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import threading
import socket
import sys


alarm_on = True   # Flag to stop the thread
state = b"ALARM OFF"

def setup():
    global BuzzerPin
    global LEDPin
    global MotionSensorPin
	
    BuzzerPin = 11      #BCM17  GPIO17
    LEDPin = 12         #BCM18  GPIO18
    MotionPin = 13      #BCM27 GPIO27

    
    GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
    
    #Buzzer Setup
    GPIO.setup(BuzzerPin, GPIO.OUT)
    GPIO.output(BuzzerPin, GPIO.LOW)
    
    #LED Setup
    GPIO.setup(LEDPin, GPIO.OUT)
    GPIO.output(LEDPin, GPIO.LOW)
    
   
            

def beep():
    while alarm_on:
        for y in range(3):
            GPIO.output(BuzzerPin, GPIO.HIGH)
            time.sleep(1)
            GPIO.output(BuzzerPin, GPIO.LOW)
            time.sleep(1)
        time.sleep(1.5)
    
    
def light():
    while alarm_on:
        for y in range(10):
            GPIO.output(LEDPin, GPIO.HIGH)
            time.sleep(0.2)
            GPIO.output(LEDPin, GPIO.LOW)
            time.sleep(0.5)
    
        
def destroy():
	GPIO.output(BuzzerPin, GPIO.HIGH)
	GPIO.cleanup()                     # Release resource

if __name__ == '__main__':     # Program start from here
	try:
            setup()
            thrd1 = threading.Thread(target=beep).start()
            thrd2 = threading.Thread(target=light).start()
		
	    
	except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destroy()

Return to “Beginners”