Blynk measuring RPM with tachometer

hello Blynker, i make prject blynk to read RPM with Tachometer optocopler U, in serial monitor value sensor is shown and accuarcy, i add blynk code for display RPM value oon blynk app, but the Value is not Shown in Virtual Pin on Value Display, please help me, here’s my code i’m attcah

>      #define BLYNK_USE_DIRECT_CONNECT
> 
>     // You could use a spare Hardware Serial on boards that have it (like Mega)
>     #include <SoftwareSerial.h>
>     SoftwareSerial DebugSerial(2, 3); // RX, TX
> 
>     #define BLYNK_PRINT DebugSerial
>     #include <BlynkSimpleSerialBLE.h>
> 
>     BlynkTimer timer;
> 
> 
>     // You should get Auth Token in the Blynk App.
>     // Go to the Project Settings (nut icon).
>     char auth[] = "YourAuthToken";
> 
>     int val;
>     long last=0;
>     int stat=LOW;
>     int stat2;
>     int contar=0;
> 
>     int sens=75;  // this value indicates the limit reading between dark and light,
>                   // it has to be tested as it may change acording on the 
>                   // distance the leds are placed.
>     int nPalas=2; // the number of blades of the propeller
> 
>     int milisegundos=500; // the time it takes each reading
> 
> 
>     void sendSensor()
>     {
>       val=analogRead(0);
>       if(val<sens)
>         stat=LOW;
>        else
>         stat=HIGH;
>        digitalWrite(13,stat); //as iR light is invisible for us, the led on pin 13 
>                               //indicate the state of the circuit.
> 
>        if(stat2!=stat){  //counts when the state change, thats from (dark to light) or 
>                          //from (light to dark), remmember that IR light is invisible for us.
>          contar++;
>          stat2=stat;
>        }
>        if(millis()-last>=milisegundos){
>          double rpm=((double)contar/nPalas)/2.0*60000.0/(milisegundos);
>          Serial.print(" RPM");Serial.print(rpm);
>          Blynk.virtualWrite(V5, rpm);
>          contar=0;
>          last=millis();
>        }
>     }
> 
>     void setup()
>     {
>       Serial.begin(9600);
>       pinMode(13,OUTPUT);
>       DebugSerial.begin(9600);
> 
>       DebugSerial.println("Waiting for connections...");
> 
>       // Blynk will work through Serial
>       // 9600 is for HC-06. For HC-05 default speed is 38400
>       // Do not read or write this serial manually in your sketch
>       Serial.begin(9600);
>       Blynk.begin(Serial, auth);
>       timer.setInterval(1000L, sendSensor);
>       
>     }
> 
>     void loop()
>     {
>       Blynk.run();
>     timer.run();
> 
>     }

Does the serial print value show correctly? If not, then it is possibly an issue with your code, not with Blynk.

Yes the serial print is correct, In serial monitor, RPM value is shown, but in blynk the ROM value is not shown

What, if anything IS shown? How have you setup that widget? Should be PUSH

Wait!! You can not use the same Serial port for debug and the BT/BLE Blynk connection… you are probably disconnecting after each Serial.print()

1 Like

For precise time interval counting you should use interrupts - I don’t see them in code.

Sorry, the serial monitor for I test the value sensor, if I use to

i just delete the serial.monitor, but the value still zero on blynk app, please help

your code looks ok, so that means something else is the problem. Perhaps the auth token is incorrect?
What you can also try is a basic test example, e.g.: https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=GettingStarted%2FVirtualPinWrite to check whether the blynk connection to the app is working at all. This at least eliminates other factors.

my connection is correect, for test i make slider button for control speed motor wth pwm pin 9, speed can control but the sensor RPM value still zero

We seem to be missing a few curly brackets from this if statement.

Pete.

but RPM sensor works, i get these example code for read tachometer RPM from here https://playground.arduino.cc/Learning/Tachometer the code can work perfect and verry accuracy if display in serial monitor

I think you should post your latest code, and what you see in the serial monitor.

