Tuesday, October 14, 2014

The Wonders of Analog to Digital Conversion

In the digital world, information is shared through software using multiple digital patterns of ones and zeros. These digital patterns can represent alphanumeric characters, mathematical operations, memory commands, or any other representative data that when decoded by software, corresponds to understandable information based on an agreed upon model of the digital patterns and data manipulation methods. This is all well and good for applications of math calculation, word processing, database entry, and other symbol based system uses, however, one can ask: how does one get real world physical data into a format of digital patterns that can be used by computers to measure, display, calculate upon, and manipulate the data?

Welcome to the big and wonderful world of analog to digital conversion. Here, real world physical information such as temperature, speed, color, pressure, distance, power, location, softness, and a sundry more properties, are quantified and converted into digital patterns; digital patterns that can be measured, displayed, calculated upon, and manipulated by software code. Achieving analog conversion can be accomplished by using very simple electronic circuits and/or dedicated integrated circuits known as analog to digital converters, or ADC chips.

These ADC chips are typically designed to measure a quantity represented by a voltage level range. This voltage level range is used for the conversion of real world measurements, which are normally variable due to the fluctuations present in this analog and ever changing outside world. To measure or sense these real world fluctuations, sensor circuitry is typically designed such that it constantly outputs a varying voltage level in the range of the ADC chip. This is done so that when the ADC chip is triggered, the ADC takes a snapshot of the sensing circuit’s voltage level much like a camera. Here, the ADC reads, or samples the voltage level and then converts this sample into a digital pattern representative of the voltage level measured at the time of the trigger.

So how is this accomplished and what is going on during this voltage level to digital pattern conversion snapshot in an ADC chip? To explain this, we must venture into the world of ADC sample and hold. At its very essence, an ADC sample and hold circuit contains an input amplifier, an energy storage device, which is typically a capacitor, an output buffer, and a switching circuit, which is used to control the sampling, or charging of the capacitor, and the holding of this charge for conversion into a digital pattern.  Below is a typical sample and hold circuit so that you can review how these items are connected. Here, A1 is the input amplifier where the real world voltage from the sensing circuit is connected. This input amplifier presents high input impedance to the real world voltage and it outputs a high current level that is directly proportional to the varying real world voltage, which is used to charge or discharge the capacitor during the sample or track mode. C is the switching circuit that controls the sample, or tracking mode, and the converting or holding mode. When the switch is closed the circuit is in tracking mode, which allows the charging or discharging of the capacitor in conjunction with the fluctuations from the output current of the input amplifier. When the switch is opened, the capacitor is disconnected from the input amplifier and then holds the last voltage level that it was charged at, so that the AO, or the output buffer can be activated to convert this voltage level to a digital pattern. The output buffer offers high input impedance to the capacitor to keep the voltage held inside the capacitor from discharging prematurely before it is converted to the digital pattern.

Sample and Hold Diagram 

To delve further into ADC Sample and Hold circuits and understanding, here is a good reference from Analog Devices titled: ADC Sample and Hold Amplifiers. It is available at the link below:

Luckily, much of this sample and hold magic is done for us in the many ADC chip offerings on the market like Microchip’s MCP3208, which is a 12-bit ADC chip, which brings us into another matter called: bits. Bits are the single basic unit in digital information represented by a one or a zero in binary, or in electronic terms, high or low voltage levels. For example, a one bit device can measure something that is on or off, as one bit only has two states, high or low, on or off, true or false, one or zero. A two bit device can measure four states, so in binary terms the two bit device has the following possibilities: 00, 01, 11, and 10. As you can see, adding to the collection of available bits adds to the collection of states usable for readings by the power of two. Where one bit has two states, eight bits has 256 states or 2x2x2x2x2x2x2x2, or 2 to the power of 8 (28).

Why is this important you may ask? Let’s consider our measurement of a real world fluctuating voltage using the ADC sample and hold circuit. If the output buffer is one-bit long, the ADC would only be able to tell us if the capacitor was charged or discharged, so in the real world, our fluctuation measurement would be very limited to on or off, so this measurement is not very informative due to the lack of detail. Now if the output buffer was say four-bit, or 24, which has 16 different states or levels, the real world voltage fluctuation when converted would have 16 different level possibilities, meaning that we will gather more detailed information about the real world voltage fluctuations. The same holds true for the MCP3208 ADC chip with its 12-bit conversion ability, or resolution, as it provides 4096 different states to show us the detail of the real world voltage fluctuations; meaning that each state represents 1 out of 4096 possibilities, or 0.0244% of the full scale resolution of 0 to 4096. AndroiDAQ uses a MCP3208 for its ADC conversion and you can review the MCP3208 data sheet at:

