Rotary Encoder Module

  • Rotary Encoder Module
Below you can see the corresponding code:
An example project for the Rotary encoder.
  /* Author: Danny van den Brande, Arduinosensors.nl
 This is a example on how to use the KY-040 Rotary encoder. 
 Its very basic but if your new to arduino or could not find 
 any code, then you have something to start with.
 because there is little documentation about the KY sensor kit.
 */
 int CLK = 9;  // Pin 9 to clk on encoder
 int DT = 8;  // Pin 8 to DT on encoder
 int RedLed = 4;// You do not need to use the leds. 
                // you can take a look in the serial monitor if you dont have leds.
                // there it will display values. 
 int GreenLed = 5;
 int BlueLed = 6;
 int RotPosition = 0; 
 int rotation;  
 int value;
 boolean LeftRight;
 void setup() { 
   Serial.begin (9600);
   pinMode (CLK,INPUT);
   pinMode (DT,INPUT);
   pinMode (RedLed, OUTPUT);
   pinMode (GreenLed, OUTPUT);
   pinMode (BlueLed, OUTPUT);
   rotation = digitalRead(CLK);   
 } 
 void loop() { 
   value = digitalRead(CLK);
     if (value != rotation){ // we use the DT pin to find out which way we turning.
     if (digitalRead(DT) != value) {  // Clockwise
       RotPosition ++;
       LeftRight = true;
     } else { //Counterclockwise
       LeftRight = false;
       RotPosition--;
     }
     if (LeftRight){ // turning right will turn on red led.
       Serial.println ("clockwise");
       digitalWrite (RedLed, HIGH);
       digitalWrite (GreenLed, LOW);
     }else{        // turning left will turn on green led.   
       Serial.println("counterclockwise");
       digitalWrite (RedLed, LOW);
       digitalWrite (GreenLed, HIGH);
     }
     Serial.print("Encoder RotPosition: ");
     Serial.println(RotPosition);
     // this will print in the serial monitor.
     
   } 
   rotation = value;
 } 
Specifications
Working voltage: 3.3v-5v

Write a review

Please login or register to review

Rotary Encoder Module

  • Webwinkel Keurmerk en klantebeoordelingen

  • 14 dagen bedenktijd 
  • Verzendkosten voor eigen rekening
  • Product Code: KY-040
  • Availability: 13
  • 2.29€

  • Ex Tax: 1.89€

Similar Products

Mini Reed Module

Mini Reed Module

Below you can see the corresponding code: An example of a project for the mini reed switch//Author:..

2.36€ Ex Tax: 1.95€

Key Switch Button Module

Key Switch Button Module

Below you can see the corresponding code: An example project for the KY-004//Hello world! This time..

2.49€ Ex Tax: 2.06€

Photoresistor Sensor Module

Photoresistor Sensor Module

Below you can see the corresponding code: An example of a project for the KY-018/* Author: Danny v..

1.95€ Ex Tax: 1.61€

2 Color LED Module RG Small

2 Color LED Module RG Small

Below the corresponding code:// Arduino test code for KY011 int redpin = 11; // select the pin for ..

2.29€ Ex Tax: 1.89€