Sonar Reading Lillie and Kim

This test plan will give you instructions on how to connect a sonar reader to a breadboard, calibrate the values, and program it so you can determine whether a car is speeding away from the sonar.

Step 1: Gathering Your Materials

For our test plan you will need:

  • One sonar sensor
  • One breadboard
  • One resistor
  • One LED
  • One toy car
  • One ruler
  • One arduino
  • Six jumper wires

Step 2: Connecting Sonar and LED

Follow the diagram above to connect the LED and the sonar sensor to the breadboard. As you configure the setup you will notice that there are four different variables/values on the sonar sensor. When connecting the sonar to the breadboard make sure that VCC is connected to the 5V, Trig is connected to pin 9, Echo is connected to pin 10, and GND is connected to ground.

Although we will not be dealing with the LED for the next few steps, make sure that the LED is connected to pin 4.

Step 3: Programming Sonar Sensor

In this step you will program the sonar sensor so it can read and print values.

At the beginning of your code

  • Initialize pin 9 and 10.
  • Set the sensor value as 0 so the printed values are accurate.

In void setup

  • Set pin 9 as the OUTPUT and set pin 10 as the INPUT. This can be achieved by using the variable pinMode.
  • Put Serial.begin (9600). This tells the sonar how often to collect data.

In void loop

  • Use the variable digitalWrite to make the OUTPUT pin send out a low signal.
  • Make the OUTPUT signal last for 4 microseconds using the variable delayMicroseconds().
  • Then make the OUTPUT pin send out a high signal.
  • Make the OUTPUT signal last for 10 microseconds.
  • Put duration= pulseIn(10, HIGH)
  • Place Serial.println (duration) so the sonar sensor prints the values in the serial monitor.

Step 4: Record Calibration Data

In order to calibrate the data you will need to take several data points. Start by opening serial monitor in arduino and getting a ruler. Find the duration when the distance is 3 in, 6 in, 9 in, and 12 in. Make sure you write the data down because you will need to reference this in step 5.

Above is the data we received when we completed this step.

Step 5: Creating Calibration Curve

After collecting the data in step 4, open up Logger Pro. In logger pro input your collected data in the data table. Make sure to input values for duration in the x -axis and values for distance in the y-axis. After plugging in the data you should notice that a graph with several data points has been created. In order to find the line of best fit go to the tool bar and press linear fit. Write down the equation for the line of best fit.

Step 6: Code to Calibrate Data

Now that you have found the equation for the line of best fit in Logger Pro, you have to update the code. Before updating code remove the Serial.println(duration) code- you will no longer need it.

In void loop

  • Enter this code: int distance= (your value for m * duration) - your value for b.
  • Place Serial.println (distance) to make the sonar sensor print the values for distance.
  • Make the sonar range reading delay for 1 second.

Step 7: Testing Data

In this step we will use the LED to test when a car is moving away from the sonar range finder.

At the beginning of your code

  • Initialize the LED using the variable int ledPin. Because the LED is in pin 4, make sure int ledPin= 4.
  • Create a limit that equals 10, label it Limit_1.
  • Create a limit that equals 9, label it Limit_2.

In void setup

  • Set the ledPin as an OUTPUT using variable pinMode.

In void loop

  • Make the LED turn on when distance is greater than or equal to Limit_1.
  • Make the LED turn off when distance is less or equal to than Limit_2.

You should notice that as the car gets closer to sonar the LED remains off but as it gets further away the LED turns on.



    • Faux-Real Contest

      Faux-Real Contest
    • Paper Contest

      Paper Contest
    • Build a Tool Contest

      Build a Tool Contest



    Nice first Instructable. Thanks for sharing with the community.