Suddenly change in value

l have 12 ultrasonic sensor connected with Arduino mega 2560, sensor shield and w5500 Ethernet shield. I am monitoring all the sensor data via modbus TCP/IP in EBO 3.2. I am monitoring all the sensor data in inch sometimes 1 or 2 sensor suddenly change their value to 0, 1.5 and 2 and for 1 to 2 second and then go back their actual value. Can someone tell me how to stop th
is behavior.

From what You tell, no one can tell for sure.
Posting the code, the way described in the advice "How to get the best out of this forum.", is the first request.
A second request is a description of the orientation of the sensors as well as the surroundigs, a little drawing.

if you are shooting them all at the same time or too frequently then you might have interferences from nearby ultrasonic emission or bounces from a previous trigger

but as @Railroader said, without the code and the actual circuit and understanding of how the sensors are laid out, it's hard to tell and just guess work...

help us help you, read How to get the best out of this forum and provide the necessary documentation for your ask.

hi this is my code

#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>

#define NUM_SENSORS 12

int triggerPins[NUM_SENSORS] = {3, 5, 7, 9, 13, 15, 17, 19, 21, 23, 25, 27};
   int echoPins[NUM_SENSORS] = {2, 4, 6, 8, 12, 14, 16, 18, 20, 22, 24, 26};


unsigned long previousMillis = 0;
const long interval = 100; // Adjust this interval as needed (1 second in this example)

// Create a ModbusIP object
ModbusIP mb;

int previousDistance[NUM_SENSORS] = {0}; // Store values as integers

void setup() {
  Serial.begin(9600);
  
  // Ethernet settings (depending on MAC and Local network)
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  // The IP address for the shield
  byte ip[] = { 192, 168, 1, 121 };
  
  // Config Modbus IP
  mb.config(mac, ip);
  
  for (int i = 0; i < NUM_SENSORS; i++) {
    pinMode(triggerPins[i], OUTPUT);
    pinMode(echoPins[i], INPUT);
    mb.addIreg(i, 0); // Initialize input registers starting from 0
  }
}

void loop() {
  mb.task();

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    for (int i = 0; i < NUM_SENSORS; i++) {
      // Trigger the ultrasonic sensor
      digitalWrite(triggerPins[i], LOW);
      delayMicroseconds(2);
      digitalWrite(triggerPins[i], HIGH);
      delayMicroseconds(10);
      digitalWrite(triggerPins[i], LOW);

      // Read the echo pin and calculate distance in inches
      int duration = pulseIn(echoPins[i], HIGH);
      float distanceInInches = duration / 74.0 / 2.0; // Convert to inches (74 microseconds per inch)

      // Convert to an integer with two decimal places
      int distanceInteger = int(distanceInInches * 100.0);

      // Check if the sensor value has changed
      if (distanceInteger != previousDistance[i]) {
        // Send the distance value over Modbus to Input Register
        mb.Ireg(i, distanceInteger); // Use input registers for data storage

        Serial.print("Sensor ");
        Serial.print(i + 1);
        Serial.print(": Distance = ");
        Serial.print(float(distanceInteger) / 100.0, 2); // Convert back to float with 2 decimal places
        Serial.println(" inches");

        // Update the previous sensor value
        previousDistance[i] = distanceInteger;
      }
    }
  }
}

picture is also attached

try to add some delay between two consecutive triggers and/or ensure you trigger one from the top shelf and then one from the bottom shelf and then the next one from the top shelf should not be close to the first one to minimise interferences

may be try triggering in this order (the green circles sequence)

Thank you so much. I'll try tomorrow

Add a 2 ms delay to your for loop, increase "interval" to 200.

Hi,
What is the application?
What are you trying to detect?

From your video, it looks like you will have reflections from all over the assembly, hard surfaces and the ultrasonic units mounted to look overt basically overlapping areas.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

May be issue is overlapping.I am at a stage where I cannot remove all the sensors now.


That’s a lot of wires that do not seem solidly connected…

Just give it a try with a 250ms delay between two consecutive pings
It will take a long time to go through the sensors but if you don’t see the bad behavior again then it likely means that cross contamination and ultrasound bouncing was likely the issue. Try to reduce the 250 to something smaller (probably 50 or 60 would do) and as I said don’t proceed in sequence on the same row to minimize the local bouncing.

Just curious - is that a system to track the presence / absence of a collector object that would be on the shelf ?

I check and update because there is no issue with the wires. Because I have individually tested all the sensors, I am automating a shop in which whenever something is lifted, I receive an indication on the computer.If the timing is too long, then it's not very beneficial.

Documentations I had seen recommends 60 milliseconds between measurements.

so for 12 sensors measured successively it would be 60 x 12 = 720ms + the time it takes to do the measurement and given the shelf space is pretty narrow it's probably 1 or 2ms max

so you should be able to read all the position at least once per second

if there is no cross contamination when reading different shelves then you could probably do x3 (since you have 3 shelves) and work at least at 3 readings per second.

The suggestion to have 250ms was to be extra cautious and see if the issue you have is coming from this or not

basically the risk is this:

you trigger a measurement on a sensor and get reading back (green arrows)

then you trigger the next one but the bouncing from the previous one is just reaching the detector (red arrows) and so you see a short distance as the sensor believes it's the signal coming back already from its trigger

and the bouncing can be complicated like

as you have a short distance and a reflective surface the bouncing does not get soften quickly. the 60ms delay helps ensure there are no parasites left

1 Like

Hi,
What are you aiming to detect in the space below each sensor?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

it's a shop

I suppose @Noman-Jameel wants to prevent shop lifting and be made aware that an object has been removed from the shelf (a smart thief noticing the distance sensor would slide a box of about the same size under the sensor)

@J-M-L yes exactly

A random thought, which might be the solution or it might be a difficult waste of time. If there are various objects on the shelf then the ultrasonic sensors are going to produce a pattern characteristic of that particular set of objects. Move one of the objects and the pattern will change. Detecting the change in pattern will tell you if something has been taken.

No, I don't know how to do this! It's just an idea.

This shop is not yet complete; I will install glass in front of each shelf where only a specific box can be taken out.

Hi,
In hindsight.

Did you look at a Time of Flight distance sensor, probably less reflection and more precise location?

Tom... :grinning: :+1: :coffee: :australia:

I want to run the shop without the shopkeeper, and by the end of the day, I want to know today's sales and manage inventory.This is just my idea, and I am in the process of implementing it.

2 Likes