패시브 부저의 소리를 직접 제어하는 전자 피아노 처럼 만들수 있을까 하는 생각으로 한번 제작해 보았습니다.

부저 (buzzer)는 총 크게 두가지 나누어 집니다.

    패시브 부저 (passive buzzer) : 음의 출력 시간 대역폭을 이용하여 여러 음역대의 소리를 재생하는 것이 가능한 부저.

    액티브 부저 (active buzzer) : 음이 소리가 단일적으로 단조로우며 활용할수 있는 것은 출력의 크기 뿐 이다.


해당 프로젝트는 이중 패시브 부저를 이용하여 컴퓨터의 키보드를 피아노 건반으로 사용하는 부저 피아노 시스템 입니다.

준비물 :

      PASSIVE_BUZZER X1
        TRANSISTOR X1
        LED X1
        1kΩ x1

       

라즈베리파이의 GPIO 시스템과 각 연결 상태는 이러합니다.

      5V -> PASSIVE_BUZZER_1 (+) -> LED_1 (+)

        GPIO_18 -> 1KΩ -> TRANSITOR1(2)
                TRNSISTOR_1(1) -> PASSIVE_BUZZER1 (-) -> LED_1 (-)


        GND -> TRNSISTOR_1(3)



실행 구조 :

    buano (bash)
        - pb1.py (python)


소스 코드 :


buano


#!/bin/bash

trap 'killall -9 python' 0
while [ : ]; do
clear
echo "
            << buano >>

This proram source is buzzer piano by rasberrypi on python module.

Maintainer : C O S M O S project. <cosmosproject15@gmail.com>
Homepage : http://cosmosproject2015.tistory.com
"
read -n 1 -p "input [keyboard]:" pia

    if [ "$pia" = "q" ]; then
        sed -i 's/number/250/g' pb1.py
        python pb1.py
        sed -i 's/250/number/g' pb1.py
    fi
    if [ "$pia" = "w" ]; then

        sed -i 's/number/340/g' pb1.py
        python pb1.py
        sed -i 's/340/number/g' pb1.py


    fi
    if [ "$pia" = "e" ]; then
        sed -i 's/number/420/g' pb1.py
        python pb1.py
        sed -i 's/420/number/g' pb1.py

    fi
    if [ "$pia" = "r" ]; then
        sed -i 's/number/500/g' pb1.py
        python pb1.py
        sed -i 's/500/number/g' pb1.py

    fi

    if [ "$pia" = "t" ]; then
        sed -i 's/number/580/g' pb1.py
        python pb1.py
        sed -i 's/580/number/g' pb1.py
    fi
    if [ "$pia" = "y" ]; then

        sed -i 's/number/640/g' pb1.py
        python pb1.py
        sed -i 's/640/number/g' pb1.py


    fi
    if [ "$pia" = "u" ]; then
        sed -i 's/number/710/g' pb1.py
        python pb1.py
        sed -i 's/710/number/g' pb1.py

    fi
    if [ "$pia" = "i" ]; then
        sed -i 's/number/780/g' pb1.py
        python pb1.py
        sed -i 's/780/number/g' pb1.py

    fi
    if [ "$pia" = "o" ]; then
        sed -i 's/number/850/g' pb1.py
        python pb1.py
        sed -i 's/850/number/g' pb1.py
    fi
    if [ "$pia" = "p" ]; then

        sed -i 's/number/930/g' pb1.py
        python pb1.py
        sed -i 's/930/number/g' pb1.py


    fi
    if [ "$pia" = "a" ]; then
        sed -i 's/number/1000/g' pb1.py
        python pb1.py
        sed -i 's/1000/number/g' pb1.py

    fi
    if [ "$pia" = "s" ]; then
        sed -i 's/number/1080/g' pb1.py
        python pb1.py
        sed -i 's/1080/number/g' pb1.py

    fi
    if [ "$pia" = "d" ]; then
        sed -i 's/number/1140/g' pb1.py
        python pb1.py
        sed -i 's/1140/number/g' pb1.py
    fi
    if [ "$pia" = "f" ]; then

        sed -i 's/number/1220/g' pb1.py
        python pb1.py
        sed -i 's/1220/number/g' pb1.py


    fi
    if [ "$pia" = "g" ]; then
        sed -i 's/number/1300/g' pb1.py
        python pb1.py
        sed -i 's/1300/number/g' pb1.py

    fi
    if [ "$pia" = "h" ]; then
        sed -i 's/number/1380/g' pb1.py
        python pb1.py
        sed -i 's/1380/number/g' pb1.py

    fi
    if [ "$pia" = "j" ]; then
        sed -i 's/number/1430/g' pb1.py
        python pb1.py
        sed -i 's/1430/number/g' pb1.py
    fi
    if [ "$pia" = "k" ]; then

        sed -i 's/number/1500/g' pb1.py
        python pb1.py
        sed -i 's/1500/number/g' pb1.py


    fi
    if [ "$pia" = "l" ]; then
        sed -i 's/number/1570/g' pb1.py
        python pb1.py
        sed -i 's/1570/number/g' pb1.py

    fi
    if [ "$pia" = "z" ]; then
        sed -i 's/number/1630/g' pb1.py
        python pb1.py
        sed -i 's/1630/number/g' pb1.py

    fi

    if [ "$pia" = "x" ]; then
        sed -i 's/number/1710/g' pb1.py
        python pb1.py
        sed -i 's/1710/number/g' pb1.py
    fi
    if [ "$pia" = "c" ]; then

        sed -i 's/number/1800/g' pb1.py
        python pb1.py
        sed -i 's/1800/number/g' pb1.py


    fi
    if [ "$pia" = "v" ]; then
        sed -i 's/number/1870/g' pb1.py
        python pb1.py
        sed -i 's/1870/number/g' pb1.py

    fi
    if [ "$pia" = "b" ]; then
        sed -i 's/number/1920/g' pb1.py
        python pb1.py
        sed -i 's/1920/number/g' pb1.py

    fi
    if [ "$pia" = "n" ]; then
        sed -i 's/number/2000/g' pb1.py
        python pb1.py
        sed -i 's/2000/number/g' pb1.py
    fi
    if [ "$pia" = "m" ]; then

        sed -i 's/number/2080/g' pb1.py
        python pb1.py
        sed -i 's/2080/number/g' pb1.py


    fi
    if [ "$pia" = "," ]; then
        sed -i 's/number/2140/g' pb1.py
        python pb1.py
        sed -i 's/2140/number/g' pb1.py

    fi
    if [ "$pia" = "." ]; then
        sed -i 's/number/2200/g' pb1.py
        python pb1.py
        sed -i 's/2200/number/g' pb1.py

    fi

