Sensor data not read when run Blynk.begin

Hello, I have a problem with my project. I am totally new in Blynk. I’ve spent hours trying to understand the code for Blynk and so on.

My problem is my sensor reading stuck at “0” when I write “Blynk.begin(auth, ssid, pass);” inside void setup()

and when I wite “Blynk.run” inside the void loop()

However, when I mark “//” these two lines, the sensor reads the data like usual. Need help!

Another details

  1. I’m using ESP32 Dev Module, 38pins
  2. Smartphone: Samsung A32 5G, android 13.
  3. Blynk version (coped from Serial Monitor): v1.2.0 on ESP32
  4. Blynk library: Blynk by Volodymyr Shymanskyy 1.2.0
#define BLYNK_TEMPLATE_NAME "HUHA2"
#define BLYNK_AUTH_TOKEN "psznFo-_IrkwEgGGmUgb1LtTwhoEtJx_"

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

BlynkTimer timer;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "お前はだれ";
char pass[] = "olio26222";

#define Threshold 400
int MQ2pin = 4;
int sensorValue;

void myTimerEvent() {
  sensorValue = analogRead(MQ2pin);
  Blynk.virtualWrite(V0, sensorValue);

  Serial.print("Sensor Value: ");
  Serial.print(sensorValue);

  if (sensorValue > Threshold) {
    Serial.print(" | Smoke detected!");
    Blynk.logEvent("gas_alert","Gas Leakage Detected");
  }
  Serial.println("");
  delay(500);  
  
}

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(MQ2pin, INPUT);
  Serial.println("MQ2 warming up!");

  timer.setInterval(500L, myTimerEvent);
  delay(3000);
}

void loop() {
  Blynk.run();  
  timer.run();
}

The ESP32 has two analog to digital converters (ADCs)
One of these (ADC2) cannot be used at the same time as WIFi.

The GPIO that you’ve chosen to use (GPIO4) …

is attached to ADC2, so can’t be used at same time as other libraries that use WiFi (including Blynk).

The solution is to use a pin that is connected to ADC1….

  • ADC1_CH0 (GPIO 36)
  • ADC1_CH1 (GPIO 37)
  • ADC1_CH2 (GPIO 38)
  • ADC1_CH3 (GPIO 39)
  • ADC1_CH4 (GPIO 32)
  • ADC1_CH5 (GPIO 33)
  • ADC1_CH6 (GPIO 34)
  • ADC1_CH7 (GPIO 35)
  • ADC2_CH0 (GPIO 4)
  • ADC2_CH1 (GPIO 0)
  • ADC2_CH2 (GPIO 2)
  • ADC2_CH3 (GPIO 15)
  • ADC2_CH4 (GPIO 13)
  • ADC2_CH5 (GPIO 12)
  • ADC2_CH6 (GPIO 14)
  • ADC2_CH7 (GPIO 27)
  • ADC2_CH8 (GPIO 25)
  • ADC2_CH9 (GPIO 26)

Pete.

Thank you for remind me that pin cannot be used when connecting to the internet. I absolutely forgot since I did not use it with WIFI.

The code and Blynk works now. Thank you again!

1 Like