School Project with Ultrasonic Sensors and Conveyor belt.

Thread Starter

karezy

Joined Feb 17, 2023
1
Hey there!

I'm doing a project for school where a package is on the move and during it's travel I need to measure the volume of that package. I've measured 2 dimensions already by using an ultrasonic sensor. So we can save 2 extra sensors we decided to measure the third dimension by timing how long it is detected under the sensor that measures the height. But me and my team are having trouble setting up the timer. When we have the time period we want to multiply it with the speed of the belt so we get the length. as last we will multiply every dimension and it will be printed on our lcd. We've already spend more than 15 hours trying to solve this problem. The arduino forum didn't help us either, can someone pls help us? (You can view our code at the bottom)



MicrosoftTeams-image.jpg





C-like:
//I2C LCD Toevoegen

  #include <Wire.h>

  #include <LiquidCrystal_I2C.h>

  LiquidCrystal_I2C lcd (0x27,16,2); /// of 16, 2

  int trigPin1 = 10;

  int echoPin1 = A1;

  int trigPin2 = 3;

  int echoPin2 = A0;

  int trigPin3 = 5;

  int echoPin3 = A2;

// Sensor 1,2 & 3 maximum afstand

  int max1 = 60;

  int max2 = 60;

  int max3= 70;

  // Timers opzetten

  unsigned long starttime;

  unsigned long time1;

  unsigned long endtime;


void setup() {



//Sensor pinnen

  pinMode(trigPin1, OUTPUT);   

  pinMode(echoPin1, INPUT);


  pinMode(trigPin2, OUTPUT);

  pinMode(echoPin2, INPUT);


  pinMode(trigPin3, OUTPUT);

  pinMode(echoPin3, INPUT);


//LCD Setup

  lcd.begin();

  lcd.clear();

  lcd.backlight();

//Basic text LCD

  lcd.setCursor(2,0);

  lcd.print("Volume");

  starttime = millis() //start timer

}


void loop() {

// Variabelen set

  int duration1, distance1, duration2, distance2, duration3, distance3;


// Sensor 1

  digitalWrite (trigPin1, HIGH);

  delayMicroseconds (10);

  digitalWrite (trigPin1, LOW);

  duration1 = pulseIn(echoPin1,HIGH);

// De afstand is de tijd maal door de snelheid van het geluid gedeeld door 2

  distance1 = duration1 * 0.034 / 2;

  if (distance1 < max1) {

    int final1 = distance1;


  delay(50);


    // Sensor 2

  digitalWrite (trigPin2, HIGH);

  delayMicroseconds (10);

  digitalWrite (trigPin2, LOW);

  duration2 = pulseIn(echoPin2,HIGH);

// De afstand is de tijd maal door de snelheid van het geluid gedeeld door 2

  distance2 = duration2 * 0.034 / 2;

  if (distance2 < max2) {

    int final2 = distance2;


delayMicroseconds(50);


//sensor 3

digitalWrite (trigPin3, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin3,LOW);

duration3=pulseIn(echoPin3,HIGH);

distance3=duration3 * 0.034 / 2;

if (distance3 < max3) {

int final3 = distance3;


delayMicroseconds(50);


// startime false maken

bool starttime = false;


// timer starten

if (final3<56){

  time1 = millis();

  if (final3>56){

    endtime = millis();

    // tijd pakket onder sensor 3

  unsigned long elapsedtime = (endtime-meetwaarde1);


// gemeten waardes omvormen en een paar berekeningen

const int measuredvalue = (final1 + final2);

const int widthpackage = (60-gemetenwaarde);

const int heightpackage = (54-final3);

const int opp1 = (heightpackage*widthpackage);

const int timsensor3 = (elapsedtime*10^-3);//measured time is in millis and i need it in seconds

const int speedconveyorbelt = (1);

const int lengthpackage = (tijdsensor3*speedconveyorbelt);




  lcd.setCursor(2,1);

  lcd.print (lengthpackage);

    }

   }   

  }

}

}

delay(4000);

}
 
Last edited by a moderator:

ericgibbs

Joined Jan 29, 2010
19,122
hi karezy.
Welcome to AAC.
Have you considered using the millis option for measuring the time the object enters and leaves the sensor #3 detection zone.?
E
 

MrSalts

