Release 2017.4.20 / Update 2017.10.22

Using rotary encoder part 2 : High-Res with “non-click”

Previously, I wrote about efficient Sketch how to use click type Rotary encoder. But, it wasn’t for non-click type encoder.

So I will try write new Sketch for it, and more, it makes worth to High-Resolution. No need extra electronic parts, just a Sketch.

Of course, it is not a perfect method. If you want it, you should get more expensive parts like “absolute-type” encoder. But I think my Sketch will works pretty well.

New sketch is available on Part 3 (2017/10/22)

Wiring

Necessary electric parts is non-click Rotary Encoder, and LED if you have. Digital pins for Rotary Encoder is pull up by inside Arduino.

Rotary Encoder

Generally, rotary encoder judges “forward” or “backward” by comparing 2 digital pins of the current combination and the last. And “click-type” is consist of 4 different combination as one “click”. So if you have “20 click” encoder, there are 80 tangent point for each rotation on it.

If we can pick up all the change, we could get more high-resolution control by the encoder. That’s the what I want.

You can buy “non-click-type” rotary encoder on shops.

But most of those are fewer tangent point, and it hard to find one which have many tangent point. So I find “click-type” encoder which has more tangent point, and remodel to “non-click-type” physically.

Remodeling Rotary Encoder

Do take your responsibility for your own if you will try this. I will never take for yours.

This idea was coming from this article.(Sorry, this page is in Japanese only.)

The mechanism of “click” is generated by protulutions meshing with the rotating table inside. And it becomes “non-click” if you remove it.

In this article, I use this Rotary Encoder which has 24 clicks.

It will have 96 resolution, in my theory.

Remove caulking that connects the foundation.
You can see mechanism of tangent point.
When further separated from the knob, you can see what makes “click” mechanism. So bend the protrusion not to touch ditch.

Mechanism for “click” differs from each type. So this way is reference.

Sketch

I noticed a miss in the Sketch and modified it.(Ver1.1) So it may improved encoder-read. (16/10/2017)
New sketch is available on Part 3. (22/10/2017)

This method uses “MsTimer2” library. So if you haven’t installed it yet, get from “Library Manager”.

Download this project file,

Sketch:High Res Rotary Encoder

or copy code of down below.

If you fail to compile, read here.

I recommend you to use “project file”. Because it is tabbed.

How to use is very simple. Set value to “ENC_COUNT()”. And it returns applied value.

In sample code, you can see ‘val’ is printed to “serial monitor”.

Also, you can check the behavior by Arduino “serial plotter”.(Ctrl+Shift+M)

Although it can’t catch fast change, it won’t be extreme bullshit value.

It becomes jagged if it failed.

I haven’t used this code with other routine so far. If you like this sketch, let me know your comment.

Incidentally, I explain about how it works as reference.

On my theory

First of all, we have to recognize how many “chattering” occurred.

Watch the “Chattering”

By using attachinterrupt to record and display pin changes. There is a problem use attachinterrupt and Serial.print use same time in a function. So it keeps the changes in arrays and prints all after intervals. (Because this sketch function slightly wrong, data of first and last goes wrong. Never minds)

There are so many “chattering” on. “A” to “D” means pattern of encoders 2 pin. So you can see the patterns go back and forth repeatedly.

Sometimes it repeats over 100 time, on the other hands, it never appears. On the fast move of encoder, you can’t tell which pattern is correct.

Counterplan

On this situation, attachinterrupt may not helps counting.

So I thought to use “gauging” which I invented and “timer” as flash update.

  • confirmed position is “Home”
  • latest position is “Current”

I judge a position by comparing those two elements.

I wrote a chattering-less switch library with “gauging” method.
(26.6.2017)

If “Home” and “Curr” is different, “Trigger” goes on. But it isn’t confirmed yet. By triggered, digital pins is read repeatedly.

And it watches changes of pins by two gauge. If one of them reaches value of “ENC_JUDGE”, it is comfirmed “moved” or “not moved” by the gauge’s value. If it moves, update “Home” position and count. Or abort if it is “not move”.

This is the basic task for reading pins.

Process and Constant

Patterns for 2 pins can be expressed by 2 bits. So, I set several encoder’s status to a byte in variable “edc_status”.

Those values is manipulated by “bit-shift”. So I made some functions.

Then, I create a function to read the pin state.

Timer works to this “ENC_GAUGE”.

By not to update encoder change immediately, but charge to function “ENC_COUNT()”, prevent mistakes and it makes easy access to counter value.

Adjustment

This sketch is optimized for Arduino Uno. So it may not works as well if you use another type Arduino. Several changes of constant may helps for your trouble shooting.

  • ENC_JUDGE
  • TIM::set(1, ENC_GAUGE);

ENC_JUDGE value is for “repeating times” and “judging gauge”. If wobble is terrible, changing here may resolves it.

“Timer” is set to 1ms. This is minimum value. But if you use faster one than Arduino Uno, it may not necessary to be 1ms.

By this explain, I realized some code are irrelevant. Those were abandoned on my try and error. But as it are. Sorry. Modify it yourself if you care about.

Reference Site

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.