Pages: [1]   Go Down
Author Topic: Pushbutton rotary encoder with only one pin?  (Read 1085 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 318
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,

I currently have a project with a user interface that consists of an LCD screen and a small number of momentary pushbuttons. Users interact with the buttons to control the behavior of the device.

The buttons are a bit awkward, which has me thinking about replacing them with a pushbutton rotary encoder. The user would scroll through menus by turning the encoder, click it to choose a setting, then spin the encoder to increment or decrement the value. I can picture this having a sort of ipod-like simplicity and I really like it.

However, I don't have a bunch of free pins sitting around. The pushbuttons are working on an analog input pin with a series of resistors acting as voltage dividers such that I can detect which button was pushed and act accordingly. Has anyone ever done this approach with a rotary encoder? Does it strike anyone as being within the realm of possibility?

I've seen the rotary encoder page on the playground:

http://arduino.cc/playground/Main/RotaryEncoders

but after a quick scan it looks like all of those examples require multiple pins (worst case, interrupt pins). I don't care a ton about accuracy or speed. I have no experience with rotary encoders so I plan on experimenting a bit with this, but I figured I'd throw the question out to see if someone else had already solved this. Looking at the graph of "a" and "b" waveforms in the first example in the playground, it strikes me that you could put the two on one pin with a resistor divider, and then just work out the maths to determine which transitions indicated which state(s) for the encoder.
Logged

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 121
Posts: 8446
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

AFAIK you will need at least 2 pins if you don't care about the direction the knob is being turned and three pins if you do.

Can you use a shift register to free up some pins elsewhere?

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 121
Posts: 8446
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OK just thinking out load here. You could use a Nx1 rotary switch (remove the stopper) to select various resistors and feed that into an analog input. You could detect the turning in either direction that way but I've not seen a rotary switch that also has a push button.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Shannon Member
****
Karma: 161
Posts: 10438
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rotary encoders typically have a common terminal for the A and B channels and the pushbutton.  This common terminal is normally connected to ground and the 3 outputs are sent to individual pins with pull-ups enabled.

However if you connect the common terminal to an analog pin, and via a 10k resistor to ground, then each of the other pins can be fed +5V via a different value resistor (say 4k7, 10k, 22k) then you will get a unique voltage at the analog pin for each of the 8 possible states I think...
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
God Member
*****
Karma: 10
Posts: 651
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I really doubt that an analog read would be anywhere near fast enough to catch a rotary encoder.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 318
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


However if you connect the common terminal to an analog pin, and via a 10k resistor to ground, then each of the other pins can be fed +5V via a different value resistor (say 4k7, 10k, 22k) then you will get a unique voltage at the analog pin for each of the 8 possible states I think...

This is what I had in mind, more or less.

Quote
I really doubt that an analog read would be anywhere near fast enough to catch a rotary encoder.

Care to explain? How fast does something need to be to catch a rotary encoder? This will be an input on a UI, i.e. a human will be turning a knob.
Logged

Offline Offline
God Member
*****
Karma: 10
Posts: 651
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



Quote
I really doubt that an analog read would be anywhere near fast enough to catch a rotary encoder.

Care to explain? How fast does something need to be to catch a rotary encoder? This will be an input on a UI, i.e. a human will be turning a knob.

When I last played with a rotary encoder, I had it set up to give an interrupt on pin 2 and then read the state of the other pin with a digital read.  I kept missing clicks or having the thing go backwards on me.  I put it on my scope and found that for one detent (click) of the encoder there was a complete cycle of the waveform.  digitalRead was too slow to pick it out.  So I had to read the port directly. 

It may work different for you, try it out.  But an analog read takes a while, and I'm thinking the pulse will be gone halfway through it. 
Logged

Anchorage, AK
Offline Offline
Edison Member
*
Karma: 37
Posts: 1147
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also keep in mind that rotary encoders can be kinda jittery.  I recommend a hardware debounce, which might be a little more complicated using a resistive network.  Maybe not, but it would sure help to see the waveforms on a scope first.  Do you have one?

On a related note...  The nice thing about the interrupt / digital read is that the signal is either above the comparator or not.  On an analog read, you could potentially be sampling somewhere along the LOW / HIGH transition.  So, if your debounce slows the transitions much, you have that statistic to contend with as well.

Even with a human turning the knob, quadrature encoders are really chatty.  You may need to change the ADC resolution to get faster samples (both to catch the pulse and to finish reading quickly enough to be available to catch the next one), which will of course make your readings less precise.

In other words, it's entirely possible, but it will likely complicate things.  IMHO, encoders are complicated enough on "slow" GP microcontrollers as it is.  You could always look into a dedicated driver IC that speaks serial, or get a knob with such a thing built in.  Just a thought.
Logged

0
Offline Offline
Shannon Member
****
Karma: 161
Posts: 10438
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

analogRead takes about 0.1ms, so it should be fast enough assuming no/little contact-bounce.  But with a single pin there is the problem of recognising contact bounce in the first place, which I have glossed over.
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: