Rotary Encoder เป็นอุปกรณ์อิเล็กทรอนิกส์ที่ใช้แปลงการหมุนเชิงกลให้เป็นสัญญาณไฟฟ้าดิจิทัล สัญญาณไฟฟ้าที่ได้นี้จะบอกถึงตำแหน่งและทิศทางของการหมุน ซึ่งข้อมูลเหล่านี้สามารถนำไปใช้ในการควบคุมมอเตอร์ ป้อนข้อมูลเข้าคอมพิวเตอร์ หรือใช้ในระบบอัตโนมัติต่างๆ
1.ต่ออุปกรณ์
.png?alt=media&token=7f1e1607-cca0-41a2-b6e0-d802c2816667)
Rotary Encoder > Arduino UNO
CLK>PIN 2DT>PIN 3SW>Pin 4+>5VGND>GND
2.ลงโปรแกรม
Copy โค้ดด้านล่าง
Arduino
1#define ENCODER_PIN_A 2 // CLK
2#define ENCODER_PIN_B 3 // DT
3#define ENCODER_BUTTON 4 // SW
4
5volatile int encoderPosition = 0; // ตัวแปรสำหรับนับจำนวนการหมุน
6int lastEncoderPosition = 0;
7bool buttonState = HIGH;
8bool lastButtonState = HIGH;
9
10void setup() {
11 // กำหนดขาเป็น input และเปิดใช้งาน pull-up resistors
12 pinMode(ENCODER_PIN_A, INPUT_PULLUP);
13 pinMode(ENCODER_PIN_B, INPUT_PULLUP);
14 pinMode(ENCODER_BUTTON, INPUT_PULLUP);
15
16 // ใช้ interrupt เพื่ออ่านค่าจาก Rotary Encoder
17 attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_A), updateEncoder, CHANGE);
18
19 Serial.begin(9600);
20 Serial.println("Rotary Encoder Test (No Library):");
21}
22
23void loop() {
24 // อ่านค่าปุ่มกด
25 buttonState = digitalRead(ENCODER_BUTTON);
26
27 // ตรวจสอบการเปลี่ยนแปลงของปุ่มกด
28 if (buttonState == LOW && lastButtonState == HIGH) {
29 Serial.println("Button Pressed");
30 }
31
32 lastButtonState = buttonState;
33
34 // แสดงผลตำแหน่งปัจจุบันเมื่อมีการเปลี่ยนแปลง
35 if (encoderPosition != lastEncoderPosition) {
36 Serial.print("Position: ");
37 Serial.println(encoderPosition);
38 lastEncoderPosition = encoderPosition;
39 }
40
41 delay(10); // หน่วงเวลาเล็กน้อยเพื่อให้การอ่านค่ามีความเสถียร
42}
43
44void updateEncoder() {
45 // อ่านค่าจากขา A และ B
46 int aState = digitalRead(ENCODER_PIN_A);
47 int bState = digitalRead(ENCODER_PIN_B);
48
49 // ตรวจสอบทิศทางการหมุน
50 if (aState == bState) {
51 encoderPosition++;
52 } else {
53 encoderPosition--;
54 }
55}
561#define ENCODER_PIN_A 2 // CLK
2#define ENCODER_PIN_B 3 // DT
3#define ENCODER_BUTTON 4 // SW
4
5volatile int encoderPosition = 0; // ตัวแปรสำหรับนับจำนวนการหมุน
6int lastEncoderPosition = 0;
7bool buttonState = HIGH;
8bool lastButtonState = HIGH;
9
10void setup() {
11 // กำหนดขาเป็น input และเปิดใช้งาน pull-up resistors
12 pinMode(ENCODER_PIN_A, INPUT_PULLUP);
13 pinMode(ENCODER_PIN_B, INPUT_PULLUP);
14 pinMode(ENCODER_BUTTON, INPUT_PULLUP);
15
16 // ใช้ interrupt เพื่ออ่านค่าจาก Rotary Encoder
17 attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_A), updateEncoder, CHANGE);
18
19 Serial.begin(9600);
20 Serial.println("Rotary Encoder Test (No Library):");
21}
22
23void loop() {
24 // อ่านค่าปุ่มกด
25 buttonState = digitalRead(ENCODER_BUTTON);
26
27 // ตรวจสอบการเปลี่ยนแปลงของปุ่มกด
28 if (buttonState == LOW && lastButtonState == HIGH) {
29 Serial.println("Button Pressed");
30 }
31
32 lastButtonState = buttonState;
33
34 // แสดงผลตำแหน่งปัจจุบันเมื่อมีการเปลี่ยนแปลง
35 if (encoderPosition != lastEncoderPosition) {
36 Serial.print("Position: ");
37 Serial.println(encoderPosition);
38 lastEncoderPosition = encoderPosition;
39 }
40
41 delay(10); // หน่วงเวลาเล็กน้อยเพื่อให้การอ่านค่ามีความเสถียร
42}
43
44void updateEncoder() {
45 // อ่านค่าจากขา A และ B
46 int aState = digitalRead(ENCODER_PIN_A);
47 int bState = digitalRead(ENCODER_PIN_B);
48
49 // ตรวจสอบทิศทางการหมุน
50 if (aState == bState) {
51 encoderPosition++;
52 } else {
53 encoderPosition--;
54 }
55}
56
3.ผลลัพธ์
เมื่อหมุน Encoder ไปทิศทางนึง จำนวนนับจะเพิ่มขึ้น หมุนกลับทางจะลดลง
.png?alt=media&token=a181df4d-6a90-4f54-9738-733c1c329cc3)
