Prepare signals for microcontroller ADCs
Sean Newton and Alec Bath, STMicroelectronics- June 1, 2011
|Read more from the June 2011 issue.|
For most applications, you must condition the voltages to bring them within the ADC’s input-voltage range. Signal conditioners range from simple resistor-divider networks to amplifiers and filters. You’ll also have to plan for signal frequency and sampling rates when using an ADC. For AC signals above the audio range, you may need a microcontroller that contains an SAR (successive-approximation register) ADC, which can sample at rates of 1 Msample/s (see “Inside an SAR-type ADC”).
Preparing the signals
Before connecting an analog signal to a microcontroller, you must take into account the input-voltage range of the ADC. Consider an STM32 microcontroller as an example. The microcontroller has power pins specifically for the VDDA and VSSA of its ADC, where VDDA is the positive supply. The ADC’s input-voltage range is between 0 V and VREF+, where VREF+ can be the same as or lower than VDDA, but not lower than 2.4 V. If VREF+ is equal to VDDA, then VREF+ can go up to the maximum value of VDDA, which is 3.6 V.
The microvolts/bits ratio is important because you can get finer resolution as you decrease the value of VREF+. Once the ADC input voltage is in digital form, you can use a software look-up table or an equation to convert the raw bit count to volts. Then, you can use another conversion routine to convert volts into your desired unit of measure, such as degrees Celsius.
Assume that you have a temperature sensor with a range 0 V to 2.4 V where the output is linearly proportional from –20°C to 200°C. In that case, an ADC count of 0 represents –20°C and an ADC count of 4095 represents 200°C if VREF+ equals 2.4 V. Once you know the volts/bits ratio, you can design signal-conditioning circuits that convert the analog-input signal to the appropriate input-voltage range for the ADC.
Two examples help illustrate how you can design a signal-conditioning circuit. For the first example, suppose you need to monitor a DC bus voltage using a microcontroller’s ADC input. During normal operation, the bus voltage is fixed at 24 VDC. For this example, the ADC will accept a maximum input voltage of 3.3 V. The signal-conditioning circuit need be only a simple voltage divider (Figure 1).
To protect against an overvoltage condition, the design requires you to set the maximum peak bus voltage threshold to 10 times the desired bus voltage, or 240 V. A resistor divider with a 520k resistor and a 7.2k resistor will give a maximum analog input voltage of 3.3 V when the DC bus voltage is 244 V. At 24 V, the analog input voltage will be 328 mV, which is 407 counts in this application. A conversion algorithm or look-up table can convert the digital count into volts for display or other action.
For the second example, imagine you have an AC-measurement application that requires you to sense the rotor position in a three-phase PMSM (permanent-magnet synchronous motor). The motor uses a permanent magnet in the rotor, and it uses digital commutation instead of brushes and copper-commutator segments.
You can find the rotor position using sense resistors in one or more of the three motor windings, from which you can calculate the commutation angle and motor speed. Figure 2 shows that you can add the sense resistors between the low-side switch and ground.
When the motor’s phase is inactive, you can measure the back-EMF (electromotive force) with an ADC. You need three of these circuits to make the three-phase measurement.
To make the PMSM measurement, you need a simultaneous-sampling ADC because you need to measure all three motor-winding voltages at once. The sense resistor typically has a value of less than 1 Ω. Thus, windings develop only small voltages, and you’ll likely need an op amp to boost the voltage to around 3.3 V (or perhaps slightly less).
Setting the sample rate and input resistance
Once you’ve conditioned the ADC’s input voltage to the proper levels, you must set the ADC’s sample rate. The ADC on an STM32 microcontroller, for example, can sample at rates up to 1 Msample/s. You must take into account the measured signal’s analog characteristics to find the best sample rate. Slow-changing signals such as temperature and pressure don’t need a high sample rate; sample rates of 1 s to several minutes may be sufficient. Fast-changing signals, however, need higher sample rates.
At 1 Msample/s, a signal’s highest frequency component must be less than 500 kHz to meet the Nyquist criteria. Otherwise, aliasing will produce unwanted artifacts. For an accurate signal representation, you should use a sample rate that’s 10 times the measured signal’s highest frequency, which could limit your signal to 100 kHz. Audio applications fit nicely within this limitation. Ultrasonic applications, though, could soon overrun the ADC’s sampling ability, resulting in poor digital-signal definition.
The ADC’s input impedance relative to that of the signal source is also important because it can limit the ADC’s maximum sample rate. Capacitance associated with the internal ADC circuitry of the microcontroller, combined with the resistance to the incoming signal source, forms a low-pass filter. The filter’s RC time constant affects the time needed for the signal to stabilize before the ADC can sample it. If the signal changes rapidly over time, the low-pass filter can distort a signal and thus its digital equivalent.
You must also consider other ADC parameters such as accuracy, linearity, and resolution before connecting an analog circuit to a microcontroller. But a basic understanding of the ADC’s voltage input range, sampling frequency, and input-resistance requirements can get you started connecting analog circuits to a microcontroller’s ADC.
Handling the data
After a microcontroller’s ADC digitizes analog signals, what do you do with the data? Depending on the sample rate and processing speed of the microcontroller, you may be able to process and act upon the data in real time. For example, you can use software to process the data using digital filters, or you can operate control loops with the microcontroller’s digital I/O lines.
Most 32-bit microcontrollers include a DMA (direct-memory access) controller, which lets you move data directly into system memory. A DMA controller can relieve the microcontroller’s CPU core from transferring data into memory, which increases performance.
In its simplest form, a DMA controller consists of three parts: a memory source, a memory destination, and a trigger. The ADC’s data register is usually the memory source. An array of data in the microcontroller’s RAM is the destination, and the ADC’s conversion-complete flag is the trigger. Because you usually want to capture a large number of samples without overwriting any data in memory, you need to increment a data-array pointer for each new DMA transfer. You can configure either the source or destination memory locations for auto-increment with the STM32. Other DMA options may include the ability to move different data types, such as bytes, 16-bit half-words, or 32-bit words into memory.
Microcontrollers may have several communications peripherals. Traditional communications ports include I2C, SPI, and USART (RS-232). Advanced communications ports include CAN, USB, and Ethernet. Communications ports let you move data to networked devices or to a local PC.
Microcontrollers also contain storage memory (typically 32 kbytes to 512 kbytes) of flash and EEPROM. If you need more storage, you can connect the microcontroller to an SD (secure digital) memory card that you can easily remove or replace. External memory controllers provide access to large amounts of flash memory. Through a USB port, you can connect to a flash drive or hard drive and store your data for analysis. T&MW
Sean Newton is a field applications engineer in STMicroelectronics’ marketing organization supporting microcontrollers. He has worked at the company since 2001 supporting microcontrollers, microprocessor units, and smart card products. Prior to ST, Newton worked in several manufacturing facilities with Electronic Data Systems. He graduated with a degree in electrical engineering and computer science from the University of Nevada, Reno.
Alec Bath is a field applications engineer in the microcontroller division of STMicroelectronics. He has worked at ST since 2000 supporting microcontroller and memory products. Prior to ST, Bath worked for Motorola. As an avid adventure racer and ultra marathoner, he has been tinkering with home-brew microcontroller-controlled LED headlamp systems.