Why does the value (A0) is always 0 when I add IoT Cloud-related code?

this code work fine

"
#include <Arduino_ESP32_OTA.h>
#define AOUT_PIN_MQ2 13  // ESP32's pin GPIO13 connected to AOUT pin of the MQ2 sensor
#define AOUT_PIN_FLAME 12  // ESP32's pin GPIO12 connected to AOUT pin of the flame sensor
#define BUZZER_PIN 5  // ESP32's pin GPIO5 connected to the buzzer
#define LED_RED_PIN 22  // ESP32's pin GPIO22 connected to the red LED
#define LED_GREEN_PIN 23  // ESP32's pin GPIO23 connected to the green LED

// const char ssid[] = "FAM_2.4"; // Wifi name
// const char pass[] = "gGcJca5D";  // Wifi Password

void setup() {
  Serial.begin(115200);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_RED_PIN, OUTPUT);
  pinMode(LED_GREEN_PIN, OUTPUT);

  ledcSetup(0, 5000, 8);
  ledcAttachPin(BUZZER_PIN, 0);

  digitalWrite(LED_RED_PIN, HIGH);
  Serial.println("Warming up sensors...");
  delay(20000);  // 20 seconds warm-up
  digitalWrite(LED_RED_PIN, LOW);
  digitalWrite(LED_GREEN_PIN, HIGH); 

  Serial.println("Connected, Sensors are ready!");
}




void loop() {
  // Read smoke (MQ2) sensor
  int smokeValue = analogRead(AOUT_PIN_MQ2);
  Serial.print("Smoke Value: ");
  Serial.println(smokeValue);

  // Read flame sensor from A0
  int flameValue = analogRead(AOUT_PIN_FLAME);
  Serial.print("Flame Value: ");
  Serial.println(flameValue);

  // Check for smoke and activate buzzer with different tones
  if (smokeValue > 500) {
    tone(BUZZER_PIN, 1000);  // Adjust frequency for smoke detection
    Serial.println("Smoke detected! Buzzer sounding (Smoke tone).");
    digitalWrite(LED_RED_PIN, HIGH);
    digitalWrite(LED_GREEN_PIN, LOW); // Alternate red and green LEDs
    delay(3000);  // Adjust duration for tone to be audible
    noTone(BUZZER_PIN);  // Turn off the buzzer
  }

  // Check for flame and activate buzzer with different tones
if (flameValue < 1000) {
    tone(BUZZER_PIN, 900);  // Adjust frequency for flame detection
    digitalWrite(LED_RED_PIN, HIGH);
    digitalWrite(LED_GREEN_PIN, LOW); // Alternate red and green LEDs
    Serial.println("Flame detected! Buzzer sounding (Flame tone).");
  } else {
    noTone(BUZZER_PIN);
    digitalWrite(LED_RED_PIN, LOW);
    digitalWrite(LED_GREEN_PIN, HIGH);
  }

  delay(1000);  // You can adjust the delay based on your application requirements
}

the reading is what i expect but for the when in the i upload the cloud iot code with the same functions as the code above.

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/40e93057-2b60-4791-99ff-3401699cfe61 

  Arduino IoT Cloud Variables description

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

  float mq2_sensor;
  int flame_sensor;

  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"
#include <Arduino_ESP32_OTA.h>
#include <WiFi.h>
#include <WiFiClient.h>
#define AOUT_PIN_MQ2 13  // ESP32's pin GPIO13 connected to AOUT pin of the MQ2 sensor
#define AOUT_PIN_FLAME 12  // ESP32's pin GPIO12 connected to AOUT pin of the flame sensor
#define BUZZER_PIN 5  // ESP32's pin GPIO5 connected to the buzzer
#define LED_RED_PIN 22  // ESP32's pin GPIO22 connected to the red LED
#define LED_GREEN_PIN 23  // ESP32's pin GPIO23 connected to the green LED
void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin();
  
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_RED_PIN, OUTPUT);
  pinMode(LED_GREEN_PIN, OUTPUT);

  ledcSetup(0, 5000, 8);
  ledcAttachPin(BUZZER_PIN, 0);

  digitalWrite(LED_RED_PIN, HIGH);
  Serial.println("Warming up sensors...");
   // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  digitalWrite(LED_RED_PIN, LOW);
  digitalWrite(LED_GREEN_PIN, HIGH); 

  Serial.println("Connected, Sensors are ready!");
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

 
  /*
     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(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
   // Read smoke (MQ2) sensor
  int smokeValue = analogRead(AOUT_PIN_MQ2);
  Serial.print("Smoke Value: ");
  Serial.println(smokeValue);

  // Read flame sensor from A0
  int flameValue = analogRead(AOUT_PIN_FLAME);
  Serial.print("Flame Value: ");
  Serial.println(flameValue);

  // Check for smoke and activate buzzer with different tones
  if (smokeValue > 500) {
    tone(BUZZER_PIN, 1000);  // Adjust frequency for smoke detection
    Serial.println("Smoke detected! Buzzer sounding (Smoke tone).");
    digitalWrite(LED_RED_PIN, HIGH);
    digitalWrite(LED_GREEN_PIN, LOW); // Alternate red and green LEDs
    delay(3000);  // Adjust duration for tone to be audible
    noTone(BUZZER_PIN);  // Turn off the buzzer
  }

  // Check for flame and activate buzzer with different tones
if (flameValue < 1000) {
    tone(BUZZER_PIN, 900);  // Adjust frequency for flame detection
    digitalWrite(LED_RED_PIN, HIGH);
    digitalWrite(LED_GREEN_PIN, LOW); // Alternate red and green LEDs
    Serial.println("Flame detected! Buzzer sounding (Flame tone).");
  } else {
    noTone(BUZZER_PIN);
    digitalWrite(LED_RED_PIN, LOW);
    digitalWrite(LED_GREEN_PIN, HIGH);
  }

  delay(1000);  // You can adjust the delay based on your application requirements
  // Your code here 
  
  
}




The value is consistently zero, causing the flame and smoke actions to trigger. I know is not wiring because the first code works.

No real idea but ( not used cloud much as I don’t want to pay and don’t like the green/blue colour) anyway ……A couple of things come to mind , in your define line try using A12 instead of just 12.
Naming them starting with AO ( A0?) maybe causing an issue ????

Using same board type on cloud and PC ?
Have a read about cloud variables Here
and follow some examples reading inputs .
I’m not one for using “define” for I/O, I I like to follow the code examples in the IDE using

int flamewhatever = A12;

Always worth a basic sketch just to sort the issue.


int analogPin = A3; // potentiometer wiper (middle terminal) connected to analog pin 3
                    // outside leads to ground and +5V
int val = 0;  // variable to store the value read

void setup() {
  Serial.begin(9600);           //  setup serial
}

void loop() {
  val = analogRead(analogPin);  // read the input pin
  Serial.println(val);          // debug value
}

I just fixed
ESP_ERR_TIMEOUT: ADC2 is in use by Wi-Fi" suggests that there might be a conflict between using ADC (Analog to Digital Converter) and Wi-Fi on my ESP32. The ESP32 has certain limitations when it comes to using ADC and Wi-Fi simultaneously.
I tried using ADC1 for my analog readings instead of ADC2. The error seems to specifically mention ADC2, so avoiding it might help. I change the pin assignments and use ADC1. and it works.

The only problem I have left is that the value my flame sensor is always 4095 and decreasing when there's a flame; it's like an inverted reading.

Yes, ESP32 ADC2 is used by WiFi.

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