esp32 does not read analogue values

Nikosant03
Posts: 12
Joined: Wed Mar 13, 2019 11:44 pm

esp32 does not read analogue values

Postby Nikosant03 » Thu Mar 14, 2019 6:32 pm

Hi every one

I try to connect an analogue luminance sensor that I bought from Seed. I use arduino IDE to program my esp32 devkic.

I cannot take any measurement from my esp and I figured out the don't read the analogue values. I try a lot of adc pins with no luck

Any suggestion? Do I need to #include any adc driver?

Code: Select all

float VoutArray[] =  { 0.0011498,  0.0033908,   0.011498, 0.041803,0.15199,     0.53367, 1.3689,   1.9068,  2.3};
float  LuxArray[] =  { 1.0108,     3.1201,  9.8051,   27.43,   69.545,   232.67,  645.11,   73.52,  1000};

int sensor =34;


void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    pinMode(sensor, INPUT);

}

void loop() {
    // put your main code here, to run repeatedly:
    Serial.println(analogRead(sensor));
    Serial.print("Vout =");
    Serial.print(readAPDS9002Vout(sensor));
    Serial.print(" V,Luminance =");
    Serial.print(readLuminance(sensor));
    Serial.println("Lux");
    delay(1000);
}

float readAPDS9002Vout(uint8_t analogpin)
{
    // MeasuredVout = ADC Value * (Vcc / 1023) * (3 / Vcc)
    // Vout samples are with reference to 3V Vcc
    // The above expression is simplified by cancelling out Vcc
    float MeasuredVout = analogRead(sensor) * (3.0 / 4095.0);
    //Above 2.3V , the sensor value is saturated

    return MeasuredVout;

}

float readLuminance(uint8_t analogpin)
{

    // MeasuredVout = ADC Value * (Vcc / 1023) * (3 / Vcc)
    // Vout samples are with reference to 3V Vcc
    // The above expression is simplified by cancelling out Vcc
    float MeasuredVout = analogRead(sensor) * (3.0 / 4095.0);
    float Luminance = FmultiMap(MeasuredVout, VoutArray, LuxArray, 9);

    /**************************************************************************

    The Luminance in Lux is calculated based on APDS9002 datasheet -- > Graph 1
    ( Output voltage vs. luminance at different load resistor)
    The load resistor is 1k in this board. Vout is referenced to 3V Vcc.

    The data from the graph is extracted using WebPlotDigitizer
    http://arohatgi.info/WebPlotDigitizer/app/

    VoutArray[] and LuxArray[] are these extracted data. Using MultiMap, the data
    is interpolated to get the Luminance in Lux.

    This implementation uses floating point arithmetic and hence will consume
    more flash, RAM and time.

    The Luminance in Lux is an approximation and depends on the accuracy of
    Graph 1 used.

    ***************************************************************************/

    return Luminance;
}


//This code uses MultiMap implementation from http://playground.arduino.cc/Main/MultiMap

float FmultiMap(float val, float * _in, float * _out, uint8_t size)
{
    // take care the value is within range
    // val = constrain(val, _in[0], _in[size-1]);
    if (val <= _in[0]) return _out[0];
    if (val >= _in[size-1]) return _out[size-1];

    // search right interval
    uint8_t pos = 1;  // _in[0] allready tested
    while(val > _in[pos]) pos++;

    // this will handle all exact "points" in the _in array
    if (val == _in[pos]) return _out[pos];

    // interpolate in the right segment for the rest
    return (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];


    
}

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: esp32 does not read analogue values

Postby idahowalker » Fri Mar 15, 2019 11:55 am

I find that the ESP32 does, indeed, read analog values and does a nice job doing so.
In the include section I use

Code: Select all

#include <driver/adc.h>
In setup I use

Code: Select all

 adc1_config_width(ADC_WIDTH_12Bit);
  adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11);
You may want to read, up to you:
https://dl.espressif.com/doc/esp-idf/la ... s/adc.html

Using analog read, the Arduino way is not as accurate as using the ESP32 way.

Remember the ESP32 is a 3.3 volt device so you may, up to you, want to use a voltage divider, with 1% resistors.
I use // http://www.ohmslawcalculator.com/voltag ... calculator to figure out, easy, voltage dividers. For R1 I use 100K as my default choice.

If you have put more then 3.3 volts onto the ADC pin, your ESP32 is dead.

Nikosant03
Posts: 12
Joined: Wed Mar 13, 2019 11:44 pm

Re: esp32 does not read analogue values

Postby Nikosant03 » Fri Mar 15, 2019 8:19 pm

Dear idahowalker thank you for your detailed responce.

Yes I know that ESP32 way is more accurate but I am not so familiar at the moment with ESP32 functions and programming.

The sensor I use is from here http://wiki.seeedstudio.com/Grove-Luminance_Sensor/ with Linear output range 0.0 ~ 2.3V.
so it not going to damage the ADC pins

My problem at the moment isn't the accuracy but to read some measurements from ESP32 ADC pin where the sensor is connected. When I upload the sketch and read the analog input of ESP32 I read 0.

I know that the sensor work since it works with arduino. So, I suppose that ESP32 cannot read the analog input...

Do I have to include any library into my sketch related with ADC?

Thank in advance

Edje11
Posts: 18
Joined: Thu May 17, 2018 10:33 am
Contact:

Re: esp32 does not read analogue values

Postby Edje11 » Fri Mar 15, 2019 9:49 pm

Don't set your ADC channel as input

Code: Select all

pinMode(sensor, INPUT);

Nikosant03
Posts: 12
Joined: Wed Mar 13, 2019 11:44 pm

Re: esp32 does not read correct analogue values

Postby Nikosant03 » Sun Mar 17, 2019 12:35 am

I started receive some measurements but are quite inaccurate. I compared the voltage readings of esp32 with that of my multi meter and there is high deviation. For example with the multimeter I read 0.14V while with esp32 0.01V.

I suppose that this is not some issue of my code. The ADC reading is quite low than it should be... I have also test the code with Arduino and it works very well and the reading are the same as the multimeter.

I am really new in this topic so please do not snub me :)

Code: Select all

#define Analog_Pin 35

void setup() {
  Serial.begin(9600);

}

void loop() {

Serial.print("ADC Reading is= ");
Serial.println(analogRead(Analog_Pin));

Serial.print("The voltage is= ");
Serial.println(voltage());

delay(2000);

}

float voltage(){

float v= analogRead(Analog_Pin)*(3.3/4095.0); //Measure the voltage

return v;
  
}

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: esp32 does not read analogue values

Postby idahowalker » Sun Mar 17, 2019 1:20 pm

Have you read:
https://dl.espressif.com/doc/esp-idf/la ... s/adc.html or will you snub a suggestion that may be your solution?

edleno
Posts: 4
Joined: Sat Mar 27, 2021 2:06 am

Re: esp32 does not read analogue values

Postby edleno » Sat Mar 27, 2021 2:13 am

Just wanted to add that using Bluetooth ALSO disables ADC2 port so GPIOs 0, 2, 4, 12 - 15 and 25 - 27 can't be used with either wifi or bluetooth.

Who is online

Users browsing this forum: No registered users and 33 guests