[Guide] Analog to Digital Converters

BraveSirRobbin

Moderator
[Guide] Analog to Digital Converters

by BraveSirRobbin

(Edited by Guy Lavoie)

Home automation systems that are a step beyond your basic "X-10" light control usually consist of a mechanism for controlling and monitoring digital input and output modules. These systems may include an Ocelot with SECU16(I) and/or RLY8-XA expansion units, Caddx Security Systems, Elk M1 Gold Systems, JDS Stargate and others. Their basic functionality of simple, digital inputs and outputs serves most automation and security purposes.

What happens when you want to expand your automation system to include custom measurements such as temperature, water softener salt level indicator, distance measurement, weight measurements, etc...? You could use a "canned" system to measure temperature for instance but what if you want something more versatile? Well one answer would be to have a monitoring system that incorporated a way to measure "analog" inputs via an analog to digital converter. This way you could now directly measure these items with your automation system.

This Guide will describe basic analog to digital converter principles. Its intent is to introduce this topic and also describe how one would go about the necessary calculations needed for general home automation purposes. I would like to thank Guy Lavoie for kindly reviewing this Guide as his efforts made this a greatly improved article.

Description:

Analog devices usually consist of a sensor that will output a voltage that has a linear relationship with the unit that it is measuring. For instance an analog type temperature probe will output a voltage that will correlate to temperature. As the temperature increases so will its output voltage. The "measurable" range of this temperature will be such that it has a "linear" relationship with this voltage output.

A linear relationship means that a given change in the quantity being measured (such as a number of degrees of temperature) will produce a given corresponding quantity of change in its output (such as a voltage), over the device's entire measuring range.

When a sensor has a linear output such as this, it can have a function or equation which represents a way to convert this voltage to temperature units.

Now one would need a way of getting this voltage from a sensor into their automation system so this can be of use for triggering events and displaying the information on the home's computers or other information providing devices, such as vocal announcements.

This Guide will show and explain how to accomplish this task! It will also show how to get this voltage information from an analog to digital converter to "real world" units (such as temperature, distance, weight, etc...).

Recently I obtained THIS "ten-bit" analog to digital converter from PH Anderson. Though specific examples will be shown using this board, these methods will be generally explained so they may be easily adapted for other applications. The concepts presented here could be used for other analog to digital converters such as the SECU16 (used with the Ocelot) and the Elk M1 Gold systems.

Background:

An analog to digital converter will convert an input voltage to a reading that a computer can understand. This converted reading is based on a binary counting system. We all know a computer system understands bits, which are a value of zero or one. The binary counting system consists of a number of these bits, whose various values represent a number. The greater number of bits, the higher the possible maximum number which can be counted.

For instance say we have a simple two bit system. The maximum count will be two to the second power or four (ranging from zero to three). The first bit will represent a two to the zero power or one, the second bit will represent a two to the first power or two. The way this system will count is shown below:

2-Bit Binary No.____Bits it represent____How it counts____ Decimal Equivalent
0 0__________________0_______________0 + 0_____________0
0 1__________________1_______________0 + 1_____________1
1 0_________________1*2______________2 + 0_____________2
1 1_______________1*2 + 1_____________2 + 1____________ 3


Now, lets say we had a two bit analog to digital converter which can accept a maximum analog value (input voltage) of five volts. The converter will read a voltage value and assign it a binary number value between zero and three. It evenly divides the five volt span into these four chunks or sections. The important thing to remember here is that an analog value can vary as much as 1.25 volts (five divided by four) before it will increase a binary number value.

The reason this is important is for understanding the resolution of an analog to digital system. For instance lets say that we have another analog to digital converter but this one has ten bit resolution instead of the two bit one above. Our resolution now has 2 to the tenth power or 1024 steps (from 0 to 1023). Now divide that five volts maximum input by 1024 (total numberof steps) and you can see the increased resolution this system brings. (Another note to mention is an eight bit system is 2 to the eight power, or 256 steps).

This is important in cases where you want high resolution, or the ability to discern between small changes in readings (or how fine a resolution you want for a temperature sensor).

Another way to think of this is imagine you want to measure distance with a tape measure. The tape measure is divided into 1/32 of an inch so you can absolutely verify a reading to that accuracy. Now say you use a tape measure whose markings are only every inch (i.e. just a blank tape background between the inch numbers, no markings). Now you can only measure something to within one inch of accuracy because of the decreased resolution of the tape measure.

How Analog Sensors Work:

As was mentioned earlier analog sensors will usually output a voltage that corresponds to their value that they are measuring or sensing. For instance a temperature sensor will output so many volts per degree Fahrenheit. Another example would be a current sensor say from CR Magnetics that outputs a voltage which will correspond to a current value being sensed.

