Rotary encoder 4 choices

Can someone help me with a sketch

i want to use a rotary encoder to choose 4 directions north, south, west, and east When a direction is choosen i want to set an digital output pin high.

Please can someone help me with this as i am not familiar with rotary encoders and the coding of them.



You might want that but the problem with rotary encoders is they show relative motion how many steps (clockwise and counter clockwise) have been made since the starting of the program, it doesn’t report absolute position of the rotation axis.

So you need to either use an extra method to reset the current encoder position as a given direction (push a button to tell arduino encoder points to N)

Or use a different type of encoder (e.g. magnet on a wheel + magnetometer) that gives you absolute position values.

i want to use a rotary encoder to choose 4 directions north, south, west, and east

I would consider a 4 position rotary selector switch instead.


If you need to indicate all the positions of the compass rose you will need an ABSOLUTE ENCODER.

If all you want are the four prime points cattledog's suggestion would be the best and easier to code..

Tom... :)

Hi all thank you for the ideas and contribution

Cattledog solution is the one i need i think

I just need it for 4 choices, the encoder i have has also a push button if a choice is made for instance south i put a digital output high if i rotate for instance to west i put the another digital input high an the digital input of south low

That would be the setup, but how to code this with a rotary encoder ?

Greetings Fred

If you only need 4 directions I would go with a rotary and an LCD menu, scrolling back and forth through North, E, S and W, selecting the desired direction with a press on the rotary button. This will then trigger the required action (where you switch OFF the previous LED and switch ON the selected direction LED - this requires that you “note” in the same function, which LED is currently ON, or - simplest way - switch OFF all other LEDs, despite of that 2 of them definitely are already OFF).

I recently posted in the German speaking forum a simple menu, based on a rotary encoder and its button to scroll through a “flat = 1 layer” menu. I think that is pretty simple and a good starting point for you.

The comments in the sketch are in English, so the English speaking people won’t be lost.
I am sure that some more experienced guys here could improve the sketch, but for me - it’s doing what I wanted to achieve.

So have a look at it and replace the “cocktails” by your own application.

Here is the link to the project (my code is in post #6).

In that post I have attached Ben Buxton’s rotary encoder library, which, after extensive testing of numerous other libraries or direct codings, turned out to be by far the best one. My rotary encoder is a very cheap and simple one, with that library you don’t need interrupt pins (in the sketch I used them - but they are not actually needed, if you have to use them for other activities in your code).

The original library of Ben is available on github plus there you can find a modified one. But both don’t come with the button supported as in “my” library. In fact it is a library which I found last year somewhere in the I-net and it is actually the same algorithm as Ben is using in the library on his blog, but - I have no idea where I found the attached one with the implemented button support incl. debouncing.

So best luck and good start. Let us know when you are a step further. (9.31 KB)

Hi rpt007

Thank you for helping me i will download this code and have a look at it this evening

I'll keep you informed