HOME

All the secrets of an "misterious part "

EE: Encoder Experiments!

Digital knobs| Schematics | Build | Detection | Revolving LEDs | Bar LEDs | LED sequence

 

Digital knobs

Modern devices include encoders more and more frequently. They are the digital counterpart that replaces old rotary switches and potentiometers, in a way that is simple an misterious at the same time. Generally speaking, their purpose is to convert rotation of a shaft or spindle to digital pulses. Depending on the application, the shaft in question can range from the rotor of an electric motor in a packaging machine to the volume knob on your car stereo. Here are some examples:

  • the volume knob on you car stereo
  • main syncing mechanism on automatic machines as bis as a newspaper printing press
  • timer on microwave ovens
  • oscilloscope time-base selector
  • car air-conditioning temperature dial

Dimension, robustness, electrical characteristics and reliability can vary, but as long as we look at incremental (relative) quadrature encoders,the pulse output looks always the same.

Most encoders used in electronics look like potentiometers: a small plastic or metallic box with a shaft protruding out and three pins for connection.

For all-mechanical encoders (opposed to optoelectronic o hall-effect encoders replacing mechanical switches with optical or magnetic ones) one pin is a common wire, shared by tho normally-open switches. We consider mechanical encoders as they are most commonly used in consumer electronics devices.

The part we use in this experiment is manufactured by ALPS, and provides 24 pulses per revolution. Each pulse corresponds to a mechanical "click" on the shaft. 24 pulses/revolution are perfect, among other things, for setting a clock, but parts with 36 or 48 pullses/rev are popular as well.

If you get your hands on an encoder from machinery automation markets, more likely you will find a continuous movement encoder (no clicks). Most encoders of this kind include an optoelectronic circuit (like mechanical PC mouse) and are capable of giving much more pulses per revoltuion, e.g. 256 or 360 pulses/rev for am angular resolution of just one degree.

Electronic encoders need more pins to power the internal electronic circuit. However, these are specilist parts so we are not going to explain how they work mora than this.

In this section you will see:

  • how to connect an encoder to a Nutchip
  • how encoders pulses are interpreted in order to rotate a LED light

 

A few steps with an encoder...

Most encoders provide tactile feedback, meaning that rotating them you can feel a series of clicks (steps) on the knob.

Relative encoders (opposed to absolute encoders) are essentially a pair of switches. Usually they have just three pins (instead of the four required by two switches) because two contacts are joined to save pins.

Most encoders used in portable devices are built such that both switches (A and B) are open when the shaft is in resting position. This saves power when the knob is not touched.
Moving the shaft, the switches close in a well-defined sequence...

Moving the knob clockwise, the first switch to close will be the one marked with A.
Coninuing its clockwise rotation, when the shaft is halfway between two resting points both the switches are closed.
Right after the clockwise rotation ends, switch A will be open again, leaving closed only switch B.

At last, when the knob clicks in the next resting position, both switches will be open again.

The sequence repeats uinchanged on successive clicks. As regards counter-clockwise rotation, the sequence will be similar, but the switches close in a reversed sequence (that is first B then A). This way, you can detect not only the number of steps an encoder moved, but also the direction (clockwise or counterclockwise) it moved.

Schematics

Mechanical encoders are the same as a pair of ordinary switches. You can think of the as incorporating two pushbuttons (A and B), the get pressed in an ordered sequence as the shaft turns. Nutchips are designed to connect directly to pushbuttons. Ordinary pushbuttons connect between an input pin and GND. For encoders, connect the common pin, COM, to Nutchip's GND, the A pin to input 1, and B pin to input 2:

How to connect a mechanical relative encoder to make simple experiments

As for the outputs, connect 4 LED with respective current limitng resistors as shownper limitare la corrente massima.

Breadboardin

Parts list:

4 x 390 ohm resistors (1/4W o higher)
4 x LED
1 x 100nF ceramic capacitor
1 rotary encoder ( ALPS)
1 x othree-pin, 4 MHz ceramic resonator (Murata)
1 x Nutchip (NUT01AK)

The photo show how to assemble the circuit on a solderless breadboard. Be sure to check all of the wire jumpers and not to reverse the Nutchip or the LED diodes. LED catode (K) is usually marked flattening the case and/or by a shorter leg.

To connect the necoder to the breadboard, you can solder three short wire jumpers to its pin. The encoder we used had 5.08 mm spaced pins, that is twice as ordinary ICs (2.54 mm). This means that encode pins can be fitted into IC sockets leaving a free pin between them. So we cut a standard IC socket to make a customized "encoder socket" to use for experiments, leaving the original as new!

Detecting rotation

Detecting shaft rotation is easy: just wait that one of its switches - A or B - close. The kind of sequence (A fors or B first) tells you wheter the know moves clockwise or counterclockwise. From a look to our schematic discgram, A connects to IN1 and B connects to IN2. When the switch is open, the input will read 1, otherwise 0.

Clockwise rotation
Counterclockwise rotation
rest position
IN1 = 1, IN2 = 1
rest position
IN1 = 1, IN2 = 1
starting rotating clockwise (A)
IN1 = 0, IN2 = 1
starting rotating clockwise (B)
IN1 = 1, IN2 = 0
middle rotating clockwose (A+B)
IN1 = 0, IN2 = 0
middle rotating clockwose (A+B)
IN1 = 0, IN2 = 0
ending rotating clockwise (B)
IN1 = 1, IN2 = 0
ending rotating clockwise (A)
IN1 = 0, IN2 = 1

A Nutchip's truth table tracks movements thorugh successive states according to the tables above. We choose to trigger detection when both input appera closed (A+B), jumping to a midlle-state waiting for the next step. Depending on the input that re-opens first, we will step to the next or previous states according to wich switch re-opens first (A for clockwise, B for counterclockwise).

Revolving LEDs

First experiment is to try some revolving lights. Turning the encoder's shaft, the light on the LED will appear to rotate the same way. This way of working is the same as the old 4-way rotative switch, selecting on-of-four outputs. This is an endless implementation, that is once it rotates over the last LED, the cycle start again from the first LED.The file to load for this experiment is encoder_rot.nut.

LED bargraph

This experiment lits the LEDs in a progressive way. This kind of display gives the impression of something that increases or decreases, and it is ideal, for example, for a volume control or a motor speed control. Differently from the experiment above, once all the LEDs are lit, any further rotation does not restart the visuailation from zero, instead any extra rotation is rejected.
File to load is encoder_bar.nut

LEDs with arbitrary sequence

Previuos experiments powered LED lights according to a somehow "ordered" squence, one after another. Actually, we can set-up a table that fires the LEDs just in any sequence we want. As an example, using differnt colour LEDs (green, yellow and red) you can simulate the typical sequence of traffic lights.

Binary counter

A special case of arbitrary sequence is when the outputs follow exactly the sequence made from successive binary numbers. With four outputs, we can count from 0 to 15, for a total of 16 different values. Such a sequence is useful to drive, for example, a display.

Those with some experience in electronics probably imagine how to do it... Everybody else can see how to drive a display with next projecy, an encoder based display .