analogRead() does not work in Iot Cloud

I am using an ESP32-Board (NodeMCU ESP32 from joy-it) and want to read a voltage value. I have connected an 100K potentiometer between GND and 3,3V and the pin in the middle of the pot to GPIO 13.

If I now upload this code in the Iot Cloud and open the Serial Monitor it prints "0" the value of "pot", even when i connect 3,3 V to GPIO 13.

  /*Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  int pot;
  bool led;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

int pinPot = 13;
int ledpin = 2;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);
  
  pinMode(ledpin, OUTPUT);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
  */
  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
}


void loop() {
  ArduinoCloud.update();
  // Your code here
  pot = analogRead(pinPot);
  Serial.println(pot);
  delay(500);

}

/*
  Since Led is READ_WRITE variable, onLedChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLedChange()  {
  // Add your code here to act upon Led change
  if(led) {
    digitalWrite(ledpin, HIGH);
  }
  else {
    digitalWrite(ledpin, LOW);
  }
  
}

The onLedChange() works fine, I can turn on/off my LED through my Dashboard.

The GPIO 13 is not defect because if I upload the sketch below, with my Arduino IDE 1.8 the output is a value from 0 to 4095 (12 bit ADC) depending on how I turn the potentiometer.

int pot;
int pinPot = 13;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);
}

void loop() {
  pot = analogRead(pinPot);
  Serial.print("pot = ");
  Serial.println(pot);
  delay(500);
}

If you look at the first code in the Iot Cloud in the loop(). Now I give "pot" the value of 50 instead of analogRead(pot); and the output of the Serial Monitor is 50, I can also see that in my Dashboard.
I have also tried that with random(100) and it also works.

void loop() {
  ArduinoCloud.update();
  // Your code here
  pot = 50;
  Serial.println(pot);
  delay(500);

}

I think there is a problem with analogRead() in the Iot Cloud or my code is somewhere wrong.
Can someone please explain why I am getting "0" as the output with analogRead() in the Iot Cloud?

No upload Fritzing sketch?

I guess Cloud about code have something the matter. It is recommended to review the Cloud sample documentation.

My sketch is uploading fine (no freezing).

This article states you can’t use ADC2 when WiFi is in use. So perhaps try using one of the ADC1 gpios instead eg 34 as in the example in the article.

2 Likes

Thank you very much, now it works.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.