Pete.

this is really shooting in the dark and I would really suggest to do both what I said (test a simple routine) and pete (latest code).

Another shot:

have you tried:
Blynk.virtualWrite(V5, String(rpm));

i has try, but still zero value in blynk app

I’m sorry guys, correct me if I’m wrong, but this code will probably not gonna work with Blynk. Reason? The loop() which in this code is supposed to repeatedly run very fast (as it will miss a lot of state changes otherwise).
This the fragment, I’m talking about:

val=analogRead(0);
  if(val<sens)
    stat=LOW;
   else
    stat=HIGH;
   digitalWrite(13,stat); //as iR light is invisible for us, the led on pin 13 
                          //indicate the state of the circuit.

   if(stat2!=stat){  //counts when the state change, thats from (dark to light) or 
                     //from (light to dark), remmember that IR light is invisible for us.
     contar++;
     stat2=stat;
   }

Then the situation is even worse, as the above (aftr adaptation for Blynk) is called with timer only ONCE per second. So how it can count pulses which suppose to happen even hundreds times per second??
That code will not work IMHO… But, maybe I’m wrong…

here’s my complete code, if i’m not use blynk code, the value can shown on serial monitor, i modified it for blynk app, but value can’t shown on blynk app.

  #define BLYNK_USE_DIRECT_CONNECT

 // You could use a spare Hardware Serial on boards that have it (like Mega)
 #include <SoftwareSerial.h>
 SoftwareSerial DebugSerial(2, 3); // RX, TX

 #define BLYNK_PRINT DebugSerial
 #include <BlynkSimpleSerialBLE.h>

 BlynkTimer timer;


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

 int val;
 long last=0;
 int stat=LOW;
 int stat2;
 int contar=0;

 int sens=75;  // this value indicates the limit reading between dark and light,
               // it has to be tested as it may change acording on the 
               // distance the leds are placed.
 int nPalas=2; // the number of blades of the propeller

 int milisegundos=500; // the time it takes each reading


 void sendSensor()
 {
   val=analogRead(A0);
   if(val<sens)
     stat=LOW;
    else
     stat=HIGH;
    digitalWrite(13,stat); //as iR light is invisible for us, the led on pin 13 
                           //indicate the state of the circuit.

    if(stat2!=stat){  //counts when the state change, thats from (dark to light) or 
                      //from (light to dark), remmember that IR light is invisible for us.
      contar++;
     stat2=stat;
    }
    if(millis()-last>=milisegundos){
      double rpm=((double)contar/nPalas)/2.0*60000.0/(milisegundos);
      //Serial.print(" RPM");Serial.print(rpm);
      Blynk.virtualWrite(V5, rpm);
      contar=0;
      last=millis();
      
    }
    
 }

 void setup()
 {
   Serial.begin(9600);
   pinMode(13,OUTPUT);
   DebugSerial.begin(9600);

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

   // Blynk will work through Serial
   // 9600 is for HC-06. For HC-05 default speed is 38400
   // Do not read or write this serial manually in your sketch
   Serial.begin(9600);
   Blynk.begin(Serial, auth);
   timer.setInterval(1000L, sendSensor);
   
 }

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

 }

thank’s for help me, would you give me example, i need blynk to measure RPM with tachomter optocoupler, i get the original code from this site https://playground.arduino.cc/Learning/Tachometer and imodified to dicplay on blynk. please help

You are using the wrong tools for the job… Blynk is an IoT application, not a simple display. It requires its own communication channel that will get disrupted by a real time data stream as you require. Compromises will need to be made by putting your sensor reading into a timed loop, even 10 times a second may interfere, but much less than thousands of times a second.

I pointed you earlier - find a code with interrupts- those should work. I do have some, but currently I’m too busy to dig into it. Perhaps this evening (CET)

I’m very confused about the hardware you’re using.

What board are you using ?
I assume you’re using an Android phone/tablet?
Do you have a BLE widget in your sketch?
Does your device show as being online in Blynk?
What connections do you have to your board?

Pete.