ADC analogSetCycles = 1 yields all analogReads = adc max

letmeout
Posts: 4
Joined: Tue Oct 13, 2020 1:10 am

ADC analogSetCycles = 1 yields all analogReads = adc max

Postby letmeout » Wed Oct 14, 2020 11:08 pm

While exploring how adc1 responds to option setting limits, I came across this one unexpected behavior.
If analogSetCycles(1) is called before calling analogRead, adc1 only produces readings of 4095 (max value for default 12 bit resolution).
Other settings tried included 2,8,127, and 255, all of which read in mid range for the same external hall sensor outputting ~2.3v at rest.
The other related curiosity is that SET_PERI_REG_BITS appears to happily set cycles to 1, as readings are in expected range.
Please review the included code and point out the error of my ways or maybe even compile, run, and confirm the same unexpected behavior.

Code: Select all

#include <soc/sens_reg.h>
//#include <soc/sens_struct.h>

const uint8_t numCycles             = 1;    // 2,8,127, or 255 all yield mid scale readings
const byte analogGPIO               = 32;   // pin for volts in from source's analog output (aprox 2.3v)
const int unsigned numTakeReadings  = 100;  // at 100 readings, little difference between sample/population deviation

void setup(){
    Serial.begin(115200, SERIAL_8N1);
    while (!Serial) {};  Serial.println();
    Serial.printf("Reading gpio pin %u.  Is it wired? \n\n", analogGPIO);
    
    analogSetCycles(numCycles);     // comment this line to set cycles with SET_PERI_REG_BITS 
    
    // this, implemented in soc.h, seemed to happily set cycles = 1, at least mid range readings
    //  resulted, unless analogSetCycles(1) was called first
    SET_PERI_REG_BITS(SENS_SAR_READ_CTRL_REG, SENS_SAR1_SAMPLE_CYCLE, numCycles, SENS_SAR1_SAMPLE_CYCLE_S);
    SET_PERI_REG_BITS(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_SAMPLE_CYCLE, numCycles, SENS_SAR2_SAMPLE_CYCLE_S);
    
    delayMicroseconds(100);     // delay seems to allow adc voltages to stabilize
    int buf[1][numTakeReadings] = {0,0};
    uint32_t tStart = micros();
    
    for (int r=0; r<numTakeReadings; r++) {
        buf[0][r] = analogRead(analogGPIO);     // aprox 11 µs per reading
    } uint32_t tElapsed = micros() - tStart;
    
    for (int r=0; r<numTakeReadings; r++) {
        Serial.printf("%u\n", buf[0][r] );
    } Serial.printf("\n %u µs/reading (int div trunc'd) \n", tElapsed / numTakeReadings);
}

void loop(){
    Serial.println("\n ** Going to deep sleep. Reset to run again. **");
    delay(100);  Serial.println("");   // for esp32 so last line prints before sleep
    esp_deep_sleep_start();
}

GuillermoBastian94
Posts: 2
Joined: Thu Dec 24, 2020 5:34 pm

Re: ADC analogSetCycles = 1 yields all analogReads = adc max

Postby GuillermoBastian94 » Thu Dec 24, 2020 6:10 pm

That also is happening to me. Im using 2 analog sensors and when I move one of them to max value, all the other analog reads are getting the max value also.

Could u solve this issue?

Thanks,

letmeout
Posts: 4
Joined: Tue Oct 13, 2020 1:10 am

Re: ADC analogSetCycles = 1 yields all analogReads = adc max

Postby letmeout » Fri Dec 25, 2020 2:07 pm

Avoidance was the path of least resistance taken, if memory serves.
That means the 'solution' was to set analogSetCycles to any valid value, except one.

Who is online

Users browsing this forum: Google [Bot] and 91 guests