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

Infrared Receiver Module

Infrared Receiver Module

Below you can see the corresponding code.an example project for the Infrared Receiver//Author: Danny..

2.29€ Ex Tax: 1.89€

Temp and humidity Sensor

Temp and humidity Sensor

Below you can see the corresponding code: An example of a project for the DHT11/* Author: Danny va..

2.99€ Ex Tax: 2.47€

NTC Waterproof Thermistor/ Temperature sensor

NTC Waterproof Thermistor/ Temperature sensor

Length 50 cm.Below you can see the corresponding code:An example of a project for the sensor./* Aut..

2.36€ Ex Tax: 1.95€

Heart Beat Pulse Sensor Module

Heart Beat Pulse Sensor Module

Below you can see the corresponding code: An example project for the KY-039/* This example will sh..

1.95€ Ex Tax: 1.61€