[Diao Ye learns programming] Arduino hands-on (40)---KY-040 rotary encoder module 2

The reference to 37 sensors and modules has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, according to the concept of true knowledge (must be hands-on), for the purpose of learning and communication, here I am going to try and do more experiments one by one.

[Arduino] 168 kinds of sensor module series experiments (data code + simulation programming + graphic programming)
Experiment 40: 360-degree rotary encoder module KY-040 FOR module potentiometer digital pulse output electronic building blocks

insert image description here
The disc that rotates in the middle is called the code disc, and it rotates with the knob. The code disk can be regarded as a conductive metal disk. The A and B pins are respectively connected to the two GPIO pins of the microcontroller, and these two pins are pulled high at the same time, and the C pin is directly grounded. When the code disc rotates, if the contact of the A pin on the code disc touches the code disc, it will be connected with the contact of the C pin on the code disc. Because the C pin is grounded, it is equivalent to grounding the A pin at this time, so the GPIO pin connected to the A pin is pulled down. The B pin is the same.
When the knob is rotated, the principle shown in the figure below is installed to sample the levels of A and B pins:

insert image description here
[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Experiment 40: 360-degree rotary encoder module KY-040 FOR module potentiometer digital pulse output electronic building blocks

Procedure 7: Change of simple detection button (SW)

Arduino experiment open source code

/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
程序七:简易检测按钮(SW)的变化
*/

int SW = 4; //定义引脚连接 SW->D4
bool lastButtonStatus = false;

void setup() {
    
    
  pinMode(SW, INPUT);
  digitalWrite(SW, HIGH);//连接按钮的引脚设为上拉
  Serial.begin(9600);
}

void loop() {
    
    
  bool buttonStatus = !digitalRead(SW);//高电平时未按下,状态为false
  if (buttonStatus != lastButtonStatus)
  {
    
    
Serial.println(buttonStatus ? "按下SW" : "松开SW");
lastButtonStatus = buttonStatus; //保存当前状态
  }
  delay(100);
}

Experimental serial port return

insert image description here
Arduino experiment scene diagram
insert image description here
Arduino experiment wiring diagram and module reference circuit diagram

insert image description here
insert image description here
insert image description here

[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Experiment 40: 360-degree rotary encoder module KY-040 FOR module potentiometer digital pulse output electronic building blocks

Program 8: Rotate clockwise or counterclockwise, press the button to read the values ​​of CLK and DT

Arduino experiment open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序八:顺时针或者反时针旋转,按下按钮读取CLK和DT的值
*/

//定义引脚连接
int CLK = 2;//CLK->D2
int DT = 3;//DT->D3
int SW = 4;//SW->D4

void setup(){
    
    
  pinMode(SW, INPUT);
  digitalWrite(SW, HIGH);//连接按钮的引脚设为上拉
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  Serial.begin(9600);
}

void loop(){
    
    
  if (!digitalRead(SW)) //读取到按钮按下时读取CLK和DT的值
  {
    
    
    int clkValue = digitalRead(CLK);//读取CLK引脚的电平
    int dtValue = digitalRead(DT);//读取DT引脚的电平
    Serial.print("CLK:");
    Serial.print(clkValue);
    Serial.print("; DT:");
    Serial.println(dtValue);
    delay(1000);
  }
}

Experimental serial port return
The experiment found that, regardless of clockwise or counterclockwise rotation, the values ​​of CLK and DT read after each button press are the same, and the values ​​​​between two adjacent steps are different.
insert image description here
[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Experiment 40: 360-degree rotary encoder module KY-040 FOR module potentiometer digital pulse output electronic building blocks

Program 9: Monitor the level change on CLK through interrupt 0, and read the level values ​​of CLK and DT

Arduino experiment open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序九:通过中断0监控CLK上的电平变化,读取CLK和DT的电平值
*/


//定义引脚连接
int CLK = 2;//CLK->D2
int DT = 3;//DT->D3
const int interrupt0 = 0;// Interrupt 0 在 pin 2 上


void setup() {
    
    
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  attachInterrupt(interrupt0, ClockChanged, CHANGE);//设置中断0的处理函数,电平变化触发
  Serial.begin(9600);
}


void loop() {
    
    
}

//中断处理函数
void ClockChanged() {
    
    
  int clkValue = digitalRead(CLK);//读取CLK引脚的电平
  int dtValue = digitalRead(DT);//读取DT引脚的电平
  Serial.print("CLK:");
  Serial.print(clkValue);
  Serial.print("; DT:");
  Serial.println(dtValue);
  delay(300);
}

Experimental serial port return status
Rotate clockwise, the values ​​of CLK and DT are different, and the value order of adjacent steps is different.
insert image description here
Rotate counterclockwise, the values ​​of CLK and DT are the same, and the values ​​of adjacent steps are different.
insert image description here
[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Program 10: Use the rotary encoder module to control the servo motor SG-90

The SG-90 servo motor is a low-cost, high-output servo motor. It can rotate up to 180 degrees, with a maximum of 90 degrees per step. Plus, it's small enough to easily fit into your robotics ARM or obstacle avoidance robot project. Most importantly, it only needs an output pulse signal to control its motion. Pin Diagram of SG90 Servo Motor, it consists of only three pins such as PWM, Ground and Vcc pins. The brown, orange and red wires are GND, Vcc and PWM pins respectively.

insert image description here

Wiring diagram of Arduino experiment
insert image description here

Arduino experiment open source code

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  程序十:使用旋转编码器模块控制伺服电机SG-90
*/

//使用舵机库
#include <Servo.h>
//定义引脚连接
#define CLK 2
#define DT 3

Servo servo; // 创建一个伺服对象
int count = 0;
int current_stateCLK;
int last_stateCLK;

void setup() {
    
    
  pinMode(CLK, INPUT);   // 将编码器引脚设置为输入
  pinMode(DT, INPUT);     // 将编码器引脚设置为输入

  Serial.begin(115200);// 初始化串行通信在115200bps

  //将引脚7上的伺服附加到伺服对象
  servo.attach(7);
  servo.write(count);
  // 读取inputCLK的初始状态
  // 赋值给 previousStateCLK 变量
  last_stateCLK = digitalRead(CLK);
}

void loop() {
    
    
  current_stateCLK = digitalRead(CLK); //读取CLK的当前状态
  // 如果CLK 的先前状态和当前状态不同,则发生脉冲
  if (current_stateCLK != last_stateCLK  && current_stateCLK == 1) {
    
    
    // 如果 inputDT 状态与 inputCLK 状态不同,则
    // 编码器逆时针旋转
    if (digitalRead(DT) != current_stateCLK) {
    
    
      count --;
      if (count < 0)
        count = 0;
    } else {
    
    
      // 编码器顺时针旋转
      count ++;
      if (count > 179)
        count = 179;
    }
    servo.write(count);   // 移动舵机
    Serial.print("角度位置: ");
    Serial.println(count);
  }

  last_stateCLK = current_stateCLK; // 用当前状态更新 last_stateCLK
}

Experimental serial port return

insert image description here

Arduino experiment scene diagram
insert image description here

Experiment video

https://v.youku.com/v_show/id_XNTg0NjU0MDE0OA==

insert image description here
[Arduino] 168 sensor module series experiments (data code + simulation programming + graphics programming)

Experiment 11: TM1637 four-digit digital tube displays the value of the rotary encoder

Open source simulation programming (Linkboy V4.63)

insert image description here
Arduino experiment scene diagram

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/131821703