analogRead stops functioning with Blynk

I am trying to read analog values coming off of an accelerometer. It works fine without Blynk.begin, but but as soon as I add that line of code (which is required to run blynk), the analog values always read as high (4095).

Not working:

> #include <BlynkSimpleEsp32_BT.h>
> #define BLYNK_PRINT Serial
> #define BLYNK_USE_DIRECT_CONNECT
> char auth[] = "b5da95f86c24424ba84163934cd9527a";
> #define accelPin 0
> 
> void setup() {
>   Serial.begin(9600);
>   Blynk.setDeviceName("ESP_Current");
>   Blynk.begin(auth);
> }
> 
> void loop() {
>   Serial.println(analogRead(accelPin));
> 
> //  Blynk.run();
> }

Working (of course, Blynk doesn’t work now since Blynk.begin is commented out):

> #include <BlynkSimpleEsp32_BT.h>
> #define BLYNK_PRINT Serial
> #define BLYNK_USE_DIRECT_CONNECT
> char auth[] = "b5da95f86c24424ba84163934cd9527a";
> #define accelPin 0
> 
> void setup() {
>   Serial.begin(9600);
>   Blynk.setDeviceName("ESP_Current");
>   //Blynk.begin(auth);
> }
> 
> void loop() {
>   Serial.println(analogRead(accelPin));
> 
> //  Blynk.run();
> }

I am running this on an ESP32.

Keep the void loop() as clean as possible. Use a Blynk timer and put that serial.print command in it.
Read http://docs.blynk.cc/#blynk-firmware-blynktimer

Here is my updated code:

#include <BlynkSimpleEsp32_BT.h>
#define BLYNK_PRINT Serial
#define BLYNK_USE_DIRECT_CONNECT
char auth[] = "b5da95f86c24424ba84163934cd9527a";
#define accelPin 0

BlynkTimer timer;

void myTimerEvent() {
  Serial.println(analogRead(accelPin));
}

void setup() {
  Serial.begin(9600);
  Blynk.setDeviceName("ESP_Current");
  Blynk.begin(auth);
  timer.setInterval(1000L, myTimerEvent);
}

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

Still no luck

This is probably treated as GPIO 0. Try this instead…

#define accelPin A0

You’re not defining the pinMode for your accelPin.
I realise that it works without Blynk, but pinMode is always a good idea.

Pete.

Setting the pin to A0 results in the ESP returning 0. I thought that A0 was only used for Arduino boards since the analog pins on their are physically different from the digital counterparts (A0 and 0 are physically two different pins). Either way, unfortunately didn’t work.

I added in the pinMode, but no luck either. :frowning:

SOMETHING ELSE WORTH NOTING:

I also tried to read the sensor on the ESP’s second core. Same thing happened - returns a 4095 when blynk is implemented, works fine without it

Okay did a bit more testing.

When I use a simple voltage divider in place of the sensor (accelerometer), it seems to work with blynk there. Here is what is interesting though:

Without blynk, it reads values around 320. With blynk, it reads around 340. Nothing in the setup changed. The accelerometer values are usually pretty high (usually above 3800). Looking at the pattern from the voltage divider, maybe the ESP is artificially increasing the reading, and saturating it to 4095 in the process.

Thoughts?

Actually A0 is also for ESP8266… but I missed the fact that you are using ESP32 :blush:

All else fails… Google it :stuck_out_tongue:

OK, not Blynk per se that is causing the issue with pin 0, but the use of WiFi…

I’m using bluetooth not wifi - I don’t think that would end up being an issue (from the documentation I have read from espressif, it seems like only wifi would cause an issue).

Although…

So when I tested the voltage divider circuit, I had done it on an ADC1 pin (I have a PCB made, so its difficult for me to go in and mess with things, and only certain pins are accessible). I suppose this might be the culprit? Will test what I can tomorrow and update.

Based on what Googling I did… one example here -

The ADC2 hardware is also used by the radio subsystem. So, either WiFI or Bluetooth could make the ADC2 converter unavailable.

The fact that it works until you get Blynk running proves something that Blynk actively does can cause the issue… and wireless communication is a big active factor with Blynk.

1 Like