Lets use a specific example. Say you want to measure the main feed current that each phase is using for your home using an "analog" current sensor. You would need to purchase two current sensors (one for each phase). If your home had 200 amp service you would need two sensors such as THIS one from CR Magnetics (actual part number would be CR4310-200). This current sensor will output a DC voltage that corresponds to the AC current it is measuring. If you look at the data sheet for this product you will see that it will output zero volts for zero measured amps and five volts for 200 measured amps. So if the voltage output from this sensor is 2.5 volts, that would correspond to 100 amps being measured by the current sensor.

Now we need an analog to digital converter to get the voltage output from this current sensor to our computer system. This example will use THIS ten bit analog to digital converter (which has a five volt maximum input on its channels).

Note that we selected the current sensor that had a five volt maximum reading instead of the optional ten volt since the maximum input our analog to digital converter can accept is five volts.

Now we would connect the output of that current donut to an input channel on our analog to digital converter. Note that this analog to digital converter has a serial interface to a computer so we can read its bit converted value.

So now we have a complete (hardware) system. The current sensor is measuring/sensing the amps being drawn and converting this value to a voltage output. This voltage output is connected to our ten bit analog to digital converter (which has a maximum input of five volts) so we will then have a converted voltage number represented by a binary number somewhere between zero and 1023. This number can then be read by the computer via the serial interface.

Conversion of Volts and Bits to Real World Numbers:

Now we have a computer that can read the bit value of our analog to digital converter, but what good are these bit numbers to us? We would rather have amps being drawn displayed by the computer. Well, we now have to convert these bits to real world numbers, in this case amps.

So how do we do this? Well, we basically have to remember a couple of things about the way our analog to digital converter works and the relationship our current sensor has between its measured amps value to its output voltage (connected to our analog to digital converter).

First, what type of analog to digital converter did we say we were going to use again? A ten bit one with a maximum readable voltage input of five volts. So as we learned in the beginning of this guide, that means that this analog to digital converter will take a measured input voltage (up to five volts) and divide that value into 1024 segments. So to convert a displayed bit value from our analog to digital board to its voltage that it is measuring we would use the following equation:

voltage = bits/1024 * 5

The 1024 number comes from the fact that we are using a ten bit board. The 5 comes from the fact that the maximum measured input voltage this board can measure is five volts.

So lets say our computer is reading a bit value of 383. This means that the voltage it is measuring is:

383/1024 * 5 which equals 1.87 volts.

Well, this is only half of the equation. Remember we want to display total amps being drawn. So now we have to convert this analog to digital input voltage (coming from the sensor) to amps. How do we do this?

Well, remember from the data sheet that this particular CR Magnetics current sensor will output a voltage of zero volts for zero measured amps and five volts for 200 measured amps. This means that each volt measured by our analog to digital converter will correspond to 40 amps being sensed by our current sensor.

This can be represented by the equation below:

amps = 40 * volts

Well since we know the volts value is 1.87 from the converted analog to digital board we can now calculate amps as follows:

amps = 40 * 1.87 or amps = 74.8

So if we combine this equation with the equation above we would get:

amps = 40 * (Bits/1024 * 5)

This is the equation we would use (in our software) to convert the bits the computer is seeing from the analog to digital converter to the real world amps number.

Review:

Whew, well we finally have a real world number displayed on our computer. Here is a quick review of how we got it:
  • Current Sensor outputs five volts for 200 measured amps (max value)
  • Equation determined which converts this voltage to amps
  • Current sensor output voltage connected to an analog to digital converter
  • Analog to digital converter is 10-bit with a maximum of five volts input
  • Bit value from analog to digital converter to voltage equation determined
  • Final real world amperage number is calculated!
Slope Intercept Formula:

Lets look at a graph of our current sensor we used in the above example. Note that the X scale is the output voltage and the Y scale is the measured current.

currentplot.jpg


Note the straight line which indicates a linear relationship. Also note that the graph started at point 0,0 making the calculation (of converting output voltage to measured amps) a little easier to determine.

But, what happens if this graph did not start at point 0,0? What would happen instead if the graph of this sensor looked like the following:

bcurrentplot.jpg


Note that now the graph starts at point 0,10 instead of 0,0. So now how would we determine a calculation to calculate amps measured from a known output voltage value? Well we would have to use the Slope Intercept Formula since this graph now has an "offset" on the Y-Axis.

Since we only need to determine an equation and not become an expert with this formula here are some quick basics to help get our equation.

The slope intercept formula is generally represented by the following equation:

Y = m * X + b

Y in this case would be the measured amps, X would be the output voltage, b is the Y-intercept and is the point where the graph crosses the Y axis (in this case it is 10). m is the slope of the line which is basically its rise over run. In this case the line is increasing one unit in the X direction (run) for every 40 units in the Y direction (rise). Thus the slope or m equals 40/1 or 40.

So the equation of this line using the slope intercept formula would be

Y = 40 * X + 10

Or substituting our current and voltages for the Y and X:

amps = 40 * volts + 10.

Well, that was pretty easy to do because we had a nice graph in front of us, but what happens when you are just given a calibration sheet of a sensor? In other words, what would happen if we were just handed the following values for a calibration sheet for our current sensor shown in that graph?

Calibration Sheet for a 200 Amp Current Sensor (Example Only)

Output Voltage_________Measured Amps
1_________________________50
2_________________________90
3________________________130
4________________________170
5________________________210

Many times you are just given a few calibration points for a sensor on a data sheet and not necessarily an entire graph of the full range of the sensor.

Since we do know (from the calibration/specification sheet) that our data is linear out to 200 amps we can be confident that the numbers above would represent a straight line if we were to graph them. But, lets just calculate an equation without the need for a graph.

Well, the easiest way to do this would be to pick two sets of data points on the calibration sheet. For this purpose lets pick the high and low data points of 1, 50 and 5, 210. For the point of discussion, lets say that 1 would be represented by X1 and 50 by Y1 (since its the first data point we are going to use). Now lets represent the second data point by letting 5 be represented by X2 and 210 represented by Y2.

Now we need to calculate the m and b values from our slope intercept formula equation. We can do this as follows.

m = (Y2 - Y1) / (X2 - X1)

or in the case of our data sets above:

m = (210 - 50) / (5 - 1)

m = 160 / 4 = 40

So now that we have the slope value of m we need to calculate the y-intercept value b. Well remember that the general equation is:

Y = m * X + b

Well, we can just pick a point of X and Y and substitute m for the value we just calculated. Lets use the first data point (1, 50) and substitute these values in that equation:

50 = 40 * 1 + b

So now we can solve for b

50 = 40 + b or b = 10

So now that we know the values for m and b our equation can be written as follows:

Y = 40 * X + 10

Substituting our volts out and amps measured from our sensor for X and Y would yield the equation:

amps = 40 * volts + 10

Does this look familiar? Yes, it is the same equation that we came up with when using the graph above (which proves this method of calculating this equation).

Again, the reason this elaborate a method was shown was for cases where a sensors graph has a Y intercept other then zero; and, also so you can calculate the formula without the need to look at a graph.

Just remember that a sensor needs to be linear, i.e. have a linear relationship with whatever it is measuring to its voltage output. Also the sensors maximum linear measured number must also be known (so you know not to depend on any measurements over this number).

Conclusion:

Granted you will not need this information in this Guide very often in your home automating tasks, but when and if you do, at least you can reference it in order to determine your needed equation for your sensor using an analog to digital converter of your choosing. Note that there are many ways of performing the tasks mentioned above.

Also, hopefully this gives a basic understanding of the binary methodology used for an analog to digital converter and also some basics on determining accuracy and resolution one can obtain from them.

Another reason this Guide was created so it could be used as a detailed reference on how to determine the needed "real world" calculations for another future How-To which will explain how to monitor your garage door (to within two inch accuracy using THIS PH Anderson 10-bit analog to digital converter). This How-To will be forthcoming shortly and will go into the necessary hardware and software details for accomplishing this task! ;)

As always, please feel free to contribute comments, opinions and suggestions below!
 
BSR,

Great write up, I know you put a lot of research and effort into it.

To ME, this is something I'm glad to see, while I don't have an application for it TODAY, this WILL be one of the HOW TO's that I will reference in the future.
(Just as I have referred back to many of the other HOW TO's as I've implemented them into projects.)

A very detailed article like this has me thinking for the future, thinking about ways to use it, where as without it, I would probably not even think about projects using AD (thinking it might be too complicated for me)

SO, no replys? Wow, that means you did an EXCELLENT job, and covered the topic without leaving important questions unanswered!

Please, keep it up! I love the CocoonTech How Tos, and referr to them often as they usually answer my questions without having to start a new thread.
 
I just stumbled upon DataQ's new line of Analog to Digital Converters. They have some very attractive options and pricing. For instance four 10-bit zones with wide input range (-10 to +10 volts)) for $25. They even offer sample programs for interfacing to various programming languages and even have a support forum.
 
One other note, when formulating an equation of a line, one could just use Excel to get the equation, especially if the curve exhibited some non-linearity (they have varous curve fit forumlas/methods built in).
 
Back
Top