Trouble reading sensor values

Hello everyone!
Can someone please help me figure out what i’m doing wrong? What i’m trying to do sounds extremely simple and i don’t understand why it’s not working!
All i want is to read an LDR sensor values and have them written on a virtual pin so that i can display them on a widget in blynk console. I’m using an ESP32 board which reads the values just fine (tested it), but when i try to port the code to read the values into the Blynk code all i get are zeroes!
Code follows:

void setup() {
 pinMode(25,INPUT);
 Serial.begin(115200);
}

void loop(){
Serial.println(analogRead(25));
delay(1000);
}

This is the very simple code i use to read the values and print them on the Serial Monitor. The output is as follows:

As you can see, it works just fine.
However, as soon as i try to use the same code in the Blynk enviroment with the following code


#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_TEMPLATE_NAME "REDACTED"
#define BLYNK_AUTH_TOKEN "REDACTED"

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32_SSL.h>

char ssid[] = "REDACTED";
char pass[] = "REDACTED";

BlynkTimer timer;

void gauge()
{
Serial.println(analogRead(25));
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(1000L,gauge);
  pinMode(25,INPUT);
}

void loop()

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

All i get are zeroes!

I’ve also tried a similar code that mirrors the one found in the Blynk documentation


#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_TEMPLATE_NAME "REDACTED"
#define BLYNK_AUTH_TOKEN "REDACTED"

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32_SSL.h>

char ssid[] = "REDACTED";
char pass[] = "REDACTED";

BlynkTimer timer;

int LDRVal;

void gauge()
{
Serial.println(LDRVal);
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(1000L,gauge);
  pinMode(25,INPUT);
}

void loop()

{
  LDRVal=analogRead(25);
  Blynk.run();
  timer.run();
}

But got the same result. Does anyone see what i’m missing?

I know my blynk code has no function that writes the values to a Virtual Pin. Right now i’m trying to understand why my code isn’t working. Once i do, writing the values to a Virtual Pin should be pretty straightforward.
Thanks!

GPIO2 is connected to ADC2, but ADC2 can’t be used at the same time as WIFi.
Read this link and choose a GPIO pin that’s connected to ADC1…

Pete.

1 Like

Oh, i had no idea! Thanks for the insight, everything works perfectly now, widget included!

1 Like

You should probably study that Random Nerds tutorial about the ESP32 in the link I posted, ot gives a lot of insight into the board you are using.

Pete.

I will be sure to do that!

1 Like