So now that we have a basic understanding of how an ADC chip measures real world data and how the ADC’s bit resolution affects the quantity of digital information that the ADC can give to us in the converted digital pattern, the next question is: what is an ADC chip good for? To explain this I will be using the AndroiDAQ module and I will introduce to you two new functions that we just implemented in the AndroiDAQ DEMO for Android application, called ADC Sample and ADC Waveform.

The ADC Sample function allows you to acquire a desired number of samples taken at a set rate of time from AndroiDAQ’s ADC channel 0; meaning you can take a snapshot of 1 to 4096 analog samples, at selected increments of time, and that these samples are converted by the ADC into digital format patterns. For example, and as shown in the image below, if ADC Sample is setup to take 100 samples and the Sample Rate is set at the 100-microseconds rate, this means that each sample of the hundred samples is taken every 100-microseconds, after the “Read Samples” is pressed. So the AndroiDAQ’s ADC acquires the 100 samples in 1/100th of a second in total time (100 X 100-6). These 100 now digital samples are then saved onto the built-in micro-SD card of the AndroiDAQ module. Now if the Sample Rate was changed to 10-milliseconds, from the 100-microseconds used above, these 100 samples would be acquired in one second total time (100 X 10-6).

ADC Sample Screen

So how is this useful? As we discussed above, an ADC chip is used to measure real world analog fluctuations and convert them into digital patterns, so that we can read, manipulate, calculate, and understand them. Measuring these fluctuations at known time intervals allows us to see traits and trends, which allows us to further control, electronically or mechanically, processes or react to wanted or unwanted conditions. To illustrate this point, I introduce to you the ADC Waveform function in the AndroiDAQ DEMO for Android application. The ADC Waveform function is much like a digital oscilloscope, where the digital samples are all converted and plotted onto a visual image to allow you to see the analog real world fluctuations at a faster time scale than is humanly possible. In the image below, the ADC Waveform function is showing 448 samples of analog information which was taken 10-milliseconds apart. This means that the total 448 samples took 4.48 seconds to acquire, which was enough time to see the affects of me passing my fingers slowly across the face of a photo-resistor, in circuitry that will be explained further below.   

Example Waveform Output

As you can see, using an ADC chip in electronic data collection and control is very useful because with an ADC chip, it is quite easy to convert and record real world analog data into digital format. As an example, on each dip down in the waveform image shown above, a software program could have counted these dips. So for this simple example, one could have used the simple photo-resistor sensor circuit for counting people who were passing by the sensor. There are many more ways to use this converted analog data, so let’s explore some sensor circuits to help us read and understand the analog and ever changing world around us.

For this, I will use the AndroiDAQ module’s ADC to measure readings from our sensor circuits, on Analog Channel 0 of the AndroiDAQ ADC, and I will use the new ADC Waveform feature in the AndroiDAQ DEMO for Android application, which is installed on my Android phone. You can get your copy of the AndroiDAQ DEMO for Android application on Google Play or at our DEMO enhancement website; links are given below.

You will also need to update your AndroiDAQ module’s firmware to the newest version which is version number 4.06, which is available at the links given below:

The first point of business for us to start exploring ADC sensing circuits is to review what the voltage range of the AndroiDAQ’s ADC is. By looking at the specifications in the AndroiDAQ User’s Manual, we find that the maximum input voltage for the ADC chip is 3.3-volts, so it has a voltage range of 0 to 3.3-volts. This range is due to AndroiDAQ being a 3.3-volt module and also because the AndroiDAQ MCP3208 chip is using a 3.3-volt reference voltage.  You can find the AndroiDAQ User’s Manual and the MCP3208 Data Sheet at the links given below, so that you can review these and other specifications in detail:

Now that we know that the voltage input range of AndroiDAQ’s MCP3208 ADC chip is 0 to 3.3-volts, we can design sensor circuits to output a voltage range of zero to 3.3-volts, so that we will be able to see the range of changes presented by the sensor circuit, by measuring, or sampling, these changes at set time intervals of time, or at a set sample rate.

For our first sensor circuit we will use a simple variable resistor to see the changes of the voltage level as the knob of the potentiometer is moved. Here, a 10K potentiometer is used and connected as shown in the image below; where one side of the potentiometer is connected to 3.3-volts, our upper ADC input limit, the other side of the potentiometer is connected to ground, our lower limit, and the wiper of the potentiometer is connected to ADC Channel-0, or A0, which will then have a voltage range of change from zero to almost 3.3-volts as we turn the potentiometer knob clockwise and/or counterclockwise.

The Potentiometer as a Sensor

Simply measuring the raw data of the changes of voltage, from the movement of the wiper, at a set time interval or sample rate, allows us to measure several things, without the worry of the units that the measurement was taken, such as: the linearity of the potentiometer, how fast the wiper was moved, the position of the wiper at a certain period in time, and more. These measurements are quite useful in real world applications. For example, being able to measure the linearity of the potentiometer allows manufacturers to quantify the quality of their variable resistor product line; knowing how fast the wiper was turned during the period of time allows for speed to be monitored or calculated; and knowing the position of the potentiometer wiper at a set point in time allows servo motors to sense their shaft’s position.

