My sensor is writting out 0 when "ArduinoCloud.update();" is in my code

Hi, Im writing code in Arduino editor and using ESP32.
When is disabled the "ArduinoCloud.update();" the ultrasonic sensor is writting out raw value in variable "doba" and working properly (This is what I want). But when the "ArduinoCloud.update();" is enabled the variable "doba" is writting out only "0" all the time.

I have the same isue with soil moisture sensor and i don't know what to do.

Here is my code with Arduino IoT cloud connection.

/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/83004a78-17dc-46f3-b7e3-d36d0fbaeb86

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  float cyklusSVETLA;
  float nadrzPLNOST;
  float vzduchTEPLOTA;
  float vzduchVLHKOST;
  bool techMISTO;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

// Ultrazvukový senzor (vodní nádrž)
#define TRIGPIN 25
#define ECHOPIN 26
int obsahNadrze;
long doba, vzdalenost;
const int prazdny = 34;
const int plny = 5;

#include "thingProperties.h"

void setup()
{
  Serial.begin(9600);
  delay(1500);

  pinMode(VLAHA_A, INPUT);
  pinMode(VLAHA_D, INPUT);

  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
  */

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update(); // !!!!!!!!!!!!!
  senzory();
}

void senzory() {
    digitalWrite(TRIGPIN, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIGPIN, HIGH);
    delayMicroseconds(5);
    digitalWrite(TRIGPIN, LOW);
    doba = pulseIn(ECHOPIN, HIGH);
    vzdalenost = doba / 58.31;
    obsahNadrze = map(vzdalenost, prazdny, plny, 0, 100);
    obsahNadrze = constrain(obsahNadrze, 0, 100);
    nadrzPLNOST = obsahNadrze;
    Serial.println(doba);
}

Do a serial.Print(doba); right here to see the number of microseconds being returned. Doba should be unsigned long.

I changed the data type at "doba" and put the Serial.print(doba); where you said and here is the result:

Connected to Arduino IoT Cloud
0
0
0
0

This is when ArduinoCloud.update(); is in my code.

That is telling you that: "Returns the length of the pulse in microseconds or gives up and returns 0 if no complete pulse was received within the timeout.".

So, there is NO pulse occurring. Time to begin to track down how you have things wired and powered.

I have everything wired properly I'm 100% sure. Like I said earlier everything works when ArduinoCloud.Update(); isn't in my code. I was wondering if could be there some timing problem like the ultrasonic sensor is sending data at the same time as the Arduino update function?

Not possible as the ultrasonic sensor code blocks your program until the echo is received or the timeout waiting for the echo runs out.

So what should be wrong then? :smiley: I also switched the ultrasonic sensor for a new one and it is still the same problem.

This is why. It's not wrong, It's just it works. It blocks your program execution until an echo is returned or the 1 second time-out occurs. Read the documentation on pulseln().

Okay then let's put this code aside.
I have the same problem with soil moisture sensor.
Here is the code:

/*
  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the 
  Thing

  float flower;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

int sensorPin = 25;
int sensorValue;
int procento;

#define VLAHA_A 25
#define VLAHA_D 26
int vlhkostZeminyProcent;
bool digit = 0;

#include "thingProperties.h"

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);
  pinMode(VLAHA_A, INPUT);
  pinMode(VLAHA_D, INPUT);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();    // !!!!!!!!!!!!!!!!!!!!!!!

  sensorValue = analogRead(sensorPin);
  procento = map(sensorValue, 4095, 860, 0, 100);
  Serial.print("Analog Value: ");
  Serial.println(sensorValue);
  Serial.print("Moisture percent: ");
  Serial.print(procento);
  Serial.println("%");
  flower = procento;

}

Same story.

Do all your sensors work with 3.3 volts?

To be honest, I don't know, but I have 5V output on my ESP32, so Im using that.

When I Google for ESP32, I get back it is powered by 3.3 volts, not 5 volts.

So your circuit board has a regulator to ensure all is running on 3.3 volts, even when powered by a higher voltage. All the pins on your board must use 3.3 volts, in and out.

Exactly as you said, but I have one 5V output pin.
The GPIO pins are 3,3V when set to output.
Here is the ESP32 I have:

Sorry for shop not being in English, but I wanted to send the exact model I have.

Also, I checked the sensors and they can work with both voltages 5V or 3,3V.

Ok, then I can offer nothing further. Good luck!

Thank you for your help!