Ultrasonic SR04 not reading values

Hi
I have created an alarm project but cant get ultrasonic sensor SR04 to read measurments.
If I put it without Blynk code, the sensors reads values correclty but when I put Blynk.begin(auth); into the setup the measurments show 0.

I searched the formus but couldn find any similar problems. I belive the problem is with the delay of the Echo HIGH.

Could somebody please help with checking the code?

Thanks a lot!!

#define BLYNK_PRINT Serial // Defines the object that is used for printing

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <WidgetRTC.h>

// You should get Auth Token in the Blynk App.
char auth[] = "";

#define W5100_CS  10
#define SDCARD_CS 4

// Set your LED and physical button pins here
const int relayPin = 8;
//Support for Blynk timer
BlynkTimer timer;
//Support for Blynk real time clock
WidgetRTC rtc;
// Attach virtual serial terminal to Virtual Pin V1
WidgetTerminal terminal(V0);

int trigPin = 9;    // Trigger
int echoPin = 10;    // Echo
int messagesent= 0;
int relayState = 1;
int val;
long duration, distance;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V1);
  // Begin synchronizing time
  rtc.begin();
}

void clockDisplay()
{
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(year()) + "/" + month() + "/" + day();
  terminal.print(currentDate);
  terminal.print(" ");
  terminal.print(currentTime);
  terminal.print(" ");
}


void ultra()
{
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);

  // Convert the time into a distance
  distance= duration*0.034/2;// Divide by 29.1 or multiply by 0.0343

  Serial.println(duration);
 Serial.println(distance);
}

void AlarmOFF()
{
  digitalWrite(relayPin, HIGH);
  Blynk.virtualWrite(V1, 1);

}

// When App button is pushed - switch the state
BLYNK_WRITE(V1) {
  relayState = param.asInt();
  digitalWrite(relayPin, relayState);
  if (relayState==0) {
  timer.setTimeout(60000, AlarmOFF);

  }
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  //set ULTRASONIC
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //set relay
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, relayState);

  Blynk.begin(auth);
  delay(5);


  // Setup a function to be called every 100 ms
  timer.setInterval (5000, ultra);
}

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

}

Put three backticks at the beginning and end of your code.
Blynk%20-%20FTFC

It seems that there is an actual problem between Ultrasonic sensor readings and the blynk library.
Works great without blynk but when adding blynk.begin(auth) the output is 0 - not sure what is the reason.

Да вроде все ОТЛИЧНО работает ! Здесь есть пример

I don’t have this issue with my SR04, I am using Blynk.config(auth, server_ip, port);

I just cant understand, tried everything and no solution. image
I use ethernet shield, that is the only difference. And id doesnt work.

But when I remove Blynk.begin(auth);

image

Without being connected to the internet…

Can you spot the issue here?

Pete.

2 Likes

Thanks a lot, wasnt aware of this. Now is working perfectly!

2 Likes