Grove Ultrasonic not working properly?

Hi, I am currently doing a project about a human body thermometer. The sensor I am using is the grove ultrasonic sensor which is the same sensor with the HC-SR04 with less input pin needed (If I am not wrong) . The code I created is down below.

#include "Ultrasonic.h"
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <LiquidCrystal_I2C.h>
Ultrasonic ultrasonic(7);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
LiquidCrystal_I2C lcd(0x27, 16, 2);
long range=200;

void setup() {
pinMode(9, OUTPUT);
int counter=0;
  if (range<=1){
    lcd.print("Body Temp:");
    tone(9, 500);
  range= ultrasonic.MeasureInCentimeters();

void loop() {

The sensor will suddenly pick up range<=1 without anything in its range and go in to the if section. Try to troubleshoot it by reading the value of range with a separate code but it never shows up to be 1 or less than 1. Is it the sensor is faulty or there is a bug/problem in my code?
Any help is appreciated, Thanks for helping!! :slight_smile:

Generally, you need to wait after reading an ultrasonic sensor, so that any echoes die away. Your code is pinging continuously.

If I rewrite the echo part of the code in the void loop instead with a delay under it does it make it better? Or generally is there another way of writing the code for ultrasonic sensor. I am quite new to Arduino so I am still not sure about how it works.

You can use what you have now. Just add a delay after (or before) you read the sensor.

Alright, Thanks for the Help!! Greatly appreciate it.

It seems to work fine now.

That's good. It's a common mistake to forget that the speed of sound is slow in this context compared to how fast an Arduino runs.

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