Cov txheej txheem:

Tutorial of Rotary Encoder Nrog Arduino: 6 Kauj Ruam
Tutorial of Rotary Encoder Nrog Arduino: 6 Kauj Ruam

Video: Tutorial of Rotary Encoder Nrog Arduino: 6 Kauj Ruam

Video: Tutorial of Rotary Encoder Nrog Arduino: 6 Kauj Ruam
Video: How Rotary Encoder Works and How To Use It with Arduino 2024, Kaum ib hlis
Anonim
Tutorial ntawm Rotary Encoder Nrog Arduino
Tutorial ntawm Rotary Encoder Nrog Arduino

Rotary encoder yog cov khoom siv hluav taws xob muaj peev xwm saib xyuas kev txav chaw thiab txoj haujlwm thaum tig. Rotary encoder siv lub tshuab ntsuas qhov muag uas tuaj yeem tsim cov mem tes thaum lub tshuab ntsuas lub cav tig. Daim ntawv thov ntawm tus lej nkag teb chaws feem ntau yog siv tshuab lossis neeg hlau txav saib kuj tseem tuaj yeem siv rau kev xaiv zaub mov ntawm cov zaub. Rotary encoder muaj ob qhov kev tso tawm kom nws tuaj yeem paub qhov txawv ntawm qhov tsis zoo (CW) thiab qhov zoo (CCW) kev sib hloov thiab tseem muaj tib lub khawm.

Kauj Ruam 1: Pulse Flow ntawm Rotary Encoder

Pulse Flow ntawm Teb Encoder
Pulse Flow ntawm Teb Encoder

Cov mem tes ntws tsim los ntawm cov lus teb hauv qab no zoo li daim duab saum toj no.

Kauj ruam 2: Pinout ntawm Teb Encoder

Pinout ntawm Rotary Encoder
Pinout ntawm Rotary Encoder

Piav qhia:

  • GND GND
  • + + 5V
  • SW khawm ntawm cov teb encoder thaum nias
  • Cov ntaub ntawv DT
  • Cov ntaub ntawv CLK 2

Ib qho ntawm DT lossis CLK tus pin yuav tsum txuas nrog kev cuam tshuam taw ntawm Arduino Uno, lossis ob qho tib si ntawm DT thiab CLK txuas nrog tus pin cuam tshuam.

Kauj ruam 3: Schematic

Schematic
Schematic
  • GND thiab GND Arduino Uno
  • + à + 5V Arduino Uno
  • SW -PIN 4 Arduino Uno
  • DT -PIN 3 Arduino Uno
  • CLK à PIN2 Arduino Uno

Kauj ruam 4: Code

Code
Code

Hauv cov lus qhia hauv qab no, uas yuav raug siv los cuam tshuam yog PIN 2 ntawm Arduino Uno, thaum tus lej 3 tsuas yog siv los ua ntu zus xwb.

#define encoder0PinA 2 #txhais encoder0PinB 3 #define encoder0Btn 4 int encoder0Pos = 0; teeb tsa tsis muaj dab tsi () {Serial.begin (9600); pinMode (encoder0PinA, INPUT_PULLUP); pinMode (encoder0PinB, INPUT_PULLUP); pinMode (encoder0Btn, INPUT_PULLUP); attachInterrupt (0, doEncoder, CHANGE); } int valRotary, kawgValRotary; lub voj void () {int btn = digitalRead (encoder0Btn); Serial.print (btn); Serial.print (""); Serial.print (valRotary); yog (valRotary> lastValRotary) {Serial.print ("CW"); } yog (valRotary {

Serial.print ("CCW");

} lastValRotary = valRotary; Serial.println (""); ncua (250); } void doEncoder () {if (digitalRead (encoder0PinA) == digitalRead (encoder0PinB)) {encoder0Pos ++; } lwm {encoder0Pos--; } valRotary = encoder0Pos/2.5; }

Hauv kab 10 ntawm daim duab kos saum toj no tau siv los pab cuam tshuam ntawm tus pin 2 Arduino Uno. Hauv qhov "doEncoder" muaj nuj nqi yog xam los ntawm cov teb encoder. Yog tias tus nqi ntawm DT thiab CLK (tus pin cuam tshuam ntawm Arduino Uno) zoo ib yam, tom qab ntawd qhov "encoder0Pos" qhov sib txawv yuav nce ntxiv / ntxiv, ntxiv rau qhov xwm txheej ntawd, "encoder0Pos" qhov sib txawv tau txo qis.

Kauj ruam 5: Piav qhia

Tus nqi ValRotary yog tus nqi ntawm cov lej uas tau ua tiav. ValRotary tus nqi tau txais los ntawm kev ntsuas lub ntsuas qhov ntsuas ntsuas sib faib los ntawm 2.5. Tus nqi ntawm 2.5 tau txais los ntawm qhov kev sim, txij li ib qib ntawm tus lej nkag teb chaws tuaj yeem siab dua 1, yog li faib los ntawm 2.5 rau nws tus nqi raws li qhov nkag mus thiab tseem ntxiv qhov nyeem qeeb.

Thaum nyob ntawm kab 19 - 25 yog txoj haujlwm los txiav txim siab seb lub teb chaws rotary encoder CW lossis CCW. Cov lus piav qhia ntawm kab 19 - 25 yog thaum qhov kev nyeem ntawv nkag teb chaws tam sim no ntau dua li cov ntaub ntawv teb yav dhau los ces tau hais tawm raws li CW. Whereas yog tias kev nyeem ntawv tam sim no me dua li nyeem yav dhau los ces nws tau hais raws li CCW.

Kauj Ruam 6: Tso Tawm

Tso zis
Tso zis

1 = pib khawm tus nqi ntawm cov teb thaum nws tsis tau nias

Pom zoo: