SONAR SWEEP

QUICK PROJECT DESCRIPTION

PROJECT OBJECTIVE: Build a sweeping sonar and display the data on your computer, as in old-school submarine monitors.

HARDWARE REQUIREMENTS: Provided in the Mango Labs Mechatronics Maker Kit: Arduino-compatible board, USB cable,HC-SR04 Module, micro servo, 5 male to male jumper cable, 4 male to female jumper cables, mini breadboard.

Additional hardware: computer, 3d printed parts here (optional), glue gun, rubber bands, small star screwdriver.

SOFTWARE REQUIREMENTS: Arduino environment (or online editor). Download the sonar library here. Unzip and paste the folder in the Arduino libraries folder. It’s in the same location as your default sketches. You can look it up under File/Preferences Sketchbook location.

KNOW-HOW REQUIREMENTS: Basic Arduino environment knowledge (learn more here). Basic electrical skills.

QUICK OVERVIEW
  1. Connect everything as shown in the diagram.
  2. Install the NewPing library (if you tested the board with the Hello World example you have the library already installed).
  3. Compile and upload this sketch to your board.
  4. Install Processing.
  5. Hot glue the ultrasonic sensor on the servo horn and mount on the servo, or use the 3D printable mountings.
  6. Unzip and run this Processing sketch with the Arduino compatible board connected to your computer.
PIN OUT DIAGRAM
1
2
3
4
5
6
7
8
9
Sonar sweep project diagram
1

5 VOLTS and Ground

2

Brown cable is Ground
Red cable is power (5 volts)
Yellow/orange cable is control and goes to digital 12 on your Arduino compatible board (yellow cable)

3

GND goes to GND (black cable)
Vcc to 5 volts (red cable)
Trig to digital 10 on your Arduino compatible board (blue cable)
Echo to digital 11 on your Arduino compatible board (purple cable)

4

Breadboard acts as a power hub for the servo and Ultrasonic sensor

5

USB connected to your computer

6

PIN10 goes to the Trig pin of the ultrasonic sensor

7

PIN11 goes to the Echo pin of the ultrasonic sensor

8

PIN12 goes to the yellow/orange control line of the servo

9

PIN12 goes to the yellow/orange control line of the servo

SETUP

The easiest way to get the ultrasonic sensor on the servo is to attach a servo horn and glue the ultrasonic sensor with a hot glue gun to the horn.

Make a steady base for the servo with the ultrasonic sensor attached, using one of the breadboards as a base and a rubber band to keep it stable.

You can use the same breadboard as your power hub to supply power to the servo and ultrasonic sensor, following the cabling shown in the Fritzing  diagram.

If you own a 3D printer or have access to a 3D printer in a local library or 3D printing service, or want to use an online service such as the community-powered 3DHubs,here are the STL files. I used ABS with a 0.23 mm layer thickness, 2 bottom layers, 3 perimeter layers, 3 top layers and a 30% infill. Printed at 70 m/s it takes around 90 minutes. Depending on your printer, materials and settings, some tolerances could change (a hot glue gun is also handy here).

Sweeping sonar top view of 3D printed base

If everything is OK you should be able to fit the Arduino-compatible board on the base (there are holes to screw it in for a more permanent installation. Otherwise you can hot glue it, or simply fit it in). The breadboard also has a recessed base (It pressure-fits but also has holes for screws. You can stick it on by peeling the included double tape). The servo also fits in a recessed base and can be screwed to the supports using the long screws supplied. Screw the ultrasonic sensor holder to the servo with the small screw supplied. The sensor pressure fits in the support.

Sweeping Sonar ultrasonic sensor horn for servo

After cabling, upload the sketch to the Arduino-compatible board. When running the Processing sketch you should see the servo bouncing back and forth, as well as the visualization following the servo and displaying the distances of the objects in its path. If you have communication problems between the Arduino-compatible board and Processing, check your serial port. Processing assumes it is the first one. If it isn’t comment this line (33) out String portName = Serial.list()[0]; putting a double slash at start and un-comment this one (36) out //String portName = “COM15”; by erasing the slashes, and change the number to the one corresponding to the port you are using (the same you used to upload the sketch from the Arduino environment. It is shown on the bottom right).

Sweeping sonar project

Have Fun!

CODE

//Rewieved and modified by Dr. Mangus for Mango Labs
// based on the sketch of Dejan Nedelkovski

// Includes the Servo library
#include <Servo.h>. 
//Includes de dsitance mesurement library
#include <NewPing.h>
// Defines Tirg and Echo pins of the Ultrasonic Sensor
#define TRIGGER_PIN  10  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
// Variables for the duration and the distance
long duration;
int distance;
int initialpos;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
Servo myServo; // Creates a servo object for controlling the servo motor

void setup() {
  pinMode(TRIGGER_PIN, OUTPUT); // Sets the trigPin as an Output
  pinMode(ECHO_PIN, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600);
  myServo.attach(12); // Defines on which pin is the servo motor attached
  initialpos = myServo.read();
  delay(50);
  for(int i=initialpos;i>14;i--){    
  myServo.write(i);
  delay(42);
  }
 }
void loop() {
  // rotates the servo motor from 15 to 165 degrees
  for(int i=15;i<=165;i++){  
  myServo.write(i);
  delay(46);
  distance = sonar.ping_cm();// Calls library for calculating the distance measured by the Ultrasonic sensor for each degree
  
  Serial.print(i); // Sends the current degree into the Serial Port
  Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  Serial.print(distance); // Sends the distance value into the Serial Port
  Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  }
  // Repeats the previous lines from 165 to 15 degrees
  for(int i=165;i>15;i--){  
  myServo.write(i);
  delay(46);
  distance = sonar.ping_cm();
  Serial.print(i);
  Serial.print(",");
  Serial.print(distance);
  Serial.print(".");
  }
}