done

exit 0



pb1.py

import RPi.GPIO as GPIO  
import time              


class Buzzer(object):
 def __init__(self):
  GPIO.setmode(GPIO.BCM) 
  self.buzzer_pin = 18
  GPIO.setup(self.buzzer_pin, GPIO.IN)
  GPIO.setup(self.buzzer_pin, GPIO.OUT)


 def __del__(self):
  class_name = self.__class__.__name__

## Create the function 'buzz'

 def buzz(self,pitch, duration):  
 
  if(pitch==0):
   time.sleep(duration)
   return
  period = 1.0 / pitch    
  delay = period / 2    
  cycles = int(duration * pitch) 

## Set cycles for GPIO.output

  for i in range(cycles):   
   GPIO.output(self.buzzer_pin, True)  
   time.sleep(delay)  
   GPIO.output(self.buzzer_pin, False)   
   time.sleep(delay)   

 def play(self, tune):
  GPIO.setmode(GPIO.BCM)
  GPIO.setup(self.buzzer_pin, GPIO.OUT)
  x=0

## Set the pitch and duration.

  if(tune==1):
    pitches=[number]
    duration=0.1
    for p in pitches:
      self.buzz(p, duration) 
      time.sleep(duration *0.5)


  GPIO.setup(self.buzzer_pin, GPIO.IN)

## Play start

if __name__ == "__main__":
  buzzer = Buzzer()
  buzzer.play(int(1))
GPIO.cleanup()



실행 :


./buano



소스 다운로드 :


github : https://github.com/cosmosproject15/pi-buano



$ git clone https://github.com/cosmosproject15/pi-buano










   
 

+ Recent posts