圧電スピーカー~余興です。 ラズベリーパイ研究室

圧電スピーカーを使って遊んでみました。
PCのbeep音などで使われている圧電スピーカーは構造が単純なので簡単に音を鳴らすことができます。 8bitマイコン世代には懐かしいBeep音の響きを楽しんでみましょう。



おススメ!記事
Raspberry Pi 用「HAL」で、
カップラーメン・タイマーを作ってみよう!
ラズパイDIYの決定版! ソケットサーバー「HAL」をご紹介します。

※誠に申し訳ありませんがこちらの電子工作のコンテンツは弊社の実験制作例となっております。十分な安全が保障されているわけではないため、参照や実施は自己責任となってしまいますのでご注意ください。

はじめに

圧電スピーカーで遊んでみました。

電極に信号電圧を加えることにより圧電体が歪み、その振動を音(空気の振動)として聞くものである。(wikipediaより)

PCのbeep音などで使われていますね。圧電スピーカーを鳴らすための回路は極めて単純で、GPIOピンとGNDを圧電スピーカーに繋ぐだけです。あとはプログラムで好きな周波数のPWM信号を送るだけでメロディーが鳴らせます。

前準備(WiringPiとWiringPi2-Pythonのインストール)

圧電スピーカーでメロディーを奏でるためには、指定した周波数のPWM信号(Pulse Width Modulation)を送信しなければなりません。そこでGPIOを操作するためのC言語ライブラリであるWiringPiと、今回メロディーのプログラムに用いたPythonのWiringPiラッパーであるWiringPi2-Pythonをインストールします。

WiringPi

git clone git://git.drogon.net/wiringPi
cd wiringPi
./build
cd ../

WiringPi2-Python

sudo apt-get update
sudo apt-get install python-dev python-setuptools
git clone https://github.com/Gadgetoid/WiringPi2-Python.git
cd WiringPi2-Python
sudo python setup.py install

配線

本当にこれだけ。配線と呼べるのでしょうか?

圧電スピーカー

使用部品

  • 圧電スピーカー 1個

プログラムでGPIO25番(22番ピン)からソフトウェアPWM信号を送信します。

プログラム(Python)

プログラム beep.py の方も、とてもプログラムとは呼べないコードです。(職業プログラマにあるまじきコード)

# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
from time import sleep
import wiringpi2 as wiringpi

delay = 0.2
outpin = 25

A1 = 135
B1 = 160
C = 190
D = 240
E = 265
F = 315
G = 365
A = 390
B = 415

GPIO.setmode(GPIO.BCM)
GPIO.setup(outpin, GPIO.OUT)

wiringpi.wiringPiSetupGpio() # GPIO名で番号を指定する

wiringpi.softToneCreate(outpin)


## イントロ
wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

## Aメロ

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, C)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, F)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, C)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, F)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

##

wiringpi.softToneWrite(outpin, A)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, A)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, A)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, B)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, F)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, C)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, F)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, F)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)


## ブリッジ

wiringpi.softToneWrite(outpin, B)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)
wiringpi.softToneWrite(outpin, E)
sleep(delay)

wiringpi.softToneWrite(outpin, B)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, B)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)
wiringpi.softToneWrite(outpin, E)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay)
wiringpi.softToneWrite(outpin, D)
sleep(delay)
wiringpi.softToneWrite(outpin, E)
sleep(delay)


##

wiringpi.softToneWrite(outpin, 0)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, D)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, C)
sleep(delay)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)
wiringpi.softToneWrite(outpin, E)
sleep(delay)

wiringpi.softToneWrite(outpin, F)
sleep(delay)
wiringpi.softToneWrite(outpin, E)
sleep(delay)
wiringpi.softToneWrite(outpin, F)
sleep(delay)


##

wiringpi.softToneWrite(outpin, 0)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, D)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, C)
sleep(delay)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)
wiringpi.softToneWrite(outpin, E)
sleep(delay)

wiringpi.softToneWrite(outpin, F)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)
wiringpi.softToneWrite(outpin, F)
sleep(delay)

wiringpi.softToneWrite(outpin, G)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, G)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)


#

wiringpi.softToneWrite(outpin, B)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)
wiringpi.softToneWrite(outpin, E)
sleep(delay)

wiringpi.softToneWrite(outpin, B)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, B)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)
wiringpi.softToneWrite(outpin, E)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay)
wiringpi.softToneWrite(outpin, D)
sleep(delay)
wiringpi.softToneWrite(outpin, E)
sleep(delay)

##

wiringpi.softToneWrite(outpin, 0)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, D)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, C)
sleep(delay)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)
wiringpi.softToneWrite(outpin, E)
sleep(delay)

wiringpi.softToneWrite(outpin, F)
sleep(delay)
wiringpi.softToneWrite(outpin, E)
sleep(delay)
wiringpi.softToneWrite(outpin, F)
sleep(delay)


###

wiringpi.softToneWrite(outpin, 0)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, D)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, C)
sleep(delay)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)
wiringpi.softToneWrite(outpin, C)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, C)
sleep(delay)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)
wiringpi.softToneWrite(outpin, F)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

###アウトロ

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 2)
wiringpi.softToneWrite(outpin, 0)
sleep(delay)

wiringpi.softToneWrite(outpin, E)
sleep(delay * 3)


GPIO.cleanup()

プログラムの実行は、

sudo python beep.py

です。

クオリティーの低さにびっくり!

この記事へのコメント

※現在コメントはMarkdown記法が強制です。>>Markdown の書き方


すごいです

お名前:通りすがり

2016-11-03 22:52:13

まだまねしてませんが、すごいです。ちょっと試してみます。感動しました。

お名前:管理者ウェブサイト

2016-11-04 10:11:47

いらっしゃいませ。 WiringPiがあるととても簡単にGPIOの操作ができますので、 お気軽にお試しください。

この記事に返信

このコメントに返信