As in some of my past instructables, arduino is all about control. The ability to control certain things like displays, servos, motors, and other electrical mechanical devices in a meaningful way. I've covered using potentiometers to determine position and how to drive 7 segmented led displays. This is continuation of that. This is how to directly control a position and display that position on a led display. A potentiometer might work, but it has large limitation. It has a set range of travel. Using a rotary encoder instead of a potentiometer we have almost no limit to how big a number can be set in the position. This might be helpful if you were trying to determine the position of a motor in rotations.

This also is an intro to interrupts. Interrupts are functions that interrupt the main function when something is triggered. Pressing reset on your computer is an interrupt. It allows for small, fast, or critical functions to work in the background.

This instructable uses shift registers to control the 7 segmented display, but they are covered in detail in another instructable. Visit that one for more info. https://www.instructables.com/id/Arduino-powered-7-seg-LED-display-using-Shift-Regi/

ike all my instructables, this one was made at TechShop.

Step 1: Materials

You will need all the materials from this instructable:


- A quadrature rotary encoder. (This means that it has 2 outputs. You need two to determine direction)
Here is an example:

<p>nice, if I want to add sensors , button up ,button down,and reset can you explain code?</p>
I tried this code, but it seems pretty glitchy and only counts full cycles. It does, however, offer a good example for interrupts and grey code (which I had never used before). <br>This code counts half-cycles AND is much more stable. <br> <br>//rotary encoder example by jduffy54. <br>//this picks up rotations of a standard encoder on pins two and seven, and <br>//transmits the results over serial. <br> int r = 0; <br>float pos = 0; <br>float incr = 1; //the ammount you want it to count per step. <br>void setup(){ <br> Serial.begin(115200);//set a high communication speed. using 9600 baud <br> //will cause the program to freeze if the encoder moves too fast <br> pinMode(2, INPUT); <br> pinMode(7, INPUT); <br> digitalWrite(7, HIGH); <br> digitalWrite(2, HIGH);//sets the pins to HIGH if no signal is present <br> //if it is pulled low, the pins will register it. <br> <br> attachInterrupt(0, encoderPos, CHANGE); //go to the new void if pin two changes <br> //this allows us to double the resolution of the encoder. <br> <br> } <br> <br>void loop(){ <br> delay(0.1);//we don't need the loop to do anything, just the interrupts <br> } <br> <br> <br>void encoderPos(void){ //this is a state machine to determine the direction of the encoder. <br>//every half-cycle it counts, effectively doubling the encoder resolution. <br> r = 1; <br> if(digitalRead(2) == HIGH){ <br> if(digitalRead(7) == LOW){ <br> while(r == 1){ <br> if(digitalRead(2) == LOW){ <br> r = 0; <br> } else { <br> if(digitalRead(7) == HIGH){ <br> pos = pos + incr; <br> r = 0; <br> }}} <br> <br> <br> <br> } <br> else { <br> while(r == 1){ <br> if(digitalRead(2) == LOW){ <br> r = 0; <br> } else { <br> if(digitalRead(7) == LOW){ <br> pos = pos - incr; <br> r = 0; <br> }}} <br> } <br> Serial.print(pos); <br> }else{//same as the above code, but with High-Low reversed. <br> r = 1; <br> if(digitalRead(7) == HIGH){ <br> while(r == 1){ <br> if(digitalRead(2) == HIGH){ <br> r = 0; <br> } else { <br> if(digitalRead(7) == LOW){ <br> pos = pos + incr; <br> r = 0; <br> }}} <br> <br> <br> <br> } <br> else { <br> while(r == 1){ <br> if(digitalRead(2) == HIGH){ <br> r = 0; <br> } else { <br> if(digitalRead(7) == HIGH){ <br> pos = pos - incr; <br> r = 0; <br> }}} <br> } <br> Serial.print(pos); <br> }}
nice use of interrupts. are you planning on using this rotary encoder in any other projects?
Not currently planned for any more instructables, but I am currently using a few in some of my private projects. I particularly like the ones I'm using here though.

About This Instructable




More by madebyjoe:Arduino powered GLCD (Graphic LCD) - I made it at TechShop Arduino Bluetooth Serial Connections! - I made it at TechShop Android Tablet PS3 / Xbox360 Controller Mount - I made it at TechShop 
Add instructable to: