Blog

Acquiring Analog Sensor Data in Low-Power Applications

Examples of acquiring data from analog sensors using a microcontroller with an internal ADC in low-power applications.

Introduction

An analog sensor produces an output signal that varies in a known way as a function of the physical property being sensed. Depending on the type of analog sensor, the output signal may be a voltage, current, frequency, phase, etc. This application note provides two examples of acquiring data from analog sensors using a microcontroller (MCU) with an internal Analog-to-Digital Converter (ADC) peripheral. The first example demonstrates the use of a Resistance Temperature Detector (RTD) to measure temperature. The second example demonstrates current measurement in a circuit using a sense resistor. Both examples utilize techniques for minimizing power consumption.

Features

  • Acquisition of Analog Sensor Data Using the Microcontroller’s Internal Analog-to-Digital Converter (ADC)
  • Techniques for Minimizing Power Consumption
  • Two Examples
  • Resistance Temperature Detector (RTD) Measurement (Voltage Sensing)
  • Current Measurement (Current Sensing)

Relevant Devices

This section lists the relevant devices for this document. The following figures show the different family devices, laying out pin count variants and memory sizes:

  • Vertical migration upwards is possible without code modification, as these devices are pin-compatible and provide the same or more features
  • Horizontal migration to the left reduces the pin count and, therefore, the available features
  • Devices with different Flash memory sizes typically also have different SRAM and EEPROM

img-1.jpeg
Figure 1. AVR EA Family Overview

Overview

Many systems include one or more analog sensors that periodically must be checked. Analog sensors that generate voltage or current outputs are quite common. These signals can be measured using a microcontroller’s Analog-to-Digital Converter (ADC) peripheral. In battery-powered systems, it is crucial to minimize power consumption using techniques such as placing the microcontroller in a sleep mode between measurements.

This application note describes two examples of low-power analog sensor measurements using the AVR EA family of microcontrollers. The first example demonstrates the use of a Resistance Temperature Detector (RTD) to measure temperature. The second example shows current measurement in a circuit using a sense resistor.

RTD Measurement (Voltage Sensing) Example

In this example, the AVR EA microcontroller is used to periodically drive a Resistance Temperature Detector (RTD) with current, measure the voltage across the RTD, and calculate both resistance and temperature of the RTD. The only hardware needed in addition to the microcontroller is a 1.8 kOhm fixed resistor and the RTD itself. The type of RTD used is a Pt100, meaning that it is made of platinum and has a resistance of 100 Ohm at 0 deg C.

Hardware Block Diagram

The figure below is a high-level hardware block diagram showing the peripherals and components used in the RTD measurement and how they are connected.

img-2.jpeg

Theory of Operation

To measure the RTD, the Digital-to-Analog Converter (DAC) is enabled to produce an output of approximately 1.8V. The DAC on AVR EA is buffered so it can drive low-impedance loads. Since the DAC voltage is applied to a 1.8 kOhm fixed resistor in series with the RTD, the current flowing through the RTD will be under 1 mA. RTD datasheets usually provide guidance for maximum current, but generally, a current of less than 1 mA is recommended because it prevents the RTD from any significant self-heating. The ADC is used to measure the voltage across the RTD, which can be converted into the sensor resistance and thus, its temperature.

The resistance of the RTD varies in a known way as a function of temperature (T), by the following equations, where R0 is the resistance of the RTD at a temperature of 0 deg C and A, B and C are constants provided by the RTD manufacturer:

For T of 0 deg C or higher,

$$
R(T) = R_0 left(1 + AT + BT^2right)
$$

For T of -200 deg C to 0 deg C,

$$
R(T) = R_0 left(1 + AT + BT^2 + CT^3(T – 100)right)
$$

If the resistance of the RTD is known, then the formulas above can be used to determine the temperature. Here are the relevant formulas for determining the resistance of the RTD:

First, the voltage across the RTD is determined by the voltage divider equation, where R_F is the resistance of the fixed resistor and R_TD is the resistance of the RTD:

$$
V_{RTD} = left(frac{R_{TD}}{R_F + R_{TD}}right) cdot V_{REF}
$$

The ADC digital result x, when the ADC is used in differential mode, is determined by the following equation (from the device datasheet), where G_PGA is the gain of the PGA:

$$
x = left(frac{V_{RTD} cdot G_{PGA}}{V_{REF}}right) cdot 2048
$$