So let’s now fire up the AndroiDAQ DEMO application on our Android device and navigate to the ADC Sample screen. Set the Sample rate to 10-mSecs (10-milliseconds), which means we are breaking up one second into 100 pieces and that at each 1/100 of a second, we will take a snapshot or acquire one sample from A0. You can leave the number of samples setting on the ADC Sample screen to its default setting of 100 for this example. Now navigate to the ADC Waveform screen by swiping right to left. Ensure that your Android phone is connected to your AndroiDAQ module, via Bluetooth or WiFi, and then long press the waveform graph to acquire or take 448 samples at a sample rate of 10-milliseconds. This means that when you long press the screen, the application will take a little over 4-seconds to take all the 448 samples, plenty of time to move the potentiometer wiper back and forth for this measurement. After the samples are taken by AndroiDAQ, they are sent via Bluetooth or WiFi to the DEMO application and plotted to the waveform graph. Below is an image showing my moving my potentiometer clockwise during the 4.48 second sample time:

Potentiometer Waveform Example

As you can see by the graph above, our potentiometer’s output is zero volts to a little under 3.3-volts and that when turning the small knob of my potentiometer, there are hesitations as I re-positioned my fingers on the knob over the 4 seconds of sampling time. Below is another image of the same process, only this time I had a knob extension so that I could turn the knob more evenly and much quicker. Here you can also note the speed of the knob turning, as the 10 horizontal divisions of the graph represents 1/10 of the total sampling time of 4.48-seconds, or .448-seconds, so it took me 0.896-seconds to turn the knob from its minimum to its maximum.

Potentiometer Waveform Example 2

In electronics, the method of dividing a voltage between two resistors (Figure 1) is much like how the wiper of the potentiometer divides the resistance of the potentiometer as a whole into two resistive segments. This is called a voltage divider circuit. A voltage divider is a passive circuit that produces an output that is a fraction of the input voltage. The formula to calculate the output of a voltage divider is shown in Figure 2, where R1 and R2 are the values of each resistor, in ohms. This little circuit is useful in cases where your sensor circuit’s output voltage is above the voltage range of the ADC input, as the output of the voltage divider will follow the Vin voltage at the fractional value on Vout.

Figure 1

Figure 2

Now that we understand what we can measure with an ADC chip using a potentiometer and how to control our voltage range with a voltage divider circuit, let’s move on to another sensor circuit. Here I will introduce to you the photo-resistor, or photocell, which is more or less a light controlled variable resistor, much like a potentiometer only with no wiper, as the resistance as a whole of the photo-resistor decreases, as light intensity shining on the face of the photocell increases; in other words, a photocell exhibits photo-conductivity. A photocell is typically made of a high resistance semiconductor material such as cadmium sulfide (CdS) or other semiconductor materials. A photocell can be used for light sensitive detector circuits that are useful for say counting things passing by the photocell, which will affect the light level on the cell, detecting a dark line on a white background, for perhaps robot travel control, or sensing when an object is present on a conveyor belt and then stopping the belt, so that the object can be further worked on, in for example an assembly line.

Below is the circuit that we will use to test a photocell with our ADC measurements. The photocell that we are using is a PerkinElmer Optoelectronics VT935G-B whose resistance range is from approximately 40K minimum to 1-megaohm maximum, going from bright to dark light levels. Of course you can use any photocell you have handy, but you will need to know its minimum and maximum resistance values to follow along. To calculate our photocell sensor circuit’s voltage range, we plug in the resistance values of the voltage divider using first the 40K minimum resistance value for the photocell and then do another calculation with the 1-megaohm maximum value of the photocell. This calculates a voltage range from 3.26-volts maximum to 2.64-volts minimum, so we these voltages fall well inside the ADC chip’s voltage range for our measurement purposes.

Photocell Circuit

Below is the resultant graph for my photocell sensor circuit. Here, I simply covered up and uncovered the photocell’s face to get the five peaks that you see.

Photocell Results Image

There are many more sensor circuits that you can build and use to measure with the AndroiDAQ ADC. All that is necessary for these measurements is to remember to keep the output of your sensor circuit within the voltage range of 0 to 3.3-volt, which is ADC input range of the AndroiDAQ module. There are sundry offerings of sensors for you to use to measure the analog world; for example there are sensors to measure distance, humidity, temperature, air flow, pressure, gas presence, proximity, rotation, direction, elevation, pitch and yaw, and many more physical properties. A simple Google search for your desired property, or measurement, will aid you in finding a device that fits your needs. Many manufacturers of these devices will give example circuits in their data sheets to help you in interfacing their sensor to ADC chips. The analog changes of this ever changing world are exciting and analog to digital conversion can help you to visualize and quantify these changes in ways only left to your imagination in the digital world.  

Post a Comment