Introduction: Reading 15 Rotary Encoders With Arduino and ATmega1284

Here I come back with a new article related to my old MIDI project.

I must say that I started working on this project a few years ago. It all started with installing a demo version of FL Studio. I'm not a musician and I have not musical training, but I enjoyed to to play with different virtual instruments/ synthesizers that we have found in this DAW (Digital Audio Workstation) application.

Since I was not interested to save my "work" I still have the demo version of FL Studio, now reached version 11. Soon I was to discover that I was not happy anymore with the "Typing keyboard to piano" option and and I wanted to try a real MIDI keyboard.

So , one day, I bought from a local store this MIDI keyboard: MAUDIO Keystation 61es. Main criteria at that time was the price, because I did not want to spend too much on a "secondary hobby" (ok, secondary for me). I was very pleased with the product, and I spent many hours trying different sounds, tones, timbres.

And soon I felt the need for a more advanced MIDI controller, with many knobs, buttons, potentiometers. So I decided not to buy a product available on the market, but to try to make one, using my experience as microcontrollers developer.

To return to the topic, this article is the result of my experiments in an attempt to read/manage (as much as possible) rotary encoders, for the purpose of be used in a home-made MIDI controller.

I decided to use Arduino enviroment to make the project available (for tweaking / modifying) to many.

Later edit:

This article was in draft mode for over a year. Meanwhile I completely gave up Microsoft Windows... and (most) proprietary software... So I tried with LMMS on Linux ... :) ... it worked very nice.

Off-topic: I tried demo version of FL Studio for windows in Linux (with Wine). I did not expect to work. But it worked.

Step 1: Components

Not much to say at this step.

Just minimal components to put into operation the microcontroller ATmega 1284.

And the cheapest 15 rotary encoders that I could find. ( 50pcs/$11.70 in August 2014)

As I anticipate that I will go with the experiment even further, I bought 100 of these and I'll probably buy more... Indeed the price was critical for me... But I have learned (with great joy) that they do their job without any problems.

Step 2: Schematic

As we can see, 15 is the maximum number of encoders which we can read (directly) with a 40 pins Atmega.


  • DIP 40 => 32 GPIO.
  • 2 Pins/ Encoder => 16 Encoders.
  • 2 Pins (RX/TX)reserved for outside communication => 15 Encoders

Step 3: Mounting Encoders

I know. It looks ugly. But it is only a temporary setup.

Also I wanted to test the optimal distance between knobs. A matter of ergonomics, if I can use the some knobs without getting tangled by other knobs. For now this is the result I reached.

Step 4: Mounting ATmega1284

Looks worse than previous step. I did not bother to make it look nice. I am impatient to test the algorithm operation in real conditions. (You can be sure that I will make a PCB for this project).

Step 5: Code

What nice image to put on this step? First of all I attach the code. Then some remarks about it.

The code is based on finite state-machine (FSM) which was presented here: Rotary encoders – Experiments. Article and project is over a year old. At that time I spent few weeks to figure how to use this technique, and I watched some classes about FSM on youtube... The difficulty it was not to get from FSM to C code, but to set right all states and transitions. There were some things that I was fooled at first.

Another remark is that I have to use PCINT interrupts and not INT interrupts (and not pooling). This is why I did not use older DIP40 microcontrollers (like ATmega32 or Atmega16)... they do not have PCINT.

The code itself is very simple an short... It would certainly fit in a ATmega644... At that time I had no one; and I have not defined any 644 board in Arduino IDE. But there is already available "boards" for ATmega644, If you have one ready to use, worth trying.

Step 6: Conclusions

When I started this experiment I wanted to answer to the following questions:

  • Can I manage a greater number of rotary encoders with this kind of microcontroller?

Yes, with this combination: PCINT interrupts and FSM algorithm, this is doable.

  • It is this technique/algorithm suitable for this attempt?

Yes. And can be adapted to other microcontrollers... and other purposes (not only MIDI controllers). And I don't need hardware debouncing, so I could skip over 15x2 ceramic capacitors...

  • These cheap Chinese encoders are suitable for this kind of project?

Although I met opinions that claim that these encoders that are not so good. I took a chance and found what I suspected anyway: These are some very good encoders, and I can use them smoothly in my future projects.