Tuesday 25 July 2017

Analog to Digital Conversion using ESP8266

Analog to Digital Conversion


The datasheet describes the ADC pin as having 10 bit resolution. This means 0 to 1024. You should get a value somewhere within this range.
Here the fun begins! Apparently, the module only converts voltage between 0 and 1 volt.
As an example, I have a 10k rheostat hooked up to the ADC pin. Fully moving the slider to the position where I would expect a 0 reading, the ADC TOUT pins reads 13~15, not 0 but encouraging!
However, as I slide the slider to the right, I see the ADC TOUT reaches the maximum reading of 1024 in about 1/3 the distance.
Ideally, I want the slider to register 0 to 1024 increasing or decreasing over 100% of the slider travel.
Reaching the maximum reading in 1/3 of the travel makes sense, the ADC pin only reads up to 1 volt. Any reading over that, say 1.1v to Vcc of 3.3v will be maxed out to 1024.
So, I need to supply the ADC pin a voltage between 0 and 1 volt only.
Using a voltage divider greatly improves the situation.


Voltage Divider 




Source Code 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
Get_Anolog_Process();
delay(100);
}


int ADC_Sum=0;
int Battery_Value=0;
int ADC_Count=0;
int Round_OFF=0;

void Get_Anolog_Process(void)
{
 
       ADC_Sum+=analogRead(A0);ADC_Count++;
        if(Battery_Value==0){
            Battery_Value=(int)(3000*((ADC_Sum/ADC_Count)/1024.0));
         }
        if(ADC_Count>=10)
        {                
            Battery_Value=(int)(3000*((ADC_Sum/ADC_Count)/1024.0));
            ADC_Count=0;ADC_Sum=0;
             Serial.print("Voltage: ");
           Serial.print(Battery_Value);
           Serial.println(" mV");
        } 

No comments:

Post a Comment

ESP8266 Multiple Timer

ESP8266 Maximum upto 7 OS_Timer Source code : #include <Arduino.h> extern "C" { #include "user_interface.h"...