ESTE: Analog-to-Digital Converter (ADC) and Sensor Reading

De MediaWiki do Campus São José
Ir para navegação Ir para pesquisar

This experiment is part of this project.

Here we are going to learn about the ADC and how to use and read sensors. The ADC or conversor A/D is a analog-digital conversor. It is an electronic device capable of generating a digital representation from an analog quantity (usually a signal represented by a voltage level or intensity of electrical current). ADCs are useful and normally necessary at the interface between digital devices (microprocessors and microcontrollers) and analog devices and are used in applications such as reading sensors, audio and video scan. For example, an ADC of 10 bits and prepared for an analog input signal with voltage that varies from 0V to 5V can take binary values from 0 (0000000000) to 1023 (1111111111) respectively. It is also capable of capturing 1024 (10 bits) levels of a given signal. For example, if we presume an ADC input signal of 2.5V, the binary value generated will be 512. (0v = 0, 5v = 1023, 2.5v = 1023/2). So if we want to use, manipulate or visualize a value generated by a sensor we must use an ADC to convert these signals to digital values and then manipulate them. Those signal generated by sensors may be unstable and vary a lot, therefore we need to perform an average(RMS) of the obtained measures, get from this average a reliable value and then convert it.

Pseudo code

Let's begin with a solution. The pseudo code (actual coding is up to you) is below:

int main(void) {
    int val = 0;
    while(1) {
        val = RMS(30);    //accumulate x mesures. Recomended x=30;
        print("Digital: %d",val);
        print("Analog: %d",to_analog(val));
    }
    return 0;
}     

int RMS(int repeat){
    accumulated = 0;
    do repeat times {
        digital_value = read(analog_value);   //convert the analog-value (voltage) to digital one (0-1024)
        accumulated = accumulated + digital_value*digital_value;      //accumulate all the digital-values “repeat” times
    }
    average = accumulated / repeat;
    return sqrt(average);
}

/*
the row above should convert the digital manipulated value to a voltage/ analogic signal again acording to the following rule:
5v - 1023        1023 x voltage = average x 5
voltage - average    voltage(Vo) = averagex 5/1023
*/
}

This pseudo-code show some functions to perform: the accumulation necessary to the average; the calculation of a precise average (RMS); the conversion from analogic signal to digital values and back. These are the elements necessary to start reading any tipe of sensor amog other applications.

¹Equation of RMS

References