Page 1 of 1

Timer seems to break ADC

Posted: Mon Dec 11, 2017 3:15 pm
by robogrow
ESP32 analogRead too much noise when attaching timer to pin.

using WROOM 32 module and nodeMCU 32S ardunio board selection.

I can get a stable analog value on a seperate Nano from the sensor clocked from the ESP32.

#define CHANNEL 0
//#define FREQUENCY 40000000
#define FREQUENCY 40000000
#define OUTPIN 17

#define BITDEPTH 1
#define DUTY 1

#include <driver/adc.h>

int val=0;

const int aread_pin=32;
//#define aread_pin 32
void setup() {
pinMode(aread_pin,INPUT);
Serial.begin(115200);
int result =ledcSetup(CHANNEL, FREQUENCY, BITDEPTH);
delay(1000);
Serial.print ("result ok");
Serial.println(ESP_OK);

Serial.print ("result fail");
Serial.println(ESP_FAIL);

Serial.print ("result invalid");
Serial.println(ESP_ERR_INVALID_ARG);

Serial.print ("result ");
Serial.println(result);

ledcAttachPin(OUTPIN, CHANNEL);
ledcWrite(CHANNEL, DUTY);

//analogReference(DEFAULT);

//analogSetAttenuation(ADC_6db);
analogReadResolution(12);

adc1_config_width(ADC_WIDTH_11Bit);
adc1_config_channel_atten(ADC1_CHANNEL_4,ADC_ATTEN_0db);
}

void loop() {

val=0;
for (int i=0; i<100; i++) {
val=val+adc1_get_voltage(ADC1_CHANNEL_4);
//val=val+analogRead(aread_pin);
delay(20);
}
val=val/100;


Serial.print("anlog read = ");
// int val = adc1_get_voltage(ADC1_CHANNEL_4); // pin 32
Serial.println(analogRead(aread_pin));
Serial.println(val);
//Serial.println(analogRead(val));
delay(1000);
}

Output:

anlog read = 993
867
anlog read = 632
850
anlog read = 688
845
anlog read = 477
869
anlog read = 1056
866
anlog read = 483
839
anlog read = 688
829
anlog read = 950
861
anlog read = 1184
839
anlog read = 868
882
anlog read = 1047
851
anlog read = 864
876
anlog read = 960
875
anlog read = 482
854
anlog read = 640
873
anlog read = 624
817
anlog read = 1028
890
anlog read = 752
847
anlog read = 1174
858
anlog read = 880
831
anlog read = 754
900
anlog read = 843
858
anlog read = 1184
867
anlog read = 477
877
anlog read = 1005
879
anlog read = 1178
882
anlog read = 776
863
anlog read = 1029
904
anlog read = 884
859
anlog read = 656
898
anlog read = 769
859
anlog read = 921
864
anlog read = 1155
862
anlog read = 944
853
anlog read = 666
878
anlog read = 950
855
anlog read = 825
842
anlog read = 705
865
anlog read = 971
869