Joined Apr 2, 2020
2,767
Right. A long, unsigned internet variable "start" can be created and set to the current number of milliseconds since the Arduino was started.
Code:
unsigned long start = millis();
Same can be sone when the package passes, set a variable called "end".

then the time for the package to pass is simply
unsigned int duration = end - start;

Make sure your answer is of format, "unsigned" or you can run into an issue. The Arduino has an error when converting large unsigned long values to non-unsigned values.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,188
hi karezy.
Welcome to AAC.
Have you considered using the millis option for measuring the time the object enters and leaves the sensor #3 detection zone.?
E
…Or the micros function if you need higher resolution than 1ms. Note that micros provides a resolution of 4ns. When you use either function, there are several considerations you need to be aware of. This article presents the differences between millis and micros and discusses these considerations.
 

MrSalts

Joined Apr 2, 2020
2,767
…Or the micros function if you need higher resolution than 1ms. Note that micros provides a resolution of 4ns. When you use either function, there are several considerations you need to be aware of. This article presents the differences between millis and micros and discusses these considerations.
Most ultrasonic sensor modules can only make measurements every 40mSec (or so) so microsecond measurements are a bit extreme. Also, the micros() will roll over after 71 minutes and cause some problems if the box measurement starts just before rollover and the second measurement happens after rollover, you have some more code to add to insure you don't have a problem. With millis(), you have 71000 minutes between rollovers.
 

ericgibbs

Joined Jan 29, 2010
19,122
hi,
One downside of this method of dimensioning is that the object has to correctly aligned on the conveyor belt at right angle to the sensors.
E
 

MrSalts

Joined Apr 2, 2020
2,767
hi,
One downside of this method of dimensioning is that the object has to correctly aligned on the conveyor belt at right angle to the sensors.
E
If there are a limited number of package sizes to identify (and known beforehand), randomly oriented boxes could be identified. Also, some simple spring loaded swing-arms can easily align the boxes.
 

ericgibbs

Joined Jan 29, 2010
19,122
hi S,
I agree some method of alignment is required.
We also have to assume that the objects are a rectilinear 'box shape'.

Of course, this is a College project, so it has some limitations.

I don't think an ultrasonics approach would be my first choice.
E
 

djsfantasi

Joined Apr 11, 2010
9,188
May I make one suggestion? I will refer to the following lines (there are three):

distance1 = duration1 * 0.034 / 2;

Instead of multiplying by 0.034 and then dividing by 2, why not just multiply by 0.017 ?

distance1 = duration1 * 0.017

Much more efficient and saves an operation.
 

MrSalts

Joined Apr 2, 2020
2,767
May I make one suggestion? I will refer to the following lines (there are three):

distance1 = duration1 * 0.034 / 2;

Instead of multiplying by 0.034 and then dividing by 2, why not just multiply by 0.017 ?

distance1 = duration1 * 0.017

Much more efficient and saves an operation.
Or, instead of multiplying by a floating point value, (0.034 meters per microsecond for speed of sound), work in millimeters per microsecond and multiply by 17 to really speed up the calculation.
 
I don't think an ultrasonics approach would be my first choice.
Better to use a laser diode and photo diode/transistor perpendicular to the travel to measure time of travel of the box precisely, but if you are stuck with just using ultrasonic sensors....

I don't know how accurate those ultrasonic sensors are at measuring distance, or how they respond to bouncing of a target which is at an angle. Measuring the height above the conveyor belt is probably easiest to do with the ultrasonic sensor if the box is rectangular but how well do they respond to striking the side of a box which is at an angle?

Two downward looking sensors set maybe 10cm apart should give you a cross check of height, and the time offset between them will give you the angle of orientation of the box on the conveyor. The third sensor perpendicular to the direction of the conveyor could maybe give you a cross check of the angle of orientation if it sees the change in distance to the box with travel and if it responds nicely to give you the time of travel of the box in the line of sight of that sensor. Getting the same measurement different ways adds some nice cross-check/redundancy in the measurements.

In practice, given the tolerance in measurements, I'd argue that using millis() will not be your biggest source of inaccuracy. If T1 is used to measure time of a state let T1 = millis() at the beginning of timing and T1 = millis() - T1 at the end of timing. If you use unsigned long variable for T1 you are good for around 49 days until you get a negative result.

As suggested by MrSalts, in practice there tend to be a limited number of box sizes used, so an array with those sizes and an algorithm which selects one of those with a stated probability might be a nice addition.
 
Top