ESP32 cannot read YF-s201 sensor

Hello guys, I have a problem when I try to read a YF-s201 sensor on a ESP32 DevBoard, the board cannot read any sensor data. I don’t know if the problem is in the code or if there is a problem in the board, please help, thanks.

#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

#include "math.h"

BlynkTimer timer;

WidgetRTC rtc;

char auth[] = "xxxxxx";


char ssid[] = "xxxxxx";
char pass[] = "xxxxxx";

BLYNK_CONNECTED() {
  rtc.begin();
}

volatile int NumPulsos; 
const int sensorPin= 2;    
float factor_conversion=7.11; 
float volumen=0;
long dt=0; 
long t0=0; 
byte sensorInterrupt = 0; 

void ContarPulsos ()  
{ 
  NumPulsos++; 
} 

int ObtenerFrecuecia() 
{
  int frecuencia;
  NumPulsos = 0;  
  interrupts();  
  delay(1000);  
  noInterrupts(); 
  frecuencia=NumPulsos; 
  return frecuencia;
}

void clockDisplay()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();
}

void sendSensor()
{
  float frecuencia=ObtenerFrecuecia(); 
  float caudal_L_m=frecuencia/factor_conversion; 
  dt=millis()-t0; 
  t0=millis();
  volumen=volumen+(caudal_L_m/60)*(dt/1000); 

  Serial.print ("Caudal: "); 
  Serial.print (caudal_L_m,3); 
  Serial.print ("L/min\tVolumen: "); 
  Serial.print (volumen,3); 
  Serial.println (" L");
  
   Blynk.virtualWrite(V0, caudal_L_m);
   Blynk.virtualWrite(V1, volumen);
}


void setup()
{

  Serial.begin(9600);

  pinMode(sensorPin, INPUT); 

  attachInterrupt(0,ContarPulsos,RISING);
  t0=millis();

  Blynk.begin(auth, ssid, pass);

  setSyncInterval(10 * 60);

  timer.setInterval(1000L, sendSensor);
  timer.setInterval(10000L, clockDisplay);
}

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

What does happen??

What library version, ESP core, Server type (cloud, local), Does the device connect, any Serial monitor output, etc? Details please.

I’m using the most recent Blynk library, the server type is cloud and the device connect well (to the Blynk App), but doesn’t show any sensor data. I don’t know what you mean with the ESP core and Serial monitor output, sorry, I’m new using all this

Well, you clearly managed to setup the ESP32 core, otherwise you wouldn’t have gotten this far :stuck_out_tongue:

The Serial monitor is what all those Serial.print() commands are sending to… it is part of your IDE. You are sending all your clock data to it, are you not seeing that? Why not send that data as well to the App and confirmat least that is working.

As for the sensor, have you tested it without Blynk? Does it even work?

Ok, now I understand. Of course I forget put the Serial monitor in the YF-s201 sensor (I have modified the code to now show all of that in the Serial monitor) but the problem still. And yes, I tested the sensor without blynk, didn’t work, it works just with an Arduino Uno (Using the same code, without the Blynk part)

According to this:
https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

GPIO 0 on the ESP32 is pulled HIGH, so attaching a RISING interrupt to it probably isn’t the best thing to do.

Pete.

1 Like

You name GPIO 2 for your sensor, and correctly set the pin mode… but then use GPIO 0 for your interrupt?? I think you pulled your example from one for an older and different board…

I think you should try this instead, and make sure your sensor is connected to GPIO 2 (or an even better suited pin, one not possibly connected to onboard LED?).

attachInterrupt(digitalPinToInterrupt(2), ContarPulsos,RISING)

3 Likes

Thanks a lot guys, it works!!!