ESP32 + EC11旋转编码器调节PWM占空比输出

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ESP32 + EC11旋转编码器调节PWM占空比输出相关的知识,希望对你有一定的参考价值。

ESP32 + EC11旋转编码器调节PWM占空比输出


  • ✨本项目基于Arduino开发框架下功能实现。

⛳功能介绍

  • 🌿EC11旋转编码器单击PWM +1,双击PWM +3,长按PWM +6.
  • 🌿EC11旋转编码器顺时针旋转,每转一格,PWM +1
  • 🌿EC11旋转编码器逆时针旋转,每转一格,PWM -1

🌻EC11旋转编码器


  • 🌼EC11旋转编码器模块
  • 🔰EC11滤波电路
  • 🔧接线图原理图

    🛠接线说明

🎉参照上面的原理图进行接线,如果想实现平滑控制,最好加上滤波电容。

EC11 A ---->ESP32 GPIO 22
EC11 B ---->ESP32 GPIO 23
EC11 E ---->ESP32 GPIO 19
EC11 D 和 C---->ESP32 GND


📚所需库

🌾OneButton库:

🌾ESP32Encoder库:

📝代码实现

#include "OneButton.h"//点击这里会自动打开管理库页面: http://librarymanager/All#OneButton
#include <ESP32Encoder.h>//点击这里会自动打开管理库页面: http://librarymanager/All#ESP32Encoder


#define LED_BUILTIN 2  //板载LED灯

//EC11 引脚配置
#define EC11_A_PIN 22
#define EC11_B_PIN 23
#define EC11_K_PIN 19//按键引脚

OneButton SW(EC11_K_PIN, true);
ESP32Encoder encoder;
int lastEncoderValue = 0;
int now_count = 0;
bool activate = true;

// setting PWM properties
const int freq = 5000;//设置频率
const int ledChannel = 0;//通道号,取值0 ~ 15
const int resolution = 8;//计数位数,取值0 ~ 20

char PWM_Value = 0;//PWM占空比

//按键单击回调函数
void click() 

  PWM_Value += 1;
  if (PWM_Value > 100)
  
    PWM_Value = 100;//如果占空比100了就不让增加了
    Serial.printf("click,PWM already is 100%,Current PWM_Value=%d \\n", PWM_Value);
   else
    Serial.printf("Click,PWM ADD +1,Current PWM_Value=%d\\t", PWM_Value);



//按键长按回调函数
void longclick() 
 if (activate)    //如果旋钮转动,则不切换状态
  PWM_Value += 6;
  if (PWM_Value > 100)
  
    PWM_Value = 100;
    Serial.printf("longclick,PWM already is 100%,Current PWM_Value=%d \\n", PWM_Value);
   else
    Serial.printf("longclick,PWM ADD +6,Current PWM_Value=%d\\t", PWM_Value);
   
    
 activate = true;


//按键双击回调函数
void doubleclick() 
  PWM_Value += 3;
  if (PWM_Value > 100)
  
    PWM_Value = 100;
    Serial.printf("Doubleclick,PWM already is 100%,Current PWM_Value=%d \\n", PWM_Value);
   else
    Serial.printf("Doubleclick,PWM ADD +3,Current PWM_Value=%d\\t", PWM_Value);


void setup() 
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN, OUTPUT);
  encoder.attachSingleEdge(EC11_A_PIN, EC11_B_PIN);
  pinMode(EC11_K_PIN, INPUT_PULLUP);
  Serial.begin(115200);
  // configure LED PWM functionalitites
  ledcSetup(ledChannel, freq, resolution);//设置PWM通道、频率、计数位数
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(LED_BUILTIN, ledChannel);//将 LEDC 通道绑定到指定 IO 口上以实现输出
  ledcWrite(ledChannel, 0);//设置占空比0
  //初始化按键事件检测
  SW.attachClick(click);
  SW.attachDoubleClick(doubleclick);
  SW.attachLongPressStop(longclick);
  SW.setDebounceTicks(20);//滤波(ms)
  SW.setClickTicks(200);
  SW.setPressTicks(500);




void loop() 
  // put your main code here, to run repeatedly:

  SW.tick();
  if (lastEncoderValue != encoder.getCount()) 
    now_count = encoder.getCount();
    if (now_count != lastEncoderValue) 
      if (!SW.isIdle())          //检测按键是否空闲
        activate = false;
        Serial.print("(Long_pressed)Encoder value: ");
        Serial.println(now_count);
       else 
        Serial.print("Encoder value: ");
        Serial.println(now_count);
      
    
    if (now_count > lastEncoderValue) 
      if (!SW.isIdle())          //检测按键是否空闲
        //按钮按下顺时针功能
        Serial.println("UP_ARROW");
       else 
        //顺时针功能
        PWM_Value++;
        if (PWM_Value > 100)
        
          PWM_Value = 100;
          Serial.printf("PWM already is 100%%,Current PWM_Value=%d \\n", PWM_Value);
         else
          Serial.printf("PWM_Value +1,PWM_Value=%d\\t", PWM_Value);
      
    
    if (now_count < lastEncoderValue) 
      if (!SW.isIdle())          //检测按键是否空闲
        //模式1按钮按下逆时针功能
        Serial.println("DOWN_ARROW");
       else 
        //逆时针功能
        if (PWM_Value == 0)
         //先判断PWM_Value是否为0了,再进行--操作
          PWM_Value = 0;
          Serial.printf("PWM already is 0%%,Current PWM_Value=%d \\n", PWM_Value);
         else
          
          PWM_Value--;
        Serial.printf("PWM_Value -1,PWM_Value=%d\\t", PWM_Value);
          
      
    
    lastEncoderValue = now_count;
    // analogWrite(LED_BUILTIN, PWM_Value);         // 实现PWM引脚设置
    ledcWrite(ledChannel, PWM_Value);//指定通道输出指定占空比波形
  


  • 📜调试信息串口输出

以上是关于ESP32 + EC11旋转编码器调节PWM占空比输出的主要内容,如果未能解决你的问题,请参考以下文章

ESP32 通过Web图形控件调节PWM占空比输出

ESP8266 通过Web图形控件调节PWM占空比输出

ESP8266 通过Web图形控件调节PWM占空比输出

STM32笔记之PWM输出,调节频率及占空比

如何通过调节PWM的占空比来调节电压

怎么用51单片机来实现pwm调节占空比