Saturday, February 11, 2012

Encoders - How do they work?

 I will explain the term Quadrature a little later. This is a type of Incremental encoder, so I should first talk a little about how incremental encoders work.

Most encoders are based on a wheel of some sort. It might be made of some opaque material with a series of holes around the edge, or perhaps some transparent material with a series of opaque regions separated by transparent regions. Or it might be a printed circuit board with specially shaped traces to provide several different channels of electrical contact.

As the wheel turns, some sort of sensor, optical or electrical, marks the presence or absence of signal, and converts that information to some usable form. What is happening is that a mechanical device is generating digital data, which can be processed to give us the information we want.

                                   http://frontrangerobotics.org/Jan05/EncoderForRMS.jpg

In this picture, we see a code wheel and an optical sensor. It can detect the presence of the opaque sections of the encoder wheel, or the "see-through" areas. Sensors like this are used a lot in printers, to signal the presence/absence of a sheet of paper. I'll be showing you later how I used some to build my own encoder! This is an example of an Incremental encoder.

                                            http://en.wikipedia.org/wiki/Rotary_encoder

In this picture, we see a 13 track Absolute encoder. There are 13 concentric tracks of conductive areas on a non-conducive disk. 13 small metal spring contacts will ride on each of those 13 rings
and generate electrically a binary bit. All 13 of those bits will be used to generate a binary number,
that corresponds to one and only one position on the wheel.  And notice that the tracks don't go all the way around. We don't WANT an absolute encoder disk to turn more than one revolution.


We connect that wheel in some manner to something we want to monitor, like a rotating wheel, or the stem of a valve. Now, in the case of the tank level gauge, we are converting the linear motion of the float, riding on the surface of the liquid, into a measurement of distance.

Our little encoder wheel is revolving, being driven by a gearing system tied to the float by a flexible metal tape. Is it a rotary encoder? Or a linear encoder? Note there is a distinction made between how the encoder works and what sort of measurement is being made.

Also, consider that the encoder used in this tank level gauge is Absolute...it generates a unique digital pattern for each position on the wheel. That should tell you that we have to scale one revolution of this encoder wheel to the entire length that the float can travel, because if the wheel turns more than 360 degrees, we'd be generating these unique codes twice. This is done mechanically, with gearing.   

Investigating Rotary Incremental Quadrature Encoders

My interest in encoders started when I noticed that the volume control knob on my home stereo receiver/amplifier was acting strangely.

After some years of trouble free operation, I noticed that when I turned it on, and discovered that "the kids" had cranked the volume up to an intolerable level before turning it off, my frantic attempts to spin the volume knob in the CCW direction to lower the volume had the effect of increasing it even more!

Some experimentation revealed that by turning the knob very slowly CCW, the volume could be lowered. Turning the knob in the CW direction did not exhibit this behavior--that worked normally.

I mentioned this to my Electrical Engineer friend Gus. He asked me "Is the knob the kind that turns endlessly in either direction?" I told him it was, and he guessed it was probably a "rotary quadrature encoder".

Now, in my years as an Instrumentation & Controls technician, I had some exposure to shaft encoders on large machinery. Most of the ones I had seen simply output a switch contact to signal that a shaft or gear was either turning or not turning. Only a few of the ones I'd seen actually sent a signal range indicating the speed, direction or amount of rotation.

And I'd seen a float type level gauge for above-ground petroleum storage tanks. I'm referring to the trusty Varec 2900 Automatic Tank Gauge, with optional 2900 Float Tape Transmitter.  This device has a metal tape connected to a float inside the tank, going up and over two pulleys, and back down the outside of the tank (inside a metal tube) to a "head unit", where there is a spring to keep the tape under tension, without lifting the float out of the liquid, or preventing it from following the liquid level as it moves up or down.

Inside the head unit is a mechanical system which uses the motion of the metal tape to operate a large display, much like the old mechanical odometers found in automobiles. This provides a local display for reading the tank level visually.

The optional Float Tape Transmitter contained a circular wheel, much like a printed circuit board, with concentric rings of conductive traces linked together. Electrical spring contacts (we called them fingers) would ride on these rings and send either a "continuity" or "non-continuity" (as the encoder wheel was turned by the mechanical gauge) to an electronic circuit board that would convert that information into an analog signal range of 4 to 20 milliamps  of DC current--4 for "tank empty" and 20 for "tank full", and all the levels between those two points.

