본 제품은 로터리 인코더 모듈입니다. 한 회전당 20 펄스를 출력합니다.
로터리 모듈에는 10K 풀업저항이 장착되어 있어 외부 풀업저항이 필요가 없습니다.
SW핀은 스위치 핀으로 GND에 연결되어 있으며, 샤프트를 누를시 동작합니다.
Working voltage: 5V
Pulses per rotation: 20
Pinout
1...GND
2...+5V supply
3...SW connects pin to GND whebn shaft is pressed
4...DT direction pulse. Pin has 10K pullup connected to supply.
5...CLK clock pulse. Pin has 10K pullup connected to supply.
아두이노 코드
/* Define digital pins used to read the encoder */ #define DT 2 #define CLK 3 #define SW 4 void setup() { Serial.begin(9600); pinMode(DT, INPUT); pinMode(CLK, INPUT); pinMode(SW, INPUT); digitalWrite(SW, HIGH); } /* Main program */ void loop() { int counter; byte DialPos; byte Last_DialPos; /* Reset the counter */ counter = 0; /* Continiouously read the state of the encoder */ while(1) { /* Read the status of the dial */ DialPos = (digitalRead(CLK) << 1) | digitalRead(DT); /* Is the dial being turned anti-clockwise? */ if (DialPos == 3 && Last_DialPos == 1) { counter--; } /* Is the dial being turned clockwise? */ if (DialPos == 3 && Last_DialPos == 2) { counter++; } /* Output the counter to the serial port */ Serial.println(counter); /* Is the switch pressed? */ if(!digitalRead(SW)) Serial.println("Switch pressed!"); /* Save the state of the encoder */ Last_DialPos = DialPos; } }
* 유의사항 *
KY-040은 교육용으로 볼트 너트가
없는 경우도 잇습니다
이점 유의바랍니다.