Interfacing to Sensors


ADC and Analog Sensors

Analog-to-digital converters are widely used in data acquisition. Basically, an ADC converts analog signal into digital signal. This is required because in the physical world we live  every quantity we interact with is analog in nature.By physical quantities, i mean quantities like temperature, humidity, weight, velocity pressure etc. However, digital computers use  discrete or binary values only, this brings us to transducers. A transducer is a device used to convert physical quantities to electrical signal i.e. voltage and current. When this transducer is used to generate output then it is known as sensor. Hence we have temperature sensor, pressure sensor, humidity sensor and so on. 

Click here to jump to ADC example source code

Resolution of an ADC

The ADC resolution is defined as  the smallest change the ADC can measure.

An ADC usually has  n-bits resolution.

where n = 8,10,12,16 or 24

The higher the resolution, the smaller the change the ADC is able to measure. The resolution of the ADC is set during the design of the ADC meaning we as embedded system designers and developers do have direct control over the resolution . However, we do have an indirect means to control the resolution. We can control the  smallest amount of voltage we want the ADC to increment on . This is know as is known as the step size . We can  achieve this  by  manipulating the reference voltage,Vref. In the block diagram presented below we can see that the ADC has two voltage sources, one is labelled Vin and the other Vref. Vin, is the input voltage i.e. where we connect the pin of the sensor  and Vref is the reference voltage we want to use to adjust the step size.

adc block digram

Block diagram of 8-bits ADC


8-bits ADC    has  2e8     = 256 total number of steps

12-bits ADC   has  2e12   = 4096 total number of steps

To find the step size, we have to divide the reference voltage, Vref by the  total number of steps.

For example, the step size for an 8-bits ADC connected to a 5V source as its Vref will be :

5V / 256 = 0.0195 V =19.5mV

Similarly, a 12-bits ADC with a Vref of 5V will have a step size of :

5V/ 4096 =0.00122V = 1.2mV

Again, if we use 3V as Vref for the 8-bits ADC, this is the step value we get

3V /256 = 11.7mV . I will leave the computation of 12-bits with 3V Vref for you as homework.

As we can see, having the flexibility of regulating the reference voltage allows to be able to set our own resolution in a way.

Some sensors come with two input pins. Vref(+)  and Vref(-) . In this case, we connect the Vref(-) pin to the ground and use Vref(+) as normal Vref  just like we did above.

Input Voltage Digital Data Output

To calculate the digital output voltage i.e. the ADC value,

we simply need to divide the  input voltage by the step size.

Output voltage  = Vin/ step size

In an 8-bits ADC, there is 8-bits data output i.e. D0 – D7.

For example, lets say we have an 8-bits ADC using 3V as Vref

and then we are asked to find the 8-bits output value,D0 -D7  

when we apply 1.8V as input voltage,Vin.


step size = Vref /8-bits = 3V/256 = 11.7mV

Output voltage = Vin /step size = 1.8V /11.7mV = 153.6 , round to 154

                               154 = 10011010  in binary

Therefore, D0 – D7  = 10011010

TI TM4C123 ADC Block

TI TM4C123 ADC Block

Programming ADCs on Cortex-M

         1.Enable the ADC clock

Cortex – Microcontrollers from different manufactures usually come with                    more than one ADC. For instance, the TM4C123 Tiva C Cortex-M4                               microcontroller from Texas Instruments has two ADC’s, ADC0 and ADC1.

      2. Select the Sample Sequencer

The job of the Sample Sequencer is to move the conversion results of the ADC to a FIFO. ADCs come with more than one Sample Sequencers and for each Sample Sequencer a matching FIFO is provided . The sample sequencer adopts the depth of its matching FIFO as its length so we find Sample Sequencers with 8 samples of length, 4 samples samples of length and so on.

         3. Select start conversion trigger

We can decide to make Timers, PWM,GPIO, Analog Comparators and software as the trigger of the ADC to start conversion. The default setting is software trigger.

         4. Choose Input Voltage Channel

This is where we select the GPIO pin that our sensor is connected. Not every  GPIO pin can perform ADC functions, to find out which GPIO pins are capable of ADC functions, we have to consult the datasheet of the microcontroller and locate the part that says analog input pins assignment. For example, the TM4C123 has twelve analog input channels  named AIN0(PE3) – AIN11(PB5)

         5. Detect the end of conversion

The end-of-conversion is indicated by a flag bit in one of the ADC register. We can  simply poll this flag to detect whether its state has changed or use an interrupt for  the detection.

          6. Read results

The binary results of the ADC is stored in a register. We simply need to read the content of this register.

           7. Clear flag

After reading the results, we need to clear the flag so that we may be able to detect another conversion.

London, U.K