That signal could then be sent far away to a control room, to drive a remote display indicating the tank level, and perhaps optionally sound an alarm if the tank got "too full" or "too low", or turn pumps on and off, to maintain some desired level. 

OK, back from memory lane. Let me continue with a discussion of some terms I discovered.

This term "quadrature" sparked my curiosity, so I started searching the internets for more information. The first thing I learned is there are different words to describe encoders: Linear, Rotary, Absolute, Incremental, and Quadrature.

Linear and Rotary describe the type of motion the encoder detects. A linear encoder might report on the movement of some machine part that only moves in a straight line--forward and backwards, like the print head of a printer. A print head moves back and forth across the paper, depositing drops of ink, and its position and speed must be controlled precisely. Rotary, as the name suggests, means that the encoder is detecting motion in something that...(you guessed it) rotates.

Absolute and Incremental describe the manner in which the encoder detects and reports on the motion being observed. The example I gave, of a float type tank level gauge, would be an "absolute" encoder.

That means that for each position detected, there is a unique combination of bits (remember the multiple fingers reading "on" and "off"? That's a group of bits that can be read as numbers.)

Incremental, on the other hand, means the encoder can only detect changes--a change in distance (linear) or angle (rotary) from the previous to the current position, or the speed (and perhaps direction) of movement (linear) or rotation. You wouldn't be able to tell the exact position from that information.  You wouldn't know "where you are" unless you knew "where you were" and "how far you went".

So, I learned that my malfunctioning volume knob is an example of a Rotary Incremental Quadrature Encoder.

In my next post, I'll try to explain in more detail how encoders are made, and how they work.





Tuesday, November 8, 2011

Pluse Width Modulation

As I mentioned somewhere near the beginning, controlling the speed of a DC motor is one of my goals.

After some research, I discovered Pulse Width Modulation (PWM) can be used to do this. I had heard of PWM before, as a method of transmitting communication signals. But speed control? This was something new!

DC motors are designed to achieve their  full rated speed at some nominal supply voltage, but will turn more slowly within a certain range of lower voltages. Sure, I could simply put a potentiometer in series with the motor and control the voltage that way, but then I'd need to "be there" to turn the potentiometer knob, or build some Rube Goldberg mechanism to turn it for me.

PWM is a way to vary the average voltage (per unit of time) to the motor.
If you vizualize a regular square wave (referenced to zero volts) you'd notice that the ON time is exactly the same as the OFF time. This is known as a "50% duty cycle".



Thanks Google and masteringelectronicsdesign.com for this graphic.

OK, the ON time is represented by the line at Vp, lasting from Time Zero
to T/2. The OFF time is represented by the line at 0, lasting from T/2 to T

T is the Time Period of this pulse cycle.

PWM works by making the ON time of a square wave variable. A simple definition of duty cycle would be the percentage of ON time in one cycle, compared to the duration of one entire On/Off cycle.



Thanks Google and csound.noisepages.com for this cool graphic


To use PWM to control our DC motor, we use that ON again, OFF again signal to control a MOSFET that in turn controls the supply of power to our motor.

A 50% duty cycle applied to a 12 volt supply gives an effective 6 volt supply.
A 25% duty cycle would give 3 volts, and a 75% duty cycle would give 9 volts.

Again, thanks to my friend Gus for showing me how to build a variable duty cycle PWM circuit. My little PWM circuit uses a potentiometer too, but for now it's baby steps.

A really cool PWM circuit  would be controllable by a digital input from a personal computer or the some other form of electronics, allowing for automatic or programmable control.  

I checked the output of my PWM circuit on an oscilloscope, and I see that it is capable of a variation in duty cycle from around 10% to very nearly 100%.

The next step was to design some way to make this PWM signal turn on and off my H-Bridge MOSFETS, when the motor is rotating in either direction.

The approach I chose (again, following the helpful advice of my friend Gus) was to use the PWM output to control a new MOSFET that would alternately turn the gate signal to my H-Bridge MOSFETS On and Off.


(insert schematic here)

I was very gratified that this setup worked the on the first attempt. 











Thursday, August 11, 2011

Interesting details about H-bridges and MOSFETS

As I said, I salvaged my MOSFETS from old CRT computer monitors.

The T in MOSFET stands for Transistor. FETs, or Field Effect Transistors,
work in a different way than the more familiar BJTs or Bipolar Junction Transistors.

It's more common to use BJTs in applications where you want to use the Linear Region of Operation.  FETs are different  in two important ways:

1)   The Gate terminal has a very high impedance, which means almost no current  flows through it--the voltage you apply to it is basically creating a "field" or an electrical charge, that controls the current flow between the Source terminal and the Drain.

