English
Español
yt_link
insta_link
fb_link
twitter_link
ADVERTISERS
ANENG
cooleme TV
PCBWay Ad







AFFILATE




Anet A8





Gearbest Anycubic I3 MEGA Full Metal Frame FDM 3D Printer
Anycubic I3 MEGA Full Metal Frame FDM 3D Printer only $379.99 with coupon "Anycubici3ch"
Gearbest Anet A8 Desktop 3D Printer
Anet A8 Desktop 3D Printer only $145.99 with coupon "A8KIDA"


Gearbest Tronxy X3S Aluminum Frame LCD Screen  3D Printer DIY Kit
Tronxy X3S Aluminum Frame LCD Screen 3D Printer DIY Kit only $259.99 with coupon "GBX3S"


Gearbest Min DS202 Oscilloscope 113€
Min DS202 Oscilloscope 113€


Gearbest UTD2052CL DIGITAL 2-ch Oscilloscope 275€
UTD2052CL DIGITAL 2-ch Oscilloscope 275€


Gearbest UT139C True RMS Digital Multimeter 29€
UT139C True RMS Digital Multimeter 29€


Gearbest SBK8586 Soldering station Hot Air + Iron 63€
SBK8586 Soldering station Hot Air + Iron 63€


Gearbest BAYANGTOYS X21 Brushless RC Quadcopter
BAYANGTOYS X21 Brushless RC Quadcopter 153€


Rotary encoder arduino code


Download the .zip file below. Unzip it and open it in Arduino IDE. Compile and upload.










 /*
  Rotary encoder test
  http://www.electronoobs.com
  http://www.youtube.com/c/ELECTRONOOBS
 */
 
 #define clk 8
 #define data 9
  
 int counter = 0; 
 int State;
 int LastState;  
 
 void setup() { 
  //Define the pins as inputs
   pinMode (clk,INPUT);
   pinMode (data,INPUT);   
   Serial.begin (9600); //Start serial com so we could print the value on the serial monitor
   // Reads the initial state of the clock pin
   LastState = digitalRead(clk);   
 } 
 void loop() { 
   State = digitalRead(clk); // Reads the "current" state of the clock pin
   // If the previous and the current state of the clock are different, that means a step has occured
   if (State != LastState){     
   // If the data state is different to the clock state, that means the encoder is rotating clockwise
     if (digitalRead(data) != State) { 
       counter ++;
     } else {
       counter --;
     }
     Serial.print("Position: ");
     Serial.println(counter);
   } 
   LastState = State; // Updates the previous state of the clock with the current state
 }