An Electronic Counter Project for Digital and Analog Signals, including Quadrature Encoders

My brother told me that he plans to make his own guitar pickups by hand. However, he needs a device to measure the number of turns of magnet wire as they are wrapped around the core.

Electrical counters can increment a number once or multiple times per rotation. They can even decrement a number when something rotates backwards. But, counters aren’t limited to just rotating devices. Electrical counters are used in a variety of industrial applications -- such as counting the number of jelly beans that pass in front of a light sensor.

Although counters are available at most electronic suppliers, I thought it would be fun to build my own multi-purpose generic counter. The resulting counting device reads analog or digital pulses on one or two sensors (such as a DC motor quadrature encoder). The count is shown on a bright, crisp OLED (organic light emitting diode) screen.

BroMatic 2000 automatic electronic counter. When every turn counts -- count on the BroMatic.

BroMatic™ 2000 automatic electronic counter. “When every turn counts -- count on the BroMatic.”

Like all of my other over-engineered products, the BroMatic™ 2000 counter has numerous options to allow a wide variety of analog and digital sensors and encoders to be connected. Sensor and motor wires screw onto the terminal blocks shown at the top of the picture.

An attached motor can be turned on and off by a switch. Or, the switch can be toggled to “auto” mode to have the motor automatically turned off by software when the user adjustable count has been reached.

Lastly, motor speed is user adjustable because the device includes a pulse-width modulator.

Sensor Choices

The most reliable and convenient sensor for measuring motor rotations is probably a quad encoder attached to the motor shaft. However, other acceptable sensor choices include reflective-pair or interrupt-pair infrared sensors, hall-effect / magnetic sensors, or even mechanical switches.

There are basically two types of sensor outputs: digital and analog. For digital sensors, the counter device accepts a square wave signal with a low between -0.5 V to 1.5 V and a high between 3.5 V and 5.5 V.

For analog sensors, the user can set the low and high trip points for any value between 0 V and 5 V in steps of about 0.02 V. Although analog is more adjustable, the device can read much faster digital signals (3400 analog pulses per second versus 66,666 digital pulses per second.)

The counter device has two sensor inputs, A and B. There are five different modes to handle the various combinations of sensors:

Quadrature sensors act together in a specific pattern that allows the device to ignore sensor glitches / electrical bounces as well as to determine rotational direction. The most accurate count is in quadrature mode with digital sensors. The topic of quadrature encoding is explored later in this article.

The only difference between "A leading B" and "B leading A" is the direction of the count (up or down). If the counter is going the wrong direction, simply choose the other quadrature mode (or swap the sensor wires).

Inside the Pulse Counter

The heart of the Counter Project is an Atmel AVR ATMega168 microcontroller. It has 28 pins -- which is necessary due to the OLED display, the buttons, motor switch, sensors, and adjustment dial.

The microcontroller includes 16 KB of flash program space, of which about 70% is used. The counter program is written in C.

The inside of the project box includes the motherboard, connectors, and power board for the electronic counter.

The inside of the project box includes the motherboard, connectors, and power board for the electronic counter.

Two things are striking about the inside of the project box:

The reason for the sparse motherboard is that it is recycled from another project. Most of the missing components are for driving a multi-segment LED display and for serial output using a Maxim RS-232 chip and DB9 connector. This also explains why the 18.432 MHz crystal is less than the 20 MHz maximum for this chip, because 18.432 divides better than 20 for many baud rates.

If the motherboard were being designed from scratch for this project, it would be made smaller and it would fit against the encoder and motor terminal blocks and motor switch. That would eliminate many of the connectors and wires. Additionally, another smaller board could be added to the back of the display, to eliminate the connectors and wires going to the dial, display, and pushbuttons. Of course, there would need to be some method of communicating between the two boards (perhaps I2C).

The Power Board

The power board is on one side of the project box and is much simpler than the motherboard. The primary reason the power board exists in this project is that the motherboard (originally intended for another project) does not include a place for a power transistor to control a motor.

The power board contains protective diodes and connectors for power.

The power board contains protective diodes and connectors for power.

The power board contains a female power jack socket for an AC-to-DC adapter with a 2.5 mm center hole (All Electronics DCJ-6). A connector attaches to a power switch on the side of the project box. The power switch is the very first component in the circuit, so that no power is used internally when the power switch is off (open connection).

When power is turned on, it first flows through a 1N5817 diode to prevent damage from some idiot using an AC adapter with a negative center (or any other reversed power source). My book, Intermediate Robot Building, includes more information on reversed-battery protection and other power-source protection ideas.

Assuming the power polarity is correct, electricity then flows to a connector that attaches to the motherboard and another connector that attaches to the motor terminal block. This way the motor can draw power directly from the AC adapter as opposed to the regulated voltage on the motherboard. This prevents motor spikes and electrical dips from messing up the operation of the microcontroller.

This also allows the user to have some slight choice in selecting a different AC adapter (or a bench power supply) to provide the desired voltage and peak current to the motor. Of course, this assumes that the AC adapter will still meet the requirements of 8 V to 12 V for the motherboard voltage regulator.

The motor is controlled by a signal from the microcontroller on the motherboard. A 47 kΩ pull-up resistor keeps the motor turned off by default while the microcontroller is initializing. The International Rectifier IRLU024N HEXFET mosfet power transistor can be controlled by logic-level voltage (0V to 5V) and still provide over 10 amps of peak current. (By the way, this motor control circuit almost matches Figure 10-5 in Intermediate Robot Building, except for a slightly lower pull-up resistor and an optional diode for reverse-motor current protection.)

Let’s learn how the box was made...