Ultrasonic sensor getting different measures when plugged in only with battery

I have an Arduino MKR Wifi 1010, with a HC-SR04 ultrasonic sensor plugged in
What I want to do is get some measures and send them in a post request using the wifi network
Everything works fine if I use the USB port to power the Arduino, if I plug in the battery (LiPo, 3.7V) the sensor will get just 0 measure all the time, any idea why? Is it because the sensor should work at 5V?

#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h>

#include "arduino_secrets.h"

#define TRIG_PIN 6  // TRIG pin
#define ECHO_PIN 7  // ECHO pin
#define SAMPLES 7

char ssid[] = SECRET_SSID;          // your network SSID (name)
char pass[] = SECRET_PASS;          // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;        // the Wifi radio's status
float ultraSonicMeasures[SAMPLES];  // array to store data samples from sensor
float distance;                     // store the distance from sensor
int baskets = 0;

IPAddress server(192,168,1,110);
WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true)
      ;
  }

  String fv = WiFi.firmwareVersion();

  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(3000);
  }

  Serial.println("Connected to wifi");

  printWifiStatus();

  // configure the trigger and echo pins to output mode
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  Serial.println("Getting ultrasonic measures");
  for (int sample = 0; sample < SAMPLES; sample++) {
    ultraSonicMeasures[sample] = ultrasonicMeasure();
    delay(10);  // to avoid untrasonic interfering
  }
  Serial.println("Sending measures");
  postData(ultraSonicMeasures);
}

float ultrasonicMeasure() {
  // generate 10-microsecond pulse to TRIG pin
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // measure duration of pulse from ECHO pin
  float duration_us = pulseIn(ECHO_PIN, HIGH);

  // calculate the distance, 0.017 -> sound speed/2
  float distance_cm = 0.017 * duration_us;

  return distance_cm;
}

That is the same as every other device. If a working voltage is specified and you decide to use a much different voltage, then that device will probably not work very well. Otherwise there is no reason to have specified voltage, is there?

1 Like

Well, I didn't connect the battery directly to the sensor, there is the Arduino board in between, and I would expect that the board can increase the tension to 5V since I don't think the sensor needs a lot of amperes to work

Ah! Some one believes in magic! Lots of magic in microprocessors, but it is ALL in software!

1 Like

Does the board documentation state that?

If you try something and it doesn't work, check your assumptions.

1 Like

With different power sources you are powering the sensor with different voltages. The expected results will be different readings as your reference is different. It appears your system is working properly as it is currently designed, but not as intended. Correct the design and you will get consistent results.

The HC-SR4 is a 5V device:
HC-SR04
"I would expect that the board can increase the tension to 5V".

What happens when you connect 5V from the HCs echo output to the MKR Wifi 1010's 3.3V input pin?

"I would expect that the board can increase the tension to 5V". HOW?

What happens when you connect 5V from the HCs echo output to the MKR Wifi 1010's 3.3V input pin? Have some spares as you will probably fry the 3V3 part.

At this point I highly recommend you watch some tutorials on basic electronics. Also there is a good book called electronics for dummies, read it, that will help you understand what you are trying to do.

I see everyone here is very friendly and especially helpful!!
Ok is about the voltage, so how should I power the board to have it portable?
Also, why have a 3.7v battery in the circuit and a 5v pin and you can't just use it? Why not have a 5v battery support?

There are variants of the HC-SR04 that will work at 3.3 V, usually with the designation HC-SR04p. There are significant variations in the circuitry of generic HC-SR04 devices, so you'd want to get something spec'ed for 3.3 V operation from a reputable vendor.

1 Like

Thanks MrMark! I guess that's the easiest solution, I'll go for that

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.