Introduction: Arduino Rotating Sonar
In this Instructable you'll be able to learn how to make a rotating sonar device using an Arduino. For this instructable the following items are needed:
- 1x Arduino Uno
- 1x Breadboard
- 1x USB Cable for Serial Connection to PC or Laptop
- 1x HC-04 Ultrasound sensor
- 1x Small electric motor (TG9e)
- 3x Nuts
- 2x Washers
- 1x Bolt
- 2x Small screws
- 2x Zip-tie
- 1x Small plastic box (or other object for use as base for motor)
- 1x Plastic cup (or other object for use as base for ultrasound sensor)
- 3x Extender cables with 4 wires
- 1x Extender cable with 3 wires
- 5x Wires
- 1x Drill
One of the following:
- 1x Bicycle Bell with a 2:1 ratio
- 2x Gears with a 2:1 ratio
With that out of the way, let's get started with step one!
Step 1: Step 1: Your Motor
Make a hole in the base for the motor to fit in, and file it to be square. Then fit in the motor and attach the motor with 2 little screws. If you use a bicycle bell, open it up and salvage the gears from it. If you want to, you can remove the little metal rings that are in it to save some weight. Then attach the largest of the 2 gears to the axle of the motor.
Step 2: Step 2: the Cuppening
Drill 2 large holes into the cup where the “eyes” of the HC-SR04 can fit through. Then drill a hole on the opposite side of the cup for the cables to go through. Put the “eyes” through the two holes and ziptie the wires so you don’t have to glue anything. Glue the smallest gear to the centre of the bottom of the cup and drill another hole in the bottom of the cup all the way through the gear for the bolt to go through. Line up the gears so that they make proper contact, and mark a dot on the base directly underneath the centre of the smallest gear and drill it out.
Step 3: Step 3: Finishing Up the Mechanical Stuff
Attach the bolt to the base using two nuts, get the cup to the right height (smaller gear lines up with the larger gear) and attach it to the bolt using a nut at the top.
Step 4: Step 4: the Electronics
Attach the wires as shown, and use zipties to fasten wires without having to glue anything. Alternatively, glueing is still an option but it makes reusing wires and stuff a lot harder.
Step 5: Step 5: the Code
All that's left now is the code. Speaking of which:
#include
const int trig=13; const int echo=12; const int servopin = 9; const int delaytime = 400; int pos = 0; ///This sets the next position Servo myServo; long north, ne, nw, east, south, se, sw, west;void setup() { Serial.begin(9600); pinMode(trig, OUTPUT); pinMode(echo, INPUT); myServo.attach(servopin); }
void loop() { long duration, distance; if(Serial.available() > 0) { String data; data = Serial.readString(); if(data == "north") /////north { pos = 0; myServo.write(0); delay(delaytime); digitalWrite(trig, LOW); //make sure trigger is set to low delayMicroseconds(2); //wait for 2 microseconds digitalWrite(trig, HIGH); // Trigger the echo delayMicroseconds(10); //send out for 10 microseconds digitalWrite(trig, LOW); //turn the trigger off again duration = pulseIn(echo, HIGH); distance = duration / 2; distance = distance / 29; Serial.print("Distance: \t"); Serial.print(distance); Serial.print("cm \t"); Serial.print("Direction: \t"); Serial.print(data); Serial.print("\n"); north = distance; }
if(data == "east") /////east { pos = 90; myServo.write(pos / 2); delay(delaytime); digitalWrite(trig, LOW); //make sure trigger is set to low delayMicroseconds(2); //wait for 2 microseconds digitalWrite(trig, HIGH); // Trigger the echo delayMicroseconds(10); //send out for 10 microseconds digitalWrite(trig, LOW); //turn the trigger off again duration = pulseIn(echo, HIGH); distance = duration / 2; distance = distance / 29; Serial.print("Distance: \t"); Serial.print(distance); Serial.print("cm \t"); Serial.print("Direction: \t"); Serial.print(data); Serial.print("\n"); east = distance; }
if(data == "south") /////south { pos = 180; myServo.write(pos / 2); delay(delaytime); digitalWrite(trig, LOW); //make sure trigger is set to low delayMicroseconds(2); //wait for 2 microseconds digitalWrite(trig, HIGH); // Trigger the echo delayMicroseconds(10); //send out for 10 microseconds digitalWrite(trig, LOW); //turn the trigger off again duration = pulseIn(echo, HIGH); distance = duration / 2; distance = distance / 29; Serial.print("Distance: \t"); Serial.print(distance); Serial.print("cm \t"); Serial.print("Direction: \t"); Serial.print(data); Serial.print("\n"); south = distance; }
if(data == "west") /////west { pos = 270; myServo.write(pos / 2); delay(delaytime); digitalWrite(trig, LOW); //make sure trigger is set to low delayMicroseconds(2); //wait for 2 microseconds digitalWrite(trig, HIGH); // Trigger the echo delayMicroseconds(10); //send out for 10 microseconds digitalWrite(trig, LOW); //turn the trigger off again duration = pulseIn(echo, HIGH); distance = duration / 2; distance = distance / 29; Serial.print("Distance: \t"); Serial.print(distance); Serial.print("cm \t"); Serial.print("Direction: \t"); Serial.print(data); Serial.print("\n"); west = distance; } if(data == "northeast") /////north-east { pos = 45; myServo.write(pos / 2); delay(delaytime); digitalWrite(trig, LOW); //make sure trigger is set to low delayMicroseconds(2); //wait for 2 microseconds digitalWrite(trig, HIGH); // Trigger the echo delayMicroseconds(10); //send out for 10 microseconds digitalWrite(trig, LOW); //turn the trigger off again duration = pulseIn(echo, HIGH); distance = duration / 2; distance = distance / 29; Serial.print("Distance: \t"); Serial.print(distance); Serial.print("cm \t"); Serial.print("Direction: \t"); Serial.print(data); Serial.print("\n"); ne = distance; }
if(data == "northwest") /////north-west { pos = 315; myServo.write(pos / 2); delay(delaytime); digitalWrite(trig, LOW); //make sure trigger is set to low delayMicroseconds(2); //wait for 2 microseconds digitalWrite(trig, HIGH); // Trigger the echo delayMicroseconds(10); //send out for 10 microseconds digitalWrite(trig, LOW); //turn the trigger off again duration = pulseIn(echo, HIGH); distance = duration / 2; distance = distance / 29; Serial.print("Distance: \t"); Serial.print(distance); Serial.print("cm \t"); Serial.print("Direction: \t"); Serial.print(data); Serial.print("\n"); nw = distance; }
if(data == "southeast") /////south-east { pos = 135; myServo.write(pos / 2); delay(delaytime); digitalWrite(trig, LOW); //make sure trigger is set to low delayMicroseconds(2); //wait for 2 microseconds digitalWrite(trig, HIGH); // Trigger the echo delayMicroseconds(10); //send out for 10 microseconds digitalWrite(trig, LOW); //turn the trigger off again duration = pulseIn(echo, HIGH); distance = duration / 2; distance = distance / 29; Serial.print("Distance: \t"); Serial.print(distance); Serial.print("cm \t"); Serial.print("Direction: \t"); Serial.print(data); Serial.print("\n"); se = distance; }
if(data == "southwest") /////south-west { pos = 225; myServo.write(pos / 2); delay(delaytime); digitalWrite(trig, LOW); //make sure trigger is set to low delayMicroseconds(2); //wait for 2 microseconds digitalWrite(trig, HIGH); // Trigger the echo delayMicroseconds(10); //send out for 10 microseconds digitalWrite(trig, LOW); //turn the trigger off again duration = pulseIn(echo, HIGH); distance = duration / 2; distance = distance / 29; Serial.print("Distance: \t"); Serial.print(distance); Serial.print("cm \t"); Serial.print("Direction: \t"); Serial.print(data); Serial.print("\n"); sw = distance; } if(data == "showdist") ////show all distances { Serial.print("---------------------------------------------- \n"); Serial.print("Distance in cm: \n"); Serial.print("---------------------------------------------- \n"); Serial.print(nw); Serial.print("\t"); Serial.print(north); Serial.print("\t"); Serial.print(ne); Serial.print("\n");
Serial.print(west); Serial.print("\t"); Serial.print("x"); Serial.print("\t"); Serial.print(east); Serial.print("\n");
Serial.print(sw); Serial.print("\t"); Serial.print(south); Serial.print("\t"); Serial.print(se); Serial.print("\n");
Serial.print("---------------------------------------------- \n"); Serial.print("X represents the origin point \n"); Serial.print("---------------------------------------------- \n"); } }
}