DEV Community

Cover image for Making my own Sonar System
Liam
Liam

Posted on Originally published at moss41.ie

Making my own Sonar System

Initial:

In my boredom I designed and made my own sonar detection system with a few parts and an Arduino. I used a ultrasonic sensor to detect objects in the way with a servo to get a measurement over 180 degrees. I connected the ultrasonic sensor to pins 9 for the trigger and 8 for the echo. The servo used its own library that would take the servo degree as an int. Click here to go to the github page.

int trigPin = 9;
int echoPin = 8;
int servoPos;
Enter fullscreen mode Exit fullscreen mode

To start off I initialised the ultrasonic sensor and got a reading off it, using the reading to get a distance in centimetres. To get better results, I added a noise filter by getting 20 samples and using them to get the average distance from the sensor. The more samples I got, the more consistent the measurement were. However this could cause the Arduino to take longer to do each calculation, so I kept it to 20 samples.

int getMeasurement(){

  //Get 15 samples
  for(int sample =0; sample < 20; sample++){
    filterMeasures[sample] = ultrasonicMeasurement();
    //delay(10);
    }


  //Filter samples by ascending
  for(int i = 0; i < 19; i++){
    for (int j = i + 1; j < 20; j++){
      float swap = filterMeasures[i];
      filterMeasures[i] = filterMeasures[j];
      filterMeasures[j] = swap;
    }
  }

  //filter noise
  double sum = 0;
  for(int sample = 5; sample < 15; sample++){
     sum += filterMeasures[sample];
  }

  distance = sum / 10;

  int intDistance = (int)distance;

  return intDistance;

}

float ultrasonicMeasurement(){
  //20 microsecond pulses to trigPin
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  //Use echoPin to measure pulses
  durationUs = pulseIn(echoPin, HIGH);

  distanceCm = 0.017 * durationUs;

  return distanceCm;

}
Enter fullscreen mode Exit fullscreen mode

Next, I added the servo motor control, so I used a for loop to set the degree the servo was supposed to be. This made it straightforward to record the servo's position and log the distance at each angle. This would mean the servo would move to the set degree and then get measure the distance before moving again. So, with that I managed to get the servo to move smoothly from side to side. However, when the ultrasonic sensor was getting it measurements, it seemed like some were a little random or even plain wrong I had both parts were sharing the 5V rail and I fixed randomness by using another 5V power supply for the servo. This allowed both to get a good supply of power.

Serial.println("Loop one");
  for(int x = 0; x <= 180; x+=1){

    servoControl(x);
    delay(75);
    int currentMeasurement = getMeasurement();
    servoPos = x;
    posArray[servoPos] = currentMeasurement;
    Serial.print("Pos: ");
    Serial.println(servoPos);
    Serial.print(currentMeasurement);
    Serial.println(" cm");
    //delay(30);
  }

  Serial.println("Loop two");
  for(int x = 180; x >= 0; x-=1){
    servoControl(x);
    delay(75);
    int currentMeasurement = ultrasonicMeasurement();
    servoPos = x;
    posArray[servoPos] = currentMeasurement;
    Serial.print("Pos: ");
    Serial.println(servoPos);
    Serial.print(currentMeasurement);
    Serial.println(" cm");
    //delay(30);
  }
Enter fullscreen mode Exit fullscreen mode

Finally, I went to FreeCAD and designed a case that held everything together. The Arduino would be seated under everything with the servo and ultrasonic sensor mounted on top of the case.

Top comments (0)