Rotary Encoders with the Arduino

Rotary encoders are used to measure rotation. Unlike a potentiometer, they can be rotated infinitely in either direction and measure rotation in discrete steps. The scroll wheel of a mouse and volume knob of a car stereo are examples of encoders found in consumer goods. I pulled a small one out of a radio and setup an Arduino to read it and increment or decrement a variable accordingly. For this simple test I displayed the value on an LCD.

How it works:

The encoder has three pins: A, B and C. The C pin goes to ground while A and B are pulled high through current-limiting resistors. Those two pins output square waves that provide useful information. Clockwise rotations result in the A waveform leading the B waveform. Counter-clockwise rotations result in the B waveform leading the A waveform.

The encoder I used has detents. When the shaft is at rest the A and B pins will have the same voltage (both high or both low.) Each click of rotation results in both pins changing polarity, but with a slight time offset. The logic to interpet the square waves can be summarized as follows:

Clockwise = pins going from low/low to high/low and ending up high/high.

Clockwise = pins going from high/high to low/high and ending up low/low.

Counter-clockwise = pins going from low/low to low/high and ending up high/high.

Counter-clockwise = pins going from high/high to high/low and end up low/low.

Since the low-cost rotary encoders in consumer goods typically use mechanical switches (which short A and/or B to C to produce the square waves) you have to deal with switch bounce -- the metal contacts bounce a little when making or breaking contact. I noticed about 30-60us of switch bounce with my particular encoder:

Capacitors can be used to buffer the change in voltage, which effectively masks the problem. Small caps in the nF range are commonly used. Too small and they won't be effective, too large and you can't interpret fast rotations. Recall the RC time constant: t = RC. The time it takes (in seconds) for a capacitor to charge to ~63% or discharge to ~37% through a resistor equals the resistance (in Ohms) multiplied by the capacitance (in Farads.) I had some 27K resistors and 10nF caps laying around, so the time constant for my circuit is approximately 270us. That's more than enough to compensate for the 30-60us of switch bounce without limiting accurate readings of quick rotations. In this circuit the resistors should be at least 1K to minimize current flow, and caps should be chosen so the time constant is less than 1ms.

Here's the final schematic and code:

#include <LiquidCrystal.h> LiquidCrystal lcd(10, 9, 5, 4, 3, 2); // RS, Enable, D4, D5, D6, D7 int count = 0; byte currentA, currentB, previousA, previousB = 0; void setup() { lcd.begin(16, 2); previousA = currentA = digitalRead(12); previousB = currentB = digitalRead(11); } void loop() { previousA = currentA; previousB = currentB; currentA = digitalRead(12); currentB = digitalRead(11); if(previousA == LOW && previousB == LOW && currentA == HIGH && currentB == LOW) // clockwise count++; else if(previousA == HIGH && previousB == HIGH && currentA == LOW && currentB == HIGH) // clockwise count++; else if(previousA == LOW && previousB == LOW && currentA == LOW && currentB == HIGH) // counter-clockwise count--; else if(previousA == HIGH && previousB == HIGH && currentA == HIGH && currentB == LOW) // counter-clockwise count--; lcd.setCursor(0, 0); lcd.print(count); lcd.print(" "); }

Bolsa Chica Wetlands

First day out with my new DSLR:

Cap'n Crunch RC Car Body

This 1/10th scale touring car body was fun to paint and turned out great. Afters a few hours of vector tracing, cutting some vinyl masks and doing a test panel, here's the end result:

Cheez-It RC Car Body

I finally got around to building the CEN CT4R touring car kit that I was given. Looking through my collection of unpainted bodies I found a 200mm TC body to paint but for the life of me I could not think of a fun theme. The following day at work I went on break and decided to raid the vending machine... guess what caught my eye?

Looks kind of of soupy, I should have painted more Cheez-Its. I guess that's what I get for not painting a test panel.

Tetris and High-Visibility RC Heli Canopies

I finished three canopies recently. This Tetris canopy turned out much better than I anticipated. Silver base, candy colors, black and white for tints/shades. Made extensive use of my vinyl cutter, total time including a test panel was around 6 - 7 hours. It was worth it.

For my Raptor 50 I needed a high-visibility canopy. Yellows and pinks really pop out. I airbrushed a similar design on a smaller canopy a year ago for my T-Rex 450. I added some blue stripes and an Airtronics logo. It's hard to loose orientation now:

The colors on this last canopy are all candies on top of a silver base. The red and orange were marbleized, this was my first attempt at doing that. I like how it turned out. While not obvious in the photo, the marbleized candy red looks like blood spatter / pooling on concrete!

< Prev  8  Next >