2) FETs are optimized for operation in the Saturation and Cutoff Regions --  completely ON (max current flow) or OFF (zero current flow) between Source and Drain terminals.

MOSFETS DO have a Linear region, but we want to avoid that! We want our MOSFETS to be either all the way ON or all the way OFF.

Why, you ask? First of all, if the MOSFETS don't turn on completely, then
they are limiting the current flow to our motor. Motors need their full design current if they are to put out their full design torque.

Secondly, if the MOSFET isn't completely on, then it is acting like a resistor. It's limiting current flow and therefore wasting some of the power you want to send to the motor in the form of heat.

N-Channel and P-Channel

There are two basic types of semiconductor devices...N-channel and P-channel.

N-channel devices are more common. They are simpler (and therefore cheaper) to manufacture, and they (generally speaking) work better and last longer.

Here would be a good place for a link to some technical discussion of what that's all about. I know I read it somewhere....check back later.

Three major differences between N-channel and P-channel MOSFETS:

  1. N-channel MOSFETS turn On with a positive voltage at the Gate terminal. P-channel MOSFETS turn On with a negative voltage at the Gate terminal.
  2. N-channel devices switch from On to Off faster.
  3. N-channel MOSFETS have a lower resistance when in the ON state.

The MOSFETS I was able to salvage were all of the N-Channel variety. And of that number, most were IRFS640A. Here's a link to a datasheet for them.

There are designs for MOSFET H-bridges that use a mixture of N-channel and P-channel MOSFETS. Said designs have their own particular blends of pros and cons.  One pro is that you don't need a separate, higher voltage to apply to the Gates. But P-channel MOSFETS have some cons both in terms of cost and performance (as noted in above).

You turn an N-channel MOSFET On by applying a positive voltage to the Gate, relative to the Source terminal.

The two MOSFETS located in the lower vertical legs of the H-bridge only require the voltage to be higher than zero (ground) by 4.5 to 7 Volts.

With an N-channel MOSFET H-bridge, the voltage required to turn either of the two top MOSFETS On will be higher than the power supply voltage you supply to the Drain terminals.

Why is that?

To turn the motor On, we need to turn one of those top MOSFETS completely ON.
That would raise the voltage at  its Source terminal, which is connected to the motor, to the value of our supply voltage. But to do that, we need a voltage at the Gate terminal that is 4.5 to 7 volts HIGHER than our supply!


Since my salvaged MOSFETS are all N-channel, that's what my H-bridge uses.  Therefore, I needed some way to provide that higher-than-supply voltage to the gates of those two upper MOSFETS.

After talking with my friend Gus about this, I decided to go with his suggestion to build a Cascade Voltage Multiplier circuit, to convert the power supply voltage (nominal 12 volts) to approximately 21 volts DC.
 
And notice that the gate of one upper MOSFET is wired in parallel with the gate of one lower MOSFET in the opposite leg. The datasheet for any MOSFET should tell you what the upper limit is for just about anything--here we are concerned with the upper limit for "source to gate voltage".

In the case of my N-channnel MOSFET H-bridge, the voltage would need to be high enough to turn that upper MOSFET completely on and yet not high enough to damage its associated lower MOSFET.

In the case of the MOSFETS I used (IRFS640A) the absolute max for Source to Gate voltage is +/-30 volts. So the 21 volts my little voltage multiplier produces is plenty to do the job, and yet well within that limit. 

Some links to useful information

I thought I'd post some links here. Eventually I'll get them strewn throughout my other posts here, to make them relate better to the stuff I'm talking about.

Link 1
 A better MOSFET H Bridge Schematic - The Using MOSFETS Website
Link 2
 H-Bridge Fundamentals « Roko.ca











