Introduction: Ultrasonic 3D Maps With Python and Arduino

Picture of Ultrasonic 3D Maps With Python and Arduino

Aim:

The aim of this project is to design and develop a mechanism to compute a 3D map in a 3m range and an height factor of 1ft.

Process:

We will be using data collected from an Ultrasonic Sensor (SR04) and communicate that data back to a host system, preferably a laptop and supply the sensor data to a python script which will compute the directional data and plot a graph for an XY-plane.This process results in a 2D feature for any given plane. By adding an actuator mechanism to vary the height of the sensor assembly, we will add a Z-component to the measurements.

A stepper(or servo motor) will be used to align the sensor in a specific direction. For each step taken, the sensor will capture data and plot a 360-degree view of an XY region. Then, another motor system is initiated to vary the height while the sensor aligns back to its original position. This process when repeated across various Z-axis domains yields various XY planes. By extrapolation of each data a 3D view is generated.

Depending on our need, this data can simply be displayed or even saved for future use.

With the basic objective of the instructable made clear, let's dive head into our project!!!

Step 1: Collect All Project Materials

Picture of Collect All Project Materials

Before we can begin with exploiting the full extent of our resources, it is essential to first collect them. The following are a list of items essential for the project and all of these can easily be purchased at any online hobby store like Amazon, eBay or Flipkart.

Note : I however prefer to order my components from www.electroncomponents.com . I am not sure if they ship outside the Indian sub-continent, but one advantage I find in using this site is that my entire order is shipped as a whole rather than in parts as in case of Amazon or eBay. But in the end, it is really your choise.

Electronic Components:

  • Arduino UNO - preferably one with the aTMega328P processor. ---- 1x
  • HC-SR04 Ultrasonic Sensor ---- 2x
  • 6-wire Stepper Motor ---- 1x (A 5 wire one will also do, but it will have a different configuration methodology which I will not be providing here. I will give a link for the 6-wire model configuration)
  • Instead of a Stepper, you can use a Servo motor. I will be using a Servo motor since it is easier toconfigure and use.
  • ULN2004A - Darlington Array ---- 1x (IC to amplify and control the motor. It also prevents any potential backflow from the motor and helps keep your Arduino safe)
  • 10k POT ---- 1x
  • Jumper Wire (Male-Male and Male-Female) PS: If you want to extend connections, Female-Female wires may be needed.
  • 9V cells and battery caps.

Softwares:

  • Arduino IDE : It is a free IDE made specially for the purpose of coding and debugging codes for the Arduino family of development boards. The language used is Embedded-C which is a modification of the parent language to enable specific and targeted coding for the micro-controller. The Arduino IDE can be downloaded from here :
  • Python : We will be using Python for interpreting sensor data and for plotting the data and displaying it. I have used a Python-3.5 distribution in my instructable but you can also use Python-2.7x. At least as far as I know, there should be no backward compatibility error in this project. You can download a python distribution suiting your system specifications from www.python.org
  • matplotlib : This is specific python module which we will use to plot our data and display it. Since I am a windows user, I will specify how to get this module. Other users can refer to Python docs on how to install 3rd Party libraries for their respective systems.
    • Open Command Prompt, by pressing Start Button and typing 'cmd' in search bar. Press Enter.
    • In the cmd, type the following : > py -m pip install matplotlib
    • If you have multiple installations of python, like python3 and python2, then >py3 -m pip install matplotlib
    • This will automatically fetch matplotlib from pypi.org and save it in your system.

With that, we are ready to begin our project. Just one more thing needed. We need to design a mechanism to vary the height of observation of our sensor assembly. This can be done either manually as I prefer, or we can automate the process. For that, the following materials will be needed.

  • Ice-Cream Sticks, to make an assembly for the Sensor and to mount the Sensor on the Stepper Motor
  • Cardboard Frames to make the project structure
  • Some gears (if necessary to control the apparatus height)
  • Glue Gun

Step 2: Configuring the Stepper Motor

Picture of Configuring the Stepper Motor

One of the most tedious tasks in this Instructable is to correctly configure the stepper motor. A normal 6-wire stepper motor has its wiring like this.

http://www.piclist.com/images/member/RB-ezy-Q33/6w...

However the color scheme of the wires varies from one manufacturer to another and also some times from one motor to another. As such it is essential to find out which wires turn your motor in which direction. You can use the following link I found on youtube for configuring the Motor :

Once you have finished with this, it is time to compile all the electronics into a single package.

Alternative Used:

In the likely event that using a stepper motor becomes very hard to configure. I too faced such a problem in switched to a Servo motor. I found it easier to use and configure for my project and I suggest that you too try the project with the same.

Here is a link to setup and configure a servo motor : https://www.arduino.cc/en/reference/servo

Step 3: Develop Logic Scheme and Design Paradigm

Picture of Develop Logic Scheme and Design Paradigm

Now that we have all components ready, we have to start putting it all together. However, before we get to that, we have to ensure a logical sequence in which the project should progress. This is essential as we are working on integrating various sensors and motors into a single code, hence, for error-free execution, putting the entire project in a mock algorithm will simplify our task.

  1. Initialize all electronic components and set current state to zero.
  2. Set angle variable 'theta' to zero.
  3. for theta = 0 to theta = 360
    1. read ultrasonic sensor data
    2. send the serial data to python.
    3. store the theta and distance data in a python list( We use the cylindrical co-ordinate system)
    4. theta = theta + 1
  4. Ask user to enter z-level value.
  5. Add z-value to each co-ordinate list. Each list now has [distance, theta, z]
  6. Pause sensor system.
  7. Reset the Stepper motor back to initial state ie: -360 rotation
  8. Ask user to modify height.

Repeat the process 1-8 for 10 readings. We now have 10 lists, each list having 360 lists representing points in the plane. We now call upon the matplotlib module in python to compute the collected data and form the 3D representation of the data. Thus, we have a clear picture of how our project should proceed from here.

Note: The HC-SR04 Ultrasonic Sensor module has a range of up to 3mts with accuracy diminishing with distance to obstacle. Thus, we can expect some distortions in our data and the resulting 3D structure.

Step 4: Electronic Assembly

Stepper Motor:

The stepper motor will rotate the sensor to collect directional data. The connection scheme for the stepper motor is given in the previous section. However, there is one detail to keep in mind while connecting to the ULN2003A IC and the stepper. The connections have to be made such that wires from the same coil have to be simultaneously connected. Keep this in mind as you will have developed a number based wiring approach while configuring the stepper motor.

Ardruino UNO and HC-SR04:

Connect the trig pin of the Ultrasonic sensor to the the 7th-Digital IO pin on Arduino. Also, connect the echo pin to the 6th Pin in Arduino. The 5V pin on the sensor can be directly connected to the Arduino, or you can connect the 5V to a breadboard and make multiple connections. All these connections can be made using jumper wires.

That's it. We have connected our system and are ready to test it. Now we move on the penultimate step where we develop our code to complete the system.

Step 5: Control Codes

Arduino Code:

<p>#include<Servo.h><br>#include<Ultrasonic.h>
</p><p>//Initiate a Servo variable</p><p>Servo serv;</p><p>//Ultrasonic _identifier(trig, echo)
//We define two Ultrasonic sensors. Ensure that the Trig and Echo pins are connected correctly
Ultrasonic us1(3,4);
Ultrasonic us2(5,6);</p><p>int angle = 0;

void setup(){</p><p>	//Setup Serial Port to Communicate with Python</p><p>	

Serial.begin(9600);</p><p>	

	serv.attach(7);</p><p>}</p><p>void loop(){</p><p>	//Take 180 degree turn. The two Ultrasonic sensors will thus collect data equivalent to 360 degrees</p><p>	for(angle = 0; angle<=180; angle++){</p><p>serv.write(angle);</p><p>delay(15);</p><p>int d1, d2; //Distance from each Sensor</p><p>d1 = us1.distanceRead();</p><p>d2 = us2.distanceRead();</p><p>}</p><p>}</p>


Comments

DIY Hacks and How Tos (author)2017-09-10

I always wanted to make one of these but I never got around to figuring out the details. Thanks for sharing the plans.

Thanks... Will be completing the project soon... Stay tuned for updates.

Please tell when You can give the codes-it will be nice to have them in a week . I am 10 years old so cannot program on my own . Please give a brief diagram of the bot and tell how to connect it to a laptop or computer.

Mail to:srayanbhatta123@gmail.com

I am indian too .

About This Instructable

294views

2favorites

License:

Bio: An Electronics Engineering Student at VIT University, Vellore, I am an avid prototyper and DIY enthusiast. I really lie to work on challenging problems and ... More »
More by SanskarBiswal Auth:Ultrasonic 3D Maps With Python and Arduino
Add instructable to: