Widget Virtual Pin vs Digital Pins, question

Hello folks,

I was wondering how to use the digital pins (D0 or A0, etc) on the widgets. I have no problem using the virtual pins, (V0, V1, etc)but how about the digital pins? Any example available ? I asked many times to Mr. Google, but not luck.

Basically, I dont have any code to post, but I would love to know the difference between the two types of pins.

Thanks in advanced.

Put simply virtual pins only exist in software (specifically within blynk server environment), physical pins like d1, a0 refer to actual PHYSICAL pins on the hardware board you happen to be using. Hope that helps.

Another way to think about it is that the Digital and Analog pin options in the widgets are a very simple way to control or read pins directly on your hardware, I.e. Turn digital pins HIGH or LOW or read the 1 & 0 data from them, as well as read the range values from the Analog pins, just by using widgets in the app without any extra coding (aside from the basic setup sketch).

.

mars, Gunner,

Thank you guys for your replies. I think I understand now the difference between the two pins. Here is a motor control speed code that I am working on it, I trimmed a little bit to post it.
This code is working fine. Using a potentiometer, I am able to control the motor speed. D0 is attached to a MosFET. By the way, I am using the Photon, very nice little thing. Any way, my goal is to control the motor speed using my Iphone using the Widget Slider.

Here is my problem now, the slider do not have Analog pin selection, only Digital and Virtual pins.

// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"

/*
Motor Speed Control 
Jan 21, 2017
*/

int potPin      = A0;
int motorPin    = D0;
int potValue    = 0;
int motorValue  = 0;
int motorValue2 = 0;

char auth[] = "xxyyzz";


/* ---------------- VOID SETUP ---------------------------- */

void setup() {
 Serial.begin(9600);
 Blynk.begin(auth);
 pinMode(motorPin, OUTPUT);
 pinMode(potPin, INPUT);

}

/* ----------------- VOID LOOP ---------------------*/

 void loop() 
 {
 Blynk.run();
  potValue = analogRead(potPin);  
  // The Photon analogRead values go from 0 to 4095. The Photon analogRead is 12 bits resolution, so 2^12 = 4095
  //analogWrite values are from 0  to  255, 255  represents the MAX voltage input, in this case: +3.2V
  // The next two entries basically acomplish the same result. Use one or the other
   motorValue = map(potValue, 0, 4096, 0, 255); 
  //  motorValue2 = potValue/16; 
 analogWrite(motorPin, motorValue);
 Serial.print("Motor Value "); Serial.println(motorValue);
 delay(1000);
 }

That is because the slider is an output widget, and Analog pins are typically input from the app’s point of view, thus are available on gauge and level widgets.

So a slider will control the value of a PWM port (such as your motor control), but not read the input of an analog port

Thank you, Gunner. I need to think more about it. Its almost 1AM here. Here is a pic of the prototype.

The use case for direct pin control and reading are for very simple tasks.

You had mentioned that you already understood the use of virtual pins?.. That is where the magic happens!

With the virtual pins you could control your motor speed from either a slider widget…

BLYNK_WRITE(Vx) // Slider Widget Function - runs every-time slider widget is physically moved.
{
  speed = param.asInt();  // Get slider value.
  analogWrite(MotorPin, speed);  // Send slider value to motor
}

…or physical POT, while also reflecting it’s value back on the slider:

Blynk.virtualWrite(Vx, speed);  // Send feedback to Slider Widget

You would need some form of logic code determining the source of the speed control, otherwise the physical Pot will always override the slider.

But sorry, you can’t get the app to turn the pot for you to correspond when using the slider… not without another motor (or servo), tied into the pot lever… hmmm :wink:

Thank you, Gunner. I got it working! Yahoo!. I followed your tips, yes it works. Here is the updated code. The way it is right now, the pot is not working, but I think I can figure out how to correct that. I never had any problem setting up the pot, my problem was controlling the motor with the slider. As a matter of fact, the entire pot code has been commented out. Blynk is awesome! With Blynk you can eliminate expensive hard-to-get hardware.

I moved these two lines to void loop, and I declared “speed” as a global variable.

