Introduction: Ultrasonic Mapmaker Using Arduino and MatLab
Vision is one of the most exciting senses you can endow on your robot. The ability to perceive obstacles allows a robot to make an educated decision regarding further movements.
However, it is also not the easiest senses to implement for a few reasons. Firstly, vision can be abstracted to a two-dimensional vector when it comes to computing, as opposed to a single dimensional scalar such as, say, temperature. Secondly, it is an analog value like many other senses, so it requires a certain degree of digitization and processing for it to make sense to a computer. Finally, though this is not strictly a problem, is visualization. Vision data cannot be represented as mere numbers to humans. Although that is how computers process data, it makes no sense for a human to decode digits. However, with the help of modern technology, and basic knowledge of physics and math, we will be able to endow our robot with the gift of sight!
Step 1: Theory
In this project, we are using an ultrasonic Distance sensor. It generates sound waves beyond the scope of human hearing and measures distance by calculating the time required by these waves to hit an obstacle and travel back. This is similar to the principle used by bats.
Another component that we are going to use is a servo motor. It differs from the usual DC motor in that it can turn very precisely to a given angular position and hold its state there. When a servo motor is given pulses of a specific duration, it moves to the corresponding angular position.
We will be using both these components to get a 180 degree field of view for our robot.
Step 2: Collecting Materials
This project uses the following hardware
- Arduino Uno/Yun (Please note that any Arduino footprint board can be used in place of the Uno or Yun)
- Arduino Prototyping Shield
- An HC-04 Ultrasonic Sensor
- A servo motor (I've used the Tower Pro SG90 because its very compact)
On the software side we are using the following programs
- The Arduino IDE to upload control code to the Arduino to rotate the servo and get distance data from the ultrasonic sensor and also push it to the serial port.
- Mathworks MatLab to receive data from the serial line, process it and visualize it to a graph.
Step 3: Mechanical Assembly
Using a small piece of general purpose PCB, make a small header for the HC-04, and attach it to a servo horn using a piece of double sided tape.
This step is optional, but to make the system more compact, I've attached the servo to the jutting part of the protoboard shield using double sided tape as well.
The final result should look like Wall-E's abdomen.
Attachments
Step 4: The Arduino Code
The Arduino code controls the motion of the servo motor, and when the readings from the ultrasonic sensor are captured and how frequently. It also pushes the sensor data to the serial port.
- Import libraries
- Initialize variables and pins.
- Initialize servo object
- Initialize serial communication
- Wait for 3 seconds
- Initialize counters to 0
- Rotate servo by 1 degree
- Get ultrasonic sensor data 10 times (set by default)
- Average the data
- Send the average to serial port
- Return to step 7
Attachments
Step 5: The MatLab Code
The MatLab code deals more with data than the actual control of the board, so all the sensor data is pushed over serial to the PC, where it is read by MatLab.
Now, the data that we receive from the Arduino tells us two things. The degree of rotation of the servo and the distance of an obstacle in that direction. Hence, the data that we have at this point is in the Polar coordinate system. For it to make sense to human eyes when visualized, it must be converted to the Cartesian or X-Y coordinate system.
So the MatLab code does just this. It gets data serially from the COM port, saves it into a matrix with the angle of rotation, and then converts it into Cartesian coordinates with the formula given above.
Once it's done, it gives an output by plotting the points on a graph. I placed the board in the box, and I got the following result.
Attachments
Step 6: Conclusion
Although the system isn't perfect, it gets the job done. It can get a rough estimate of the box width and length and sends the data accurately
The only errors that I can see at the moment are due to the shaking of the sensor while the servo is moving and faulty readings from the sensor itself. Apart from this, the system works fine and can be used for depth perception experiments as well as basic computer vision projects.

Participated in the
Robotics Contest 2016
31 Comments
3 years ago
could you please tell the hardware connections. i mean the connections of pins on the arduino ,ultrasonic sensor and servo motor
5 years ago
how it taking vertical height of the obstacle???
5 years ago
why when i start compiling it says
Error compiling for board Arduino Yún?
Thanks in advance
6 years ago
WOW! It took a little bit of figuring out, but in the end it was well worth it. Thanks!
6 years ago
Can We use Stepper motor instead
??
6 years ago
nice project ! satya
Reply 6 years ago
Thanks man!
6 years ago
Hi, i did the same project for my bachelor degree. So if you need any help you can conact me.
Just a suggestion: from the specs the angle of the US is about 15 degrees so you can speed up the scanning by increasing the the rotation angle from 1 degree to 5 or maybe 10 degree per scan. If you really want to map the environment on a robot you should add a compass module.
and remember have fun ;)
Reply 6 years ago
Thanks a lot for your suggestions! I'll be sure to implement them!
Reply 6 years ago
hi, how can i contact you. Email?
Reply 6 years ago
Private message
6 years ago
WOW, way over my head but I do enjoy seeing smart people figuring things out like this. Maybe more pictures would help though, still pretty cool, thanks. Semper Fi
Reply 6 years ago
Thanks a lot! I will certainly keep that in mind from my next Instructable :)
6 years ago
The only thing I don't understand is the wiring. Could you put a scheme of the circuit in the ible, maybe the 2nd step?
Reply 6 years ago
I'm sorry I didn't include it in the first place! I've uploaded the schamatic of the circuit I created in Fritzing
6 years ago
Congratulations for the intresting project!
As a challenge I sugest you to do the same with Python in the PC side.
Matlab is cool, but is very expensive, and you don't need it for this job ;)
Reply 6 years ago
Thank you so much!
That thought has been running through my mind ever since I actually made the project, and since then I've found a few alternatives.
- As you said, Python with an appropriate data visualization library. (Running that on an RPi and interfacing both could be fun!)
- Theres a software called SciLab which also does the same job as MatlLab for free, though I'm not sure about Serial communcication failities
- The Arduino equivalent of MatLab, called Processing. This is actually something I'm planning to use in the near future!
Reply 6 years ago
yes true, a free alternative should be used since the hardware itself is cheap too. I would suggest that i like to see more graphic pictrures. maybe You could make a multicolor height map. greetings
6 years ago
Nice project! Congrats!
A further step would be to use 2 ultrassonic sensors to make a more precise map (maybe kalman filter?)
Reply 6 years ago
Thanks so much for your insight! I'll look into the Kalmann filter, I'm looking into DSP practices or even statistical techniques to make the maps more accurate, the Kalmann filter would be a good contender.