Monday, August 8, 2011

More about H-bridges


First, a definition of the term MOSFET:

It's an acronym for

Monolithic Oxide Semiconductor Field Effect Transistor

Now you understand why we call them MOSFETs...it's a whole lot easier to say!

For all practical purposes, you can think of a MOSFET as a solid-state On/Off
switch. We control this "switch" by applying voltages to the Gate terminal.

Current flow through the MOSFET will be between the Drain and the Source terminals.


Why is it called an H-bridge?

This arrangement of wires, MOSFETs and a motor can be thought of as looking like an H



The motor you want to control is located in the horizontal leg of the H.

There are four MOSFETS, two in each vertical leg of the H, one above the horizontal leg and one below.

The two upper ends of the vertical legs are connected to the Positive side of your DC supply. The two lower ends are connected to the Negative side.


How Does It Work?

With this arrangement, you simply turn On on upper MOSFET in one vertical leg of the H, and the lower MOSFET in the opposite leg of the H. In doing so, you send current from the DC voltage supply through the motor in one of two possible directions. And as a result, the motor turns in one of two possible directions.

To make the motor turn in the other direction, you turn Off those two MOSFETs and turn On the other two.

You must take great care to turn the correct MOSFETS On and Off in the correct order! If both MOSFETS in one leg are ON at the same time, it results in a DIRECT SHORT TO GROUND, which is a Bad Thing.

Here is an interconnect diagram of the H-bridge circuit I built.



I wish I could claim the credit for the pull-down resistors (thanks FunGus).

The pull-down resistors ensure that in the absence of any voltage applied to the gates of the MOSFETS, they will be OFF. I think of this as a safety measure, a "default condition" --everything is turned Off unless deliberately turned ON.

This also makes it easy to use an SPDT switch to control the direction of the motor. In the drawing you see a box that says "Polarity Reversal" .

Notice that one of the two wires leading to that box ties together the gates of two MOSFETS -- the "high" MOSFET in one vertical leg of the H and the "low" MOSFET in the opposite vertical leg.

And of course the other of the two wires ties together the gates of the other two MOSFETS in each vertical leg.

In this way we ensure that the correct MOSFETs will be turned On and Off in the correct order.

I'm saving the arcane details for later for the sake of simplicity. Things like "it's a bad idea to reverse the motor too fast and/or often", "DC braking" and "free spindown". Baby steps first.

Some images of my work

Very Important Legalese Message:

JUST BECAUSE I take my life in my hands, tinkering with electricity and other  POTENTIALLY HAZARDOUS stuff, does not mean YOU should try to do likewise.

IF YOU CHOOSE TO DO SO, it's YOUR DECISION, and YOU ALONE will REAP THE REWARDS --- OR SUFFER THE CONSEQUENCES. Or some mixture of the two!

I wish you Luck, but don't depend on Luck! Do your homework and CYA at all times!




As you read on you will learn that I salvage a lot of my parts from discarded electronic devices.

What use is a circuit like this? DC motors can be used in robotics projects.

Being able to change the direction a motor turns is pretty important.

Next comes being able to control the speed of the motor.

Next would be the ability to precisely position some mechanical device (like a robot arm).

That involves stepper motors, or some sort of position feedback device. Check back for future news on these subjects.

Also I need to put in a word of thanks to my friend Gus, who is an Electrical Engineer. He helped me get past quite a few of the stumbling blocks I have encountered.

OK, here are some photos of the prototype I've been working on:

This photo shows two groups of four MOSFETS (two separate H-bridges). I salvaged them, the heat sinks and most of the insulated wire from some old CRT monitors.

The supply voltage for this project is a nominal 12 Volts DC. I use a salvaged PC power supply to power up this gadjet.



The motor assembly was salvaged from a printer. I couldn't find any specs on this motor. It may have been designed to run on 24 volts, since that's what the printer's power supply was designed for, but it seems to do fairly well on 12 volts.



The small circuit board visible in the lower left corner is a voltage multiplier (more about this later).
There is a toggle switch mounted to the right of the motor and associated plastic gears, for reversing the motor direction.



A closeup of the voltage multiplier circuit.
This takes the nominal 12 Volts DC and converts it to approximately 21 Volts DC, to drive the gates of the MOSFETS. Why? More on that later.