最新情報

ESP32 ロータリーエンコーダー Pulse Counter (PCNT)

ESP32で

インクリメンタルエンコーダ 「LPD3806-600BM-G5-24C」

を繋げてみました。

ソフトウェアで読取り処理も大変なので、ハードウェアで読取りと思って情報収集していました。

 

ESP-WROOM-32のパルスカウンタの使い方(arduino) – Qiita

@wanko_in_lunchの情報が、とても参考になりました。

ありがとうございます。

 

ESP32には、高性能な「パルスカウンタ」が内蔵されているようです。

 

IO12とIO13にエンコーダ信号、IO19にカウンタリセットスイッチを繋げただけです。

表示は、タブレットへBluetoothで飛ばしています。

ハードウェアでカウントしているので、手でエンコーダを「ぐるぐる」程度では

読み飛ばしは無いようです。


ソースはこんな感じです。ほとんどパクリに近いです。

#include <Arduino.h>
#include “driver/pcnt.h”
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
#define PULSE_PIN_CLK 13    // エンコーダー A相
#define PULSE_PIN_DT  12    // エンコーダー B相
#define PULSE_PIN_SW  19    // リセットスイッチ
void setup() {
    Serial.begin(115200);
    SerialBT.begin(“ESP32_R_ENC”);
    delay(1000);
    pinMode(PULSE_PIN_CLK, INPUT_PULLUP);
    pinMode(PULSE_PIN_DT, INPUT_PULLUP);
    pinMode(PULSE_PIN_SW, INPUT_PULLUP);
    pcnt_config_t pcnt_config1 = {};
    pcnt_config1.pulse_gpio_num = PULSE_PIN_CLK;
    pcnt_config1.ctrl_gpio_num  = PULSE_PIN_DT;
    pcnt_config1.lctrl_mode     = PCNT_MODE_KEEP;
    pcnt_config1.hctrl_mode     = PCNT_MODE_REVERSE;
    pcnt_config1.pos_mode       = PCNT_COUNT_INC;
    pcnt_config1.neg_mode       = PCNT_COUNT_DEC;
    pcnt_config1.counter_h_lim  = 2400;
    pcnt_config1.counter_l_lim  = -2400;
    pcnt_config1.unit           = PCNT_UNIT_0;
    pcnt_config1.channel        = PCNT_CHANNEL_0;
    pcnt_config_t pcnt_config2 = {};
    pcnt_config2.pulse_gpio_num = PULSE_PIN_DT;
    pcnt_config2.ctrl_gpio_num  = PULSE_PIN_CLK;
    pcnt_config2.lctrl_mode     = PCNT_MODE_REVERSE;
    pcnt_config2.hctrl_mode     = PCNT_MODE_KEEP;
    pcnt_config2.pos_mode       = PCNT_COUNT_INC;
    pcnt_config2.neg_mode       = PCNT_COUNT_DEC;
    pcnt_config2.counter_h_lim  = 2400;
    pcnt_config2.counter_l_lim  = -2400;
    pcnt_config2.unit           = PCNT_UNIT_0;
    pcnt_config2.channel        = PCNT_CHANNEL_1;
    pcnt_unit_config(&pcnt_config1);    //ユニット1初期化
    pcnt_unit_config(&pcnt_config2);    //ユニット2初期化
    pcnt_counter_pause(PCNT_UNIT_0);    //カウンタ一時停止
    pcnt_counter_clear(PCNT_UNIT_0);    //カウンタ初期化
    pcnt_counter_resume(PCNT_UNIT_0);   //カウント開始
}
void loop() {
    int16_t count = 0;
    pcnt_get_counter_value(PCNT_UNIT_0, &count);
    Serial.println(count);
    SerialBT.println(count);
    if (digitalRead(PULSE_PIN_SW) == LOW) {
        pcnt_counter_clear(PCNT_UNIT_0);
    }
    if (SerialBT.available()) {
        if (SerialBT.read() == ‘0’) {
            pcnt_counter_clear(PCNT_UNIT_0);
        }
    }
    delay(50);
}

最新の投稿記事

加納製作所の商品開発

  • 薪ストーブ
  • メッキアノード
  • 粉体塗料
  • ミニトマト選別機

開発でお悩みの方、

まずは新たな発想を生む
私達の開発環境へお越しください。
知識・経験・ノウハウがそのお悩みを解決します。

開発のご相談