BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Blynk.run of BlynkSImpleWifi.h sets analog pin values to 1023

Setup: I’m using an Arduino UNO with a WiFi shield and LCD Crystal Display. As a result I have no Digital Pins left for use and I’m using the Analog pins for both Digital Inputs and Analog Inputs.

Problem: Now that I’m using Blynk I have noticed that my analog sensors are returning a 1023 value as soon as my code has run Blynk.Run for the first time. I’ve now seen the symptom with both a Sunfounder Analog Temperature Sensor and PhotoCell Light Sensor. I’ve tried with different analog pins the symptoms are the same. As long as I’m not running Blynk.run for the first time the sensors return correct values … Again everything works fine as long as I’m not running Blynk.run in my loop or even setup. Apart from this everything works fine… Blynk App works fine both using Virtual Pins and Digital Sensors connected to the Analog Pins addressed as 13+(1…5 depending on analog pin used).

Any ideas?

Maybe you can use the SimpleTimer to do a one-timer run of the initialization of the pins instead of in the setup(). Set pin mode’s there. Wait a couple of seconds before running that timer. I think SimpleTimer can do one-time delayed runs.

Thanks for the suggestion - I’m not sure I’m tracking though, when you’re saying initialisation of the pins can you elaborate a bit on what you mean? FYI - I’m not having any issues with the Digital Sensors (Inputs) or Outputs (e.g. Relay) … I’m not doing any initialisation of analog pins as from what I know you only need to initiliaze digital pins using pinmode. I’m by the way also only reading my analog pins for the first time using a timer after 15 seconds…

(and where I’m saying only need to initialise digital pins I also meant analog pins addressed as digital pins)