The voltage divider equation from above can be substituted in for V_RTD, causing V_REF to cancel out:

$$
x = left(frac{R_{TD}}{R_F + R_{TD}}right) cdot G_{PGA} cdot 2048
$$

This equation can be solved for R_TD, the resistance of the RTD:

$$
R_{TD} = left(frac{x cdot R_F}{G_{PGA} cdot 2048 – x}right)
$$

Note that V_REF does not appear in the equation, so errors in the reference voltage value will not affect the result. The only parameters needed to compute the RTD resistance are the resistance value of the fixed resistor, the ADC result, and the PGA gain value.

Hardware Prerequisites

  • AVR64EA48 Curiosity Nano (CNANO) Development Board
  • RTD (Pt100) Sensor
  • 1.8 kOhm Resistor

Hardware Setup

Hardware connections are as follows:

img-3.jpeg

  • The DAC0OUT pin of the device (PD6 on AVR EA) must be connected to the VREF pin of the device (PD7 on AVR EA) so the DAC output provides the reference voltage for the ADC
  • The 1.8 kOhm fixed resistor must be connected from the DAC0OUT pin (PD6 on AVR EA) to the ADC0 AIN0 pin (PD0 on AVR EA)
  • The RTD must be connected from the ADC0 AIN0 pin (PD0 on AVR EA) to ground
  • The ADC0 AIN1 pin (PD1 on AVR EA) must be connected directly to ground

Software Prerequisites

MPLAB version:

  • MPLAB X IDE v6.05 or newer
  • Microchip AVR64EA48 Device Support Pack AVR-Ex_DFP v2.4.168 or newer
  • MPLAB XC8 Compiler v2.41 or newer
  • MPLAB Code Configurator (MCC)
  • MCC Plugin v5.3.0 or newer
  • MCC Core v5.5.0 or newer

  • Microchip Studio for AVR and SAM Devices v7.0.2542 or newer

  • Microchip AVR64EA48 Device Support Pack AVR-Ex_DFP v2.2.56 or newer
  • AVR GCC 5.4.0 compiler or newer (automatically installed with Microchip Studio)

For debugging purposes, it can be helpful to install the MPLAB Data Visualizer:

  • MPLAB Data Visualizer

Implementation Details

To minimize power consumption, the AVR EA is configured to stay in Power-Down sleep mode whenever a measurement is not in progress. In this sleep mode, AVR EA consumption was measured as approximately 0.9 uA (with V_DD = 3.3V). The PIT (Periodic Interrupt Timer), a part of the RTC (Real Time Counter), is set up to periodically generate an interrupt to bring the device out of sleep mode.

When this happens, the DAC is enabled to produce an output voltage of 1.8V, and the ADC is re-enabled. The ADC is commanded to start a differential conversion immediately. While the ADC conversion is in progress, the CPU performs the calculations necessary for converting the previous ADC value into resistance and temperature. As soon as the ADC conversion is complete and the result is saved, DAC and ADC are disabled, and the device is put back to sleep.

The DAC and ADC are enabled after the device comes out of sleep. The DAC output stabilizes before the ADC is ready to start its first conversion, so there is no need for additional delays in the software.

Various strategies were tested to minimize power consumption (higher/lower CPU and ADC clock speeds, PGA on/off with less/more conversions). But in this case, the overriding issue is the fact that the DAC must supply nearly 1 mA of current to the RTD sensor while AD conversions are in progress. Therefore the best strategy is to run both the CPU and ADC as fast as possible (10 MHz and 5 MHz clocks, respectively) with maximum PGA gain so the time that the DAC must supply 1 mA is minimized. During the Power-Down Sleep mode, the 10 MHz clock source is disabled, and only the internal 32 kHz oscillator and the RTC clock source are running. With this configuration, a burst of 16 ADC conversions takes only 155 us.

During that 155 microseconds of conversion time with DAC enabled, the microcontroller supply current measured 4.7 mA (this includes what the DAC needed to drive the RTD, with V_DD = 3.3V). If there is one conversion per second, the average current will be (155 us/1s) x 4.7 mA = 1.55e-4 x 4.7 mA = 0.73 uA.

Average current consumption is thus expected to be the sleep current of 0.9 uA + (0.73 uA) x n, where n is the number of RTD temperature measurements per second:

