1
\$\begingroup\$

enter image description hereI am trying to read analog values from multiple sensors using both the ESP32-C3 devmodule 02 and the ESP32-C3-WROOM-02 module. In both cases I use the basic Arduino analogRead example code:

#define InPin 3
int x ;

void setup() {
   Serial.begin(115200);
}

void loop()
{
   delay(100);
   x = analogRead(InPin);
   Serial.println( x );
   delay(100);
}

No matter what pin I set to read the analog value from, it always only reads the value from GPIO0 (ADC1 channel 0.)

My guess is that I have to change the ADC channel that it reads, but I have not been able to find out how.

\$\endgroup\$
6
  • \$\begingroup\$ #define InPin 3 is a comment so InPin is defaulting to 0, is it not? \$\endgroup\$
    – Transistor
    Aug 5, 2021 at 9:42
  • \$\begingroup\$ i also just used analogRead(3) and defined InPin in another way but it doesnt affect anything. \$\endgroup\$ Aug 5, 2021 at 10:25
  • \$\begingroup\$ Time for a schematic then. You can add one in using the CircuitLab button on the editor toolbar. Double-click a component to edit its properties. 'R' = rotate, 'H' = horizontal flip. 'V' = vertical flip. Use Custom Component for anything odd. Note that when you use the CircuitLab button on the editor toolbar and "Save and Insert" on the editor an editable schematic is saved in your post. That makes it easy for us to copy and edit in our answers. You don't need a CircuitLab account, no screengrabs, no image uploads, no background grid. \$\endgroup\$
    – Transistor
    Aug 5, 2021 at 10:32
  • \$\begingroup\$ i added it, its literealy this simple circuit. it works normaly as it should if i connect the 2V to gpio pin 0. The problem is it doesnt want to read any other pin \$\endgroup\$ Aug 5, 2021 at 10:40
  • \$\begingroup\$ @aleskramzar: Have you selected the proper hardware in the Arduino IDE? The selected hardware defines how the Arduino functions access the hardware. If you pick the wrong one, things don't work right. \$\endgroup\$
    – JRE
    Aug 5, 2021 at 10:44

1 Answer 1

3
\$\begingroup\$

If you look at the esp32c3 data sheet ( https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf ) you will see that the esp32c3 implements only GPIO2, 3, 8, 9, 18, & 19. https://github.com/espressif/esp-idf/blob/v4.3.1/components/driver/include/driver/adc_common.h , shows that the only adc channels available are adc1, channels 2 & 3.

I have used the following code to read analog voltage using esp32c3, Arduino IDE v 1.8.19

#include "driver/adc.h"
adc1_channel_t channel = ADC1_CHANNEL_2;
adc_atten_t atten = ADC_ATTEN_DB_2_5;
void setup() {
  Serial.begin(115200);
  adc1_config_width(ADC_WIDTH_BIT_12);
  adc1_config_channel_atten(channel, atten);
}
void loop() {
  int read_value = adc1_get_raw(channel);
  Serial.println(read_value);
  delay(1000);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.