I’m not sure what is going wrong here, but can you maybe post your code? That would probably be handy to see things more clearly. You can start your code with three Backticks ` and end it with that aswell. It’ll markup your code pretty neat and tidy.

For debugging purposes I’ve written this simple piece of code … also using the SimpleTimer as per your suggestion. The result is that a value of 1023 is being written … If I comment out the Blynk.run() and leave everything else as is, correct values are read from the analog sensor and written to the serial port

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <BlynkSimpleWifi.h>
#include <SimpleTimer.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "yyyyyyyyy";     // Network SSID (name)
char pass[] = "zzzzzzzzz";     // Network password
int status = WL_IDLE_STATUS;  // Wifi radio's status
IPAddress ip;                 // IP address of the WiFi Shield

float temp;

SimpleTimer timer;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass); // Here your Arduino connects to the Blynk Cloud.
  timer.setInterval(1000L, sendUptime);
}

void sendUptime()
{
   Blynk.virtualWrite(1, temp);
   Serial.print("\nAnalog Sensor: ");
   Serial.println(temp);
}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
  temp = analogRead(A5);
}

I’d try to set the pin as an OUTPUT pin in the setup(), e.g. pinMode(A5, OUTPUT);

Maybe Blynk does something with it when it’s not declared, but I’m not sure about that. Also try to keep the loop() as clean as possible. You can move the read part to the sendUptime() function.

Thanks - I like your thinking and went down the same path earlier with my original code to no avail, i.e. I had tried with pinMode(A5, INPUT) as it’s a sensor so INPUT made more sense … I have nevertheless again tried both with INPUT and OUTPUT in my test code, INPUT made no impact whatsoever, OUTPUT does have an impact but not giving the expected result … i.e. I’m getting false readings 9.0 and 10.0 and the sensor in this particular case a Light sensor is not responding to any changes in light… I’m now also reading the sensor in the setup function, no difference.
Simply commenting out Blynk.run() again and still leads to correct readings… my assumption therefore is also that something isn’t right in the Blynk library and most likely so in the Blynk.run function.

You’d say so. What kind of light sensor is it? I use simple LDR’s with a pull up resistor to measure light and that works like a charm with Blynk. I assume it’s nothing fancy but just gives values between 0 - 1023 back to the analog port?

Blynk tries to change the pinmode, only if you have a widget set up to digital or analog pin. If widget is controlling pin (example: Slider or Button, …) it is set to OUTPUT. If it’s reading pin value (example: Value Display, Gauge, …) it sets to input.

You can always switch to virtual pins if you need a different behaviour :slight_smile:

I’m also using a simple LDR and the problem is not limited to my LDR … but any Analog sensor it seems, well I’ve tried at least one other analog sensor, not an LDR but an analog temperature sensor. So this leads me to conclude it’s not a sensor problem … as again they also both seem to operate fine as long as Blynk.run is not called :frowning:

If you look at my code you will see that I’m using virtual pins only for Blynk … i.e. I’m not reading from or writing to any analog or digital pins directly from Blynk, all comms with Blynk are happening through virtual pins. The problem I’m facing is that soon as I run Blynk.run() it seems to be overriding the values returned from analog sensors within the arduino environment driving the read values of the analog sensors up to 1023 within a few Blynk.run() runs … on a first run I may not immediately get 1023 but already a much higher value than anticipated. My digital sensors are not impacted, the values of my analog sensors are distorted as soon as Blynk.run is run. It feels like Blynk.Run is writing a HIGH (1023) to all analog
pins although there’s absolutely no reason to do so…

What I’m seeing is that everybody reads the analog input as int, not as float. Could it be something like that? See here: https://www.arduino.cc/en/Tutorial/ReadAnalogVoltage

They read the analog port as being int and later on convert it to a float.

you can enable BLYNK_DEBUG to see actual commands blynk sends. Check if you see the pm command (changing pinmodes).

Your persistence to get to the bottom of this is admirable … unfortunately it’s an option I already tried too (to no avail) in my original code. I’m going to give the DEBUG mode a shot …

Thanks - I’ll follow your suggestion. I don’t have access to my kit for the next 3 days but will come back with my findings at the end of this week.

Here’s my latest code with BLYNK_DEBUG now and nothing else attached to my Arduino apart from my WiFi Shield and the one analog sensor :

#define BLYNK_PRINT Serial
#include <BlynkSimpleWifi.h>
#include <SimpleTimer.h>

char auth[] = "1a06fbd9fb65460888ec0af956aadb85";
char ssid[] = "aaaaaaaa";     // Network SSID (name)
char pass[] = "cccccccc";     // Network password

int sensor_output;

SimpleTimer timer_Sensor;
SimpleTimer timer_Blynk;

void setup()
{
  Serial.begin(9600); 
  Blynk.begin(auth, ssid, pass); 
  timer_Sensor.setInterval(1000L, SensorGet);
  timer_Blynk.setInterval(10000L, BlynkTalk);
}

void SensorGet()
{
  sensor_output = analogRead(A5);
  Serial.print("Analog Sensor: ");
  Serial.println(sensor_output);
}

void BlynkTalk()
{
  Serial.println("Blynk.run() now ");
  Blynk.run();
}

void loop()
{
 timer_Sensor.run();
 timer_Blynk.run(); 
}

Below the output to the Serial Console :

[0] Connecting to Misseeuw…
[10001] My IP: 192.168.2.7
[10002] Blynk v0.3.1
Analog Sensor: 747
Analog Sensor: 749
Analog Sensor: 750
Analog Sensor: 750
Analog Sensor: 733
Analog Sensor: 738
Analog Sensor: 736
Analog Sensor: 734
Analog Sensor: 737
Analog Sensor: 735
Blynk.run() now
[20003] Connecting to cloud.blynk.cc:8442
[21123] <msg 2,1,32
<1a06fbd9fb65460888ec0af956aadb85
�nalog Sensor: 747
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
Blynk.run() now
[30004] >msg 0,1,200
[30005] Ready (ping: 8880ms).
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023
Blynk.run() now
�nalog Sensor: 1023
�nalog Sensor: 1023
�nalog Sensor: 1023

What’s also interesting to see is that the A of Analog Sensor is no longer written correctly once Blynk.run has run …

:frowning: looks like hardware issue… could it be?

I guess it’s hard to exclude the option until we’re able to solve it software wise. However what I can say is that I’ve run pretty comprehensive code on the UNO and WiFiShield with lots of sensors … and as long as I’m not using Blynk or Blynk.run to be specific everything is working fine …

One thing that has spotted my attention is the following warning when I compile for the first time after a reboot. Could there be any link between these warnings and my problem ? When I’m not using Blynk I’m using the “normal” WiFi library by Arduino … With Blynk I’m using the BlynkWiFi library instead …

Arduino: 1.6.6 (Mac OS X), Board:“Arduino/Genuino Uno”

/Applications/Arduino.app/Contents/Java/libraries/WiFi/src/utility/wifi_drv.cpp: In static member function ‘static uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t)’:
/Applications/Arduino.app/Contents/Java/libraries/WiFi/src/utility/wifi_drv.cpp:451:10: warning: converting to non-pointer type ‘uint8_t {aka unsigned char}’ from NULL [-Wconversion-null]
return NULL;
^
/Applications/Arduino.app/Contents/Java/libraries/WiFi/src/utility/wifi_drv.cpp: In static member function ‘static int32_t WiFiDrv::getRSSINetoworks(uint8_t)’:
/Applications/Arduino.app/Contents/Java/libraries/WiFi/src/utility/wifi_drv.cpp:476:10: warning: converting to non-pointer type ‘int32_t {aka long int}’ from NULL [-Wconversion-null]
return NULL;
^