0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

圧電スピーカー使ってみた

Posted at

概要

圧電スピーカーを購入したので試してみました。
振動などを検知するセンサーです。

部品

圧電スピーカー:セラミック圧電振動センサーモジュール
84円
image.png

表示機:TM1637が組み込まれた7セグLED
73円
image.png

コンピュータ:Arduino UNO互換機
699円
img.png

配線

スクリーンショット 2020-03-24 6.04.51.png

ソースコード

アナログ信号を受け取り、10回の平均を表示するようにしています。


# include <Arduino.h>
# include <Wire.h>
# include <TM1637Display.h>
# define SERIAL_BAUD 115200

# define CLK 2
# define DIO 3
TM1637Display display(CLK, DIO);
# define PIEZOELECTRIC_LEN 10
int Piezoelectrics[PIEZOELECTRIC_LEN] = {0,0,0,0,0,0,0,0,0,0};
int Piezoelectrics_idx = 0;

void setup() {
  Serial.begin(SERIAL_BAUD);
  while(!Serial) {}

  uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
  display.setBrightness(0x0f);
  display.setSegments(data);
  delay(1000);
}

void loop() {
  int ave = 0;
  int Piezoelectric;
  Piezoelectrics_idx++;
  if(! (PIEZOELECTRIC_LEN > Piezoelectrics_idx) )
    Piezoelectrics_idx = 0;
  Piezoelectrics[Piezoelectrics_idx] = analogRead(A0);
  for(int i=0 ; i<PIEZOELECTRIC_LEN;i++){
    ave+=Piezoelectrics[i];
  }
  Piezoelectric = ave/PIEZOELECTRIC_LEN;

  display.showNumberDec(Piezoelectric, false); 
  Serial.print("Piezoelectric: ");
  Serial.print(Piezoelectric);
  delay(100);
}

github

動作確認

gif動画で上げても分かりにくかったので、Youtubeにて確認いただけると幸いです。
IMAGE ALT TEXT HERE

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?