n Measured Average Current Consumption (with V_DD = 3.3V)
2 per second 2.4 uA
4 per second 3.7 uA
8 per second 6.6 uA
16 per second 12 uA
32 per second 24 uA
64 per second 47 uA

Source Code Overview

The key peripherals and parameters used in the source code are:

  • Main clock (CPU, peripherals) frequency: 10 MHz
  • Peripherals
  • RTC
    • RTC clock frequency: 1.024 kHz
    • PIT period: 512 cycles (0.5 seconds)
  • SLPCTRL
    • Sleep mode: Power-Down
  • DAC
    • Voltage reference: V_DD (3.3V by default on CNANO board)
    • Output voltage: 1.8V
  • ADC
    • Clock frequency: 5 MHz (main clock divided by 2)
    • Voltage reference: VREFA
    • Sample accumulation: 16
    • PGA gain: 16x, PGA bias current: 100%
  • Interrupts
  • RTC PIT Interrupt (RTC_PIT_vect)

The following figure shows the high-level flow of the code:

img-4.jpeg
Figure 2. High-level flow of voltage sensing code

Current Measurement (Current Sensing) Example

Some analog sensors, for example photodiodes and phototransistors, generate a current output. It may also be interesting to measure the current at a point in a circuit. In this example, the AVR EA microcontroller periodically measures a current using its internal ADC peripheral. Because the ADC can only sample a voltage, the current is sent through a “sense” resistor with a known resistance value to convert the current to a voltage. The current can then be calculated from the measured voltage.

Hardware Diagram

This example uses the following hardware configuration to measure the current:

img-5.jpeg

  • The DAC0 OUT pin of the device (PD6 on AVR EA) must be connected to the ADC0 IN1 pin of the device (PD0 on AVR EA) via resistor R1
  • The sensing resistor R_SENSE is connected between the ADC0 inputs IN1 and IN0 (PD0 and PD1 on AVR EA)
  • The ADC0 AIN0 pin (PD1 on AVR EA) must be connected to ground via resistor R2

This example uses the following values:

$$
R_1 = R_2 = 100 kOmega
$$
$$
R_{SENSE} = 10 kOmega
$$
$$
V_0 = 1V
$$

Theory of Operation

The DAC is used to create a current source. Although the DAC outputs a voltage signal V_0, the resistors in the circuit will create a current I_0 that is given by:

$$
I_0 = frac{V_0}{R_1 + R_{SENSE} + R_2}
$$

The voltage divider rule gives the following equation for the voltage drop across R_SENSE:

$$
V_{SENSE} = V_0 times frac{R_{SENSE}}{R_1 + R_{SENSE} + R_2}
$$

If the expression for I_0 is substituted (the same current flows through all the resistors), the following expression is obtained:

$$
V_{SENSE} = I_0 cdot R_{SENSE}
$$

This, in turn, leads to:

$$
I_0 = frac{V_{SENSE}}{R_{SENSE}}
$$

Since R_SENSE is known, measuring the voltage drop across R_SENSE may determine the current. With the output voltage V_0 set to 1V (DAC0 OUT) and using the resistor values as listed, the following result is obtained:

$$
V_{SENSE} = V_0 times frac{R_{SENSE}}{R_1 + R_{SENSE} + R_2} =
$$
$$
1.0V times frac{10 kOmega}{100 kOmega + 10 kOmega + 100 kOmega} = 47.6 mV
$$
$$
I_0 = frac{V_{SENSE}}{R_{SENSE}} = frac{47.6 mV}{10 kOmega} = 4.8 mu A
$$

Hardware Prerequisites

  • AVR64EA48 Curiosity Nano (CNANO) Development Board
  • 10 kOhm Resistor
  • Two 100 kOhm Resistors

Software Prerequisites

MPLAB version:

  • MPLAB X IDE v6.05 or newer
  • Microchip AVR64EA48 Device Support Pack AVR-Ex_DFP v2.4.168 or newer
  • MPLAB XC8 Compiler v2.41 or newer
  • MPLAB Code Configurator (MCC)
  • MCC Plugin v5.3.0 or newer
  • MCC Core v5.5.0 or newer

  • Microchip Studio for AVR and SAM Devices v7.0.2542 or newer

  • Microchip AVR64EA48 Device Support Pack AVR-Ex_DFP v2.2.56 or newer
  • AVR GCC 5.4.0 compiler or newer (automatically installed with Microchip Studio)