Blynk.virtualWrite(V21, speed);  // Check Slider value 
analogWrite(motorPin, speed);     // <== Blynk controlling the motor speed. Yahoo!

I know the code can be improved now that is working. Once again, Thanks a lot!

// This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"

/*
Motor Speed Control 
Jan 21, 2017
*/

int potPin      = A0;
int motorPin    = D0;
int potValue    = 0;
int motorValue  = 0;
int motorValue2 = 0;
int speed      =  0;

char auth[] = "xxxxzzzzyyyy";


//--------------Read Slider, store value in the global variable "speed"  ----------------

BLYNK_WRITE(V20) // Virtual pin value set to 255
{
  speed = param.asInt(); 

}


/* ---------------- SETUP ---------------------------- */

void setup() {
 Serial.begin(9600);
 Blynk.begin(auth);
 pinMode(motorPin, OUTPUT);
 pinMode(potPin, INPUT);

}

/* ----------------- VOID LOOP ---------------------*/

 void loop() 
 {
 Blynk.run();
 
/*
 potValue = analogRead(potPin);  
  // The Photon analogRead values go from 0 to 4095. The Photon analogRead is 12 bits resolution, so 2^12 = 4095
  //analogWrite values are from 0  to  255, 255  represents the MAX voltage input, in this case: +3.2V
  // The next two entries basically acomplish the same result. Use one or the other
   motorValue = map(potValue, 0, 4096, 0, 255); 
  //  motorValue2 = potValue/16; 
 analogWrite(motorPin, motorValue);
 Serial.print("Motor Value "); Serial.println(motorValue);
 
*/
 
 Blynk.virtualWrite(V21, speed);  // Just Checking Slider Values
 analogWrite(motorPin, speed);     // <== Blynk Slider controlling the motor speed. Yahoo!  
 delay(1000);
 }

The whole thing is very interesting to me. I had trouble grasping the following: D0 declared as an OUTPUT it does not care how it gets its value from 0 to 255 range. It can be real such is using the pot and A0 as an INPUT, or virtual such is the case that is setup now. This is like vudú, man! Magic! Am scare now :grinning:

hello @Gunner

I was wondering, how the way to push data from the hardware that are using analog pin to widget in blynk. any example available for this problem? the widget that i am using is the “value display”. i would love to know how to do it…
The hardware that I am using is the CT sensor
So below is my code

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

> #include "EmonLib.h"
> // Include Emon Library
> EnergyMonitor emon1;
> // Create an instance

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

> // Your WiFi credentials.
> // Set password to "" for open networks.
> char ssid[] = "Aizat";
> char pass[] = "aizat987";

> SimpleTimer timer;

> void setup()
> {
>   Serial.begin(9600);
>   Blynk.begin(auth, ssid, pass);

>   timer.setInterval(1000L, getSendData);
>   
>   emon1.current(0, 50.1);             // Current: input pin, calibration.
> }

> void loop()
> {
>   Blynk.run();
>   timer.run(); // Initiates SimpleTimer
> }

> void getSendData()
> {
>   double Irms_1st = emon1.calcIrms(1480);  // Calculate Irms only
>   Serial.println(Irms_1st);             // Irms
>   // You can send any value at any time.
>   analogWrite(0,Irms_1st); Blynk.run();
> }

@Aizat_Nazih, If I am understanding you correctly all you would need is to add the “value Display” widget to your project, and select Analog Pin 0. Set reading rate to 1 sec (same as your timer).

To have it PUSH the data, I would add a Blynk.virtualWrite(V1, Irms_1st); to your getSendData function, after the analogWrite(0,Irms_1st);. With this case, select Virtial Pin 1 in the “value display” widget, and select the reading rate to PUSH.

Additionally, you should remove your AUTH Token and SSID and Password before posting your code.

Also, try searching virtual pins and PUSH data for more information and examples. Try not to revive old post that do not relate to your question, PUSHing data is not the topic of this post.

1 Like

Thank you for your reply @Toro_Blanco and sorry for revive this old post

A post was merged into an existing topic: Correct use of BLYNK_WRITE