Introduction: Scan Nearby Objects Using ARDUINO
This project utilises the HC-SR04 ultrasonic sensor to scan for nearby objects.
You can program the Arduino to sound an alarm when the sensor detects an object within a specific vicinity. Connecting it to a computer allows data to be plotted to make a simple sonar scanner. The scanning ability is made possible through the use of a hobby servo motor SG-5010, and an Adafruit motor shield v1.0.
This project could easily be extended to provide object avoidance for any robotics project. This tutorial was designed so that you could see how the components interact, and also to see how you can use and expand the functionality of the motor shield.
Step 1: Parts Required
- Freetronics Eleven or any compatible Arduino.
- Adafruit motor shield v1.0
- HC-SR04 Ultrasonic Sensor
- MG-995 or SG-5010 Standard servo
- Mini Breadboard 4.5cm x 3.5cm
- Female header pins to allow easy access to the analog pins on the Motor Shield
- Piezo buzzer - to sound alarm
- 9V Battery and Battery Clip
- Wires to connect it all together
- Paper (to print the face of the gauge), and some glue to stick it to the wood.
- MDF Standard panel (3mm width) - for the top and base of the gauge, and the pointer.Galvanized bracket (25x25x40mm)
- Timber screws: Hinge-long threads csk head Phillips drive (4G x 12mm)Velcro dots - to allow temporary application of the mini-breadboard to the gauge.
- The gauge was used as a customisable housing for the Arduino and related parts, and to provide some visual feedback of the servo position.
Step 2: Schematics
The Servo motor can be connected to either of the Servo motor pins (Digital 9 or 10). In this case, the Servo is attached to digital pin 10.Make sure you read the servo motor data sheet and identify the VCC (5V), GND, and Signal connectors. Not all servos have the same colour wires.
My servo motor has a white signal wire, a red VCC wire and a black GND wire. Also when connecting your wires to the HC-SR04, pay attention to the front of the sensor. It will identify the pins for you. Make sure you have the sensor facing the correct way. In this sketch, the sensor is actually facing towards you.
In this sketch - we connect the :
- Echo pin to Analog pin 0 (A0).
- Trigger pin to Analog pin 1 (A1)
- VCC to a 5V line/pin and GND to a GND line/pin
Pay attention to your motor shield, I have seen some pictures on the internet where the 5V and GND are reversed.
Step 3: Arduino Code
The motor shield requires the Adafruit motor shield driver library to be installed into the Arduino IDE.
Servo Angles: You will notice on line 22, the maximum servo angle used was 176. This value was obtained through trial and error (see below).
Calibrating the servo angles You may need to calibrate your servo in order to move through an angle of 0 to 180 degrees without straining the motor. Go to line 21-22 and change the minAngle to 0 and the maxAngle to 180. Once you load the sketch to the Arduino/Freetronics ELEVEN, you can then open the Serial Monitor and type a value like 10 , and then keep reducing it until you get to 0. If you hear the servo motor straining, then move it back up to a safe value and change the minimum servo angle to that value. Do the same for the maximum value.
In this example, the servo's minAngle value was 0, and maxAngle value was 176 after calibration, however, as you can see from the video, the physical range of the servo turned out to be 0 to 180 degrees.
Step 4: The Processing Sketch
You can download the Processing IDE from this site.