NAME(S)  ____________________________________________________________________

CS 341 – Lab 7

Computer Architecture and Organization

Measuring Distance and Velocity

Equipment:  Arduino UNO microcomputer, PC with Arduino IDE installed, and a USB cable.

Parallax PING))) Ultrasonic Distance Sensor

Your assignment is to connect an ultrasonic distance sensor to the Arduino board.  Disconnect the Arduino board from the USB port.  Connect the appropriate wires to the Arduino UNO via the breadboard.  NOTE: When you are adding, changing, or removing wiring on a prototype connected to the Arduino UNO board, always disconnect the power from the USB port and check your wiring carefully before reconnecting it to the USB port.  Otherwise, you may damage the Arduino board. If you have any doubts, show your wiring to the TA before reconnecting it to the USB port.

Study the following picture for connecting the PING))) sensor to the Arduino processor.

Open the Arduino.exe program.  Write the code for the sketch to send a pulse to the PING sensor and receive a pulse width modulated (analog) value for the duration of the round trip delay.  The duration represents the round trip delay to the target in microseconds.

Define global variables for the distance, the old distance, and the velocity.

In the setup function for this sketch, you need to configure the serial port for 9600 bps.

In the loop function for this sketch, you need to update the value of the old distance variable from the distance variable, use a call to the ping function provided below to get a new distance value, calculate the velocity from the difference between the current and old distance values, and print both values in a short line.  You need to use a short printout line to avoid affecting the timing of the sampling such as the following simple format:

D: 13 / V: 4

D: 13 / V: 0

D: 14 / V: 2

D: 20 / V: 12

D: 22 / V: 4

D: 21 / V: -2

D: 21 / V: 0

D: 21 / V: 0

D: 25 / V: 8

D: 16 / V: -18

D: 11 / V: -10

D: 11 / V: 0

int ping(int pingPin)

{

int duration;                      /* duration of round trip pulse in microseconds */

pinMode(pingPin, OUTPUT);          /* set pingPin for output first */

digitalWrite(pingPin, LOW);        /* write a LOW value for a short time */

delayMicroseconds(2);

digitalWrite(pingPin, HIGH);       /* send a pulse at least 5 microseconds in length */

delayMicroseconds(5);

digitalWrite(pingPin, LOW);        /* end the pulse */

pinMode(pingPin, INPUT);           /* set the pingPin for input */

duration = pulseIn(pingPin, HIGH); /* and get round trip duration in microseconds */

/* sound velocity is 29 microseconds per centimeter */

/* distance is half of the round trip */

return duration / 29 / 2;          /* convert to centimeters */

}

At the end of the loop function, include a delay call for 1 second (1000 microseconds) to set the sampling rate.

Connect the PC to the Arduino UNO board using the USB cable.  Download the sketch and open the Serial Monitor window.  Hold a “target” (book, piece of cardboard, etc. at a known distance from the sensor.  You should see a series of your printouts with values of the distance to the target and its velocity.  Experiment with the distance from the sensor to the target and observe to ensure proper operation and calibration.

Think about and explain in your report how you could add a calculation for the acceleration to the code for the above sketch.  If you have time, try it.

As a team, write your lab report to explain what you did, how you did it, and what you learned about interfacing hardware to a microprocessor and its software (the “sketch”).  Turn in your report including a copy of your team’s final “sketch” at your next lab session.

___ / 10