For debugging purposes, it can be helpful to install the MPLAB Data Visualizer:

  • MPLAB Data Visualizer

Hardware Setup

The hardware setup is explained in the Hardware Diagram section above.

Implementation Details

The AVR EA device is configured to stay in Power-Down sleep mode whenever a measurement is not in progress to minimize power consumption.

The Periodic Interrupt Timer (PIT), a part of the Real Time Counter (RTC), is set up to generate an interrupt each second to bring the device out of sleep mode. When this happens, a counter is incremented and checked against a predefined period (10s).

If the value matches this period, the DAC is enabled to produce an output voltage of 1.0V, and the ADC is enabled. The ADC is commanded to start a differential conversion immediately. While the ADC conversion is in progress, the CPU performs the calculations necessary for converting the previous ADC value into a voltage and a current. After the ADC conversion is complete, the DAC and ADC are disabled, and the device goes back into sleep mode.

In the example code, USART1 can be used to output the measured voltage and calculated current to a terminal. The “#define USART_ON” must be included to enable this.

When measuring small signals, the PGA may be enabled to amplify the input signal to get better resolution on the measurement. In this example, the PGA gain is set to 16x. The PGA bias is set to the maximum of 100%, because the highest bias allows the ADC sampling duration to be minimized (see device datasheet).

The following table shows the average current consumptions using different configurations (with V_DD = 3.3V):

Main Clock PGA Disabled (ADC), Average Current PGA Enabled (ADC), Average Current (uA)
2 MHz 1.7 uA 1.6 uA
3.33 MHz 1.4 uA 1.3 uA
10 MHz 1.2 uA 1.1 uA

The average current consumption with the PGA enabled was lower than when the PGA was off, not following the theoretically expected result that using the PGA should result in higher current consumption. Testing showed that by not entering sleep and sampling the ADC on each interrupt (each second) with the main clock at 10 MHz, the results were as expected:

PGA (ADC) Average Current Consumption
PGA OFF 3.48 mA
PGA ON 3.50 mA

This suggests that the higher average current consumption measured when PGA is off compared to when the PGA is enabled in the initial measurement is due to a combination of the time between each ADC measurement and the code execution order.

Source Code Overview

The key peripherals and parameters used in the source code are:

  • Main clock (CPU, peripherals) frequency: 10 MHz
  • Peripherals
  • RTC
    • RTC clock frequency: 1.024 kHz
    • PIT period: 1024 cycles (1 second)
  • SLPCTRL
    • Sleep mode: Power-Down
  • DAC
    • Voltage reference: V_DD (3.3V by default on CNANO board)
    • Output voltage: 1.0V
  • ADC
    • Clock frequency: 1 MHz (main clock divided by 10)
    • Voltage reference: V_DD (3.3V by default on CNANO board)
    • Sample accumulation: 16
    • PGA gain: 16x, PGA bias current: 100%
  • USART
    • Baud rate: 115200
  • Interrupts
  • RTC PIT Interrupt (RTC_PIT_vect)

The following figure shows the high-level flow of the code:

img-6.jpeg
Figure 3. High-level flow of current sensing code

Get Code Examples from MPLAB Discover

MPLAB Discover bundles the available resources from multiple sources into one portal. The MPLAB Discover webpage: MPLAB Discover

img-7.jpeg

Code Examples

  • Example code for MPLAB X

You can download code examples as a .zip file directly from MPLAB Discover. Repositories ending with “studio” can be opened in Microchip Studio. Repositories ending with “mplab-mcc” can be opened in MPLAB X.

When the code example is hosted on GitHub, MPLAB Discover provides a link “Open with Github”. There you can download the example code or use the git tool on your PC to create a local repository clone.

Additional Resources

This Might Also Interest You

New Partnership: SEMITRON Brings GigaDevice Technologies to the DACH Region

SEMITRON W. Röck GmbH, a leading distributor of high-quality semiconductor and electronics solutions, announces its new distribution partnership with GigaDevice…

What are the Best Applications for IoT in the New World of IC Power Management?

This article explores Internet of Things (IoT) battery technology. It describes some of the problems that designers face with power…

What Are the Basic Guidelines for Layout Design of Mixed-Signal PCBs?

This article will detail what to consider when designing the layout of mixed-signal PCBs. It will cover component placement, board…