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();
}