Esp32 Blynk analog read, UV sensor

Hi,
im Having trouble to read my uv sensor value with blynk code. Im using the Guva s12sd with esp 32, it an analog sensor. The problem is that it always showing 0 on the serial monitor.


#define BLYNK_TEMPLATE_ID "T***K"
#define BLYNK_TEMPLATE_NAME "lynk"
#define BLYNK_AUTH_TOKEN "W***I"
#define BLYNK_PRINT Serial

#include <BlynkSimpleEsp32.h>
#include <WiFi.h>

char auth[]= BLYNK_AUTH_TOKEN;
const char ssid[] = "****";
const char password[] = "****";


const int SENSOR_PIN = 2;
//#define SENSOR_PIN 2
BlynkTimer timer;


void setup() {
  Serial.begin(115200);
  while (!Serial);
  
  timer.setInterval(1000L, sendAnalog);
  pinMode(SENSOR_PIN, INPUT);


 

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  Blynk.begin(auth, ssid, password);
}

int uvindex=0;
float sensorValue;
float sensorVoltage;

void sendAnalog()
{
 
 sensorValue = analogRead(SENSOR_PIN);
 sensorVoltage = sensorValue/4095*3.3;

  if      (sensorVoltage < 0.05) uvindex=0;
  else if (sensorVoltage > 0.05 && sensorVoltage <= 0.227) uvindex = 1;
  else if (sensorVoltage > 0.227 && sensorVoltage <= 0.318) uvindex = 2;
  else if (sensorVoltage > 0.318 && sensorVoltage <= 0.408) uvindex = 3;
  else if (sensorVoltage > 0.408 && sensorVoltage <= 0.503) uvindex = 4;
  else if (sensorVoltage > 0.503 && sensorVoltage <= 0.606) uvindex = 5;
  else if (sensorVoltage > 0.606 && sensorVoltage <= 0.696) uvindex = 6;
  else if (sensorVoltage > 0.696 && sensorVoltage <= 0.795) uvindex = 7;
  else if (sensorVoltage > 0.795 && sensorVoltage <= 0.881) uvindex = 8;
  else if (sensorVoltage > 0.881 && sensorVoltage <= 0.976) uvindex = 9;
  else if (sensorVoltage > 0.976 && sensorVoltage <= 1.079) uvindex = 10;
  else if (sensorVoltage > 1.079 && sensorVoltage <= 1.170) uvindex = 11;
  else uvindex = 0;  //reading the sensor on A0
  Blynk.virtualWrite(V4, uvindex); //sending to Blynk

  Serial.print("sensor reading = ");
  Serial.print(sensorValue);
  Serial.println("");
  Serial.print("sensor voltage = ");
  Serial.print(sensorVoltage);
  Serial.println(" V");
  Serial.print("uvindex= ");
  Serial.print(uvindex);
  Serial.println();
  Serial.println();
  delay(1000);
}



void loop(){
   

  Blynk.run();
  timer.run();
}

i tested th sensor alone and works fine with this code bellow :

const int SENSOR_PIN = 2;


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

void loop() {
  float sensorVoltage; 
  float sensorValue;
  int uvindex;
  sensorValue = analogRead(SENSOR_PIN);
  sensorVoltage = sensorValue/4095*3.3;

  if      (sensorVoltage < 0.05) uvindex=0;
  else if (sensorVoltage > 0.05 && sensorVoltage <= 0.227) uvindex = 1;
  else if (sensorVoltage > 0.227 && sensorVoltage <= 0.318) uvindex = 2;
  else if (sensorVoltage > 0.318 && sensorVoltage <= 0.408) uvindex = 3;
  else if (sensorVoltage > 0.408 && sensorVoltage <= 0.503) uvindex = 4;
  else if (sensorVoltage > 0.503 && sensorVoltage <= 0.606) uvindex = 5;
  else if (sensorVoltage > 0.606 && sensorVoltage <= 0.696) uvindex = 6;
  else if (sensorVoltage > 0.696 && sensorVoltage <= 0.795) uvindex = 7;
  else if (sensorVoltage > 0.795 && sensorVoltage <= 0.881) uvindex = 8;
  else if (sensorVoltage > 0.881 && sensorVoltage <= 0.976) uvindex = 9;
  else if (sensorVoltage > 0.976 && sensorVoltage <= 1.079) uvindex = 10;
  else if (sensorVoltage > 1.079 && sensorVoltage <= 1.170) uvindex = 11;
  else uvindex = 0;  
  

  Serial.print("sensor reading = ");
  Serial.print(sensorValue);
  Serial.println("");
  Serial.print("sensor voltage = ");
  Serial.print(sensorVoltage);
  Serial.println(" V");
  Serial.print("uvindex= ");
  Serial.print(uvindex);
  Serial.println();
  delay(1000);
}

Can anyone tell me where is the issue?

The ESP32 has two analogue to digital converters (ADCs) these are known as ADC1 and ADC2.

Pin GPIO2 is connected to ADC2

If you read this…

And especially this part…

you’ll realise that you need to use an analog pin connected to ADC1 if you want it to work at the same time as Blynk.

Pete.

Thank you so much, i used pin34 which uses ADC1, i didnt notice that ADC2 cant be used with wifi. But why is that? is it caused by a chip restriction ?

Yes, it’s a limitation of the ESP32 architecture.

Pete.