BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

New to Blink and ESP 32 but having a strange problem I can't work out

I’m just running tests with Blynk - I have bluetooth working and can switch LEDs on and off etc

The next step was to read a voltage on GPIO 2 -

The problem I’ve got is that the result of the code below is always 4095 output. It seems that the analogue read always returns 4095

If I comment out the lines Blynk.begin and Blynk.run I get the output I expect.

I obviously don’t understand something - I need the anaolgue pin value in my code for control later but when Blynk is running I don’t appear to be able to get any value other than 4095

#define BLYNK_PRINT Serial

#define BLYNK_USE_DIRECT_CONNECT

#include <BlynkSimpleEsp32_BT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "........riqfV";
WidgetLCD lcd(V1);
const int TPin  = 2;
int TempReading = 0;

void setup()
{
  // Debug console
  Serial.begin(115200);

  Serial.println("Waiting for connections...");

  Blynk.setDeviceName("HotTub");

  Blynk.begin(auth);

 }

void loop()
{
  Blynk.run();
  TempReading = analogRead(TPin);
  Serial.println(TempReading);
  
  delay(500);
}

Adding a pinMode statement go your void setup for your TPin would be a good move, but you also need to sort out your void loop and use a timer to call the code that reads your TPin…

Pete.

Yes I’ll progress to the timer later, I’m still trying to understand how that works.

For now I just want to prove I can get the read working. I’ve looked at setting the pin but this not in any of the examples I’ve found. Can you provide me with a setpin example?

Thanks

You won’t get it working reliably with that void loop.

I’m not sure how you’ve managed to translate “pinMode” into “setpin”, but that’s why you aren’t getting any results.

https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/

Pete.

Ok so I’ve implemented a timer and added pinMode and re-tested. Unfortunately I get the same result

This code always returns 4095.
As previously commenting out blynk.begin and blynk.run produces the correct output.

(this sketch runs on ESP32 with just one wire connected to pin 2 - you can see the voltage change by just touching it)

#define BLYNK_PRINT Serial

#define BLYNK_USE_DIRECT_CONNECT

#include <BlynkSimpleEsp32_BT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "S......XriqfV";

WidgetLCD lcd(V1);
const int TPin  = 2;
int TempReading = 0;
BlynkTimer t;

void setup()
{
  // Debug console
  Serial.begin(115200);

  Serial.println("Waiting for connections...");

  Blynk.setDeviceName("HotTub");

  Blynk.begin(auth);
  
  pinMode(TPin,INPUT);
  
  t.setInterval(1000L, readVoltage);
 }

void readVoltage()
{
  TempReading = analogRead(TPin);
  Serial.println(TempReading);
}

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

@Mslater please edit your post and replace the characters you’ve used at the beginning and end of your code with the correct ones - as you did in your earlier post.

Pete.

I thought I had - 3 dots correct ?

Triple backticks:
```

Pete.

Ah - that’s my browser - done :slight_smile:

I’m not sure why this is happening when Blynk is enabled but not when it isn’t.
However, the solution is probably to use a voltage divider to reduce the voltage to the pin.

GPIO 2 may not be the best pin top use, as it’s usually where the onboard LED is connected.

I assume that your app doesn’t have any widgets attached to D2, and that you’ve selected ESP32 as your device type in the app?

Pete.

I’m.not even running the App at this stage. It appears that enabling blink the Gpio is compromised. I’ve tried all analog pins. They all work without blink enabled.

You need the app, with the BLE widget added to connect to your device!

Pete.

There’s no point in connecting the device unless I can get the inpouts working in code.

Originally this was a much bigger sketch with lots of switches and LCD output. I;ve stripped it right back to solve this problem. To be honest I’d almost finished the sketch and just had some calculations to do for PID switching of the Heaters when I discovered this problem.

It seems that every piece of advice I give is regarded as irrelevant or unnecessary for your project at this stage in the development process, so as you clearly know more about it than me I’ll leave you to work this out for yourself.

Pete.

Peter

You’ve not understood the problem and why it’s so important to get analogue data into the loop.

However I have fixed it.

The problem is in the Blynk Library.

Actually there’s several problems with blynk for what I want to do so I’ve moved back to using a web page for what I need.

Thanks anyway

Ps. Look in the blink library for how it captures gpio. It’s then obvious as to why it won’t work except on one input only pin

Well, its strange that tens of thousands of Blynk users don’t suffer the same issues with the problematic library.

For anyone else reading this in future and looking for a solution then try adding…

#define BLYNK_NO_BUILTIN

at the beginning of your sketch, it disables the built-in analogue and digital pin operations and may be a step in the right direction.

Pete.