Skip to content

Instantly share code, notes, and snippets.

@ksasao
Last active February 24, 2019 16:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksasao/485ffbccbf3c47ea9cb814d3484e85e0 to your computer and use it in GitHub Desktop.
Save ksasao/485ffbccbf3c47ea9cb814d3484e85e0 to your computer and use it in GitHub Desktop.
M5Stack analog read and noise suppression
#include <driver/adc.h>
#include <M5Stack.h>
void setup() {
Serial.begin(115200);
M5.begin();
M5.Speaker.setVolume(0);
M5.Lcd.setBrightness(200);
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_0,ADC_ATTEN_0db);
M5.Lcd.println("Press A button...");
}
bool _adcReading = 1;
bool _updateDisplay = 1;
int _pos = 0;
void loop() {
if(M5.BtnA.wasPressed()) {
_adcReading = 1 - _adcReading;
}
if(M5.BtnB.wasPressed()) {
_updateDisplay = 1 - _updateDisplay;
}
int val = 0;
if(_adcReading){
val = adc1_get_voltage(ADC1_CHANNEL_0); // it causes hum noise...
dacWrite(25, 0); // immediate dacWrite() call reduce it, but..
}
if(_updateDisplay){ // .. light (less pixels) draw call cause noise when you use adc.
int r = random(M5.Lcd.height()-1);
M5.Lcd.drawLine(_pos, 0, _pos, r, RED);
M5.Lcd.drawLine(_pos, r, _pos,239, BLUE);
// M5.Lcd.fillRect(0,0,320,240,GREEN); // if you execute this line (draw a lot), noise would be suppressed.
_pos = (_pos + 1)% M5.Lcd.width();
}
M5.update();
}
@macsbug
Copy link

macsbug commented Mar 11, 2018

ADC 使用時のスピーカーからのノイズを消す方法。
dacWrite(25, 0); // Speaker OFF
詳細:
https://macsbug.wordpress.com/2017/12/31/audio-spectrum-display-with-m5stack/

@ksasao
Copy link
Author

ksasao commented Mar 11, 2018

ありがとうございます。いただいた情報をヒントにノイズ除去を試みました。何かを少しだけ描画した場合は若干ノイズが残りましたが、全く画面を描画しないか、または、大量に描画した場合にはノイズが消えました。それが追試できるよう上記のコードを更新しました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment