# Distance Sensor Instructable

766

6

1

The goal of this test plan is to see if we can measure whether the door is open or not. This test plan will give you instructions on how to build a simple distance sensor, calibrate it, program it, and then use it to see if you can detect if a door is open or not.

## Step 1: Gather Your Materials

First you need to gather all your necessary materials.

For this circuit you will need:

Arduino Uno microcontroller
USB cable (to connect the Arduino to the computer)

Beaker Water

Ruler

Ultrasonic Sensor

Images:

## Step 2: Connecting Your Circuit

The next step is to begin constructing the circuit that will allow you to measure distance with your ultrasonic sensor.

Follow the diagram above to connect your ultrasonic sensor to your Arduino in a way that will allow you to measure temperature. As you can see, the 5V output of your Arduino is connected to Vcc on the thermsistor. Trig and Echo connect to pins 9 and 10 respectively. Finally GND connects to GND or ground on the Arduino.

## Step 3: Programming Your Arduino

The next step is to program your Arduino so that you can start taking measurements of the voltage across your thermistor. To do so, copy the code above into your editor and then upload it onto your Arduino.

This code will take a reading from your untrasonic sensor one time per second, and will write that reading on the serial monitor. SO REMEMBER TO OPEN THE SERIAL MONITOR! Remember: the values that will be written on the serial monitor here are voltage values. In order to produce temperature values, we will need to calibrate the device.

## Step 4: Recording Your Calibration Data

Right now, your Arduino is not producing distance values. We need to calibrate it, which means taking a series of voltage measurements with the Arduino at various distances, while simultaneously recording the distances at each voltage measurement. In this way, we can create a chart that has voltage values on the left and distances on the right. From this chart we will be able to come up with an equation that will allow us to automatically convert between volts and degrees.

In order to take your calibration data, you need to take a ruler and place the the ultrasonic sensor a set distance from a object, such as your hand. Then record the value that appears. Repeat this process each time moving the sensor 1-2 inches farther away from the object until you get 10-15 values. Then enter the data into Logger Pro or Excel as shown above.

## Step 5: Creating Your Calibration Curve

Now that all of your data is in Excel or Logger Pro, we will use it to create a calibration curve and generate an equation that will allow us to convert between voltage and temperature values.

In Excel, highlight your data (make sure the voltage values are on the left) and select "Insert" on the toolbar at the top, then click "Scatter or Bubble Chart" from the Charts section. A graph should pop up with a series of dots on it. Double-check that the Y-axis represents temperature values and the X-axis represents voltage values. Right-click on one of the data points and select "Format Trendline". A dialogue box will appear. Under "Trendline options", select "Linear", and then at the bottom select the box that says "Display Equation on chart". Write down that equation, as that is what you are going to program into your Arduino to make it convert voltage to temperature automatically.

For Logger Pro, select the part of the graph you want to take the equation of and click analyze on the top tool bar. Then scroll down and select linear fit and then write the equation that appears.

## Step 6: Calibrating Your System

Now that you have successfully created a calibration curve and derived the equation that allows you to convert voltage values to distances, you must update your code so that your Arduino prints temperature values to the serial monitor.

Go back into your Arduino code and make the following changes:

Fist comment out the line Serial.println(duration);

Then under it add the following code:

float inches= (duration/X);
delay(100); Serial.println(inches);

X = The slope of your calibration curve

## Step 7: Testing Your Device

You have now constructed a distance sensor that can measure distance using a Ultrasonic Sensor and an Arduino. Now you must test it for accuracy.

To do this, modify your circuit so that it includes an led that is connected to pin 10.

void setup (){

pinmode (11, OUTPUT);

}

void loop (){

if(inches>11){
digitalWrite(11,HIGH); }else{ digitalWrite(11,LOW);

}

}

Next set up the ultrasonic sensor so that its 11 inches from the door when its closed. Then open the door. If the LED lights up then you have successfully calibrated your distance sensor.