Ultrasonic Sensor Overview:
The way in which a ultrasonic sensor, such as the Parallax 28015, functions is as follows. First, an external microcontroller will apply a pulse to the signal pin of the sensor to begin a measurement. The sensor will then emit an ultrasonic burst. Once the burst is sent, the sensor will listen for the burst's echo as well as start keeping track of the length of time between when the burst was emitted and when the echo is received, which is also known as the time of flight. Once the echo is received, the sensor will output a pulse on the signal pin.The length of this pulse corresponds to the burst's time of flight. Once the length of the output pulse is obtained, the microcontroller can do a simple calculation to determine the distance measurement.
The calculation to convert the sensor's output pulse length into distance is as follows.
- First, the fact that the speed of sound in air varies with the air's temperature needs to be accounted for. The equation for this is: Speed of Sound in Air = 331.5 + (0.6 * Air Temperature ) m/s, where the air temperature is measured in degrees Celsius. For room temperature (21 degrees), the speed of sound should be 344.1 m/s.
- The equation for measuring the distance is: Distance = (Time of Flight * Speed of Sound in Air)/2. The 28015's datasheet states that the smallest that the output pulse can be is 115 us and that the largest it can be is 18500 us. Using 344.1 m/s as the value for speed of sound, we can calculate the range of our measurable distances to be roughly 0.027m (0.85 ft) to 3.18m (10.4 ft). Any objects outside of this range cannot be accurately measured.
Some Advantages of using a Ultrasonic Sensor:
- They provide a simple and convenient way to measure the distance between two objects without needing to physically touch either object.
- They typically have a much greater range than an infrared distance sensor.
Some Disadvantages of using a Ultrasonic Sensor:
- As noted above, the speed of sound in air varies with temperature so the microcontroller attached to the sensor will typically need some method for correcting for these variations.
- They will not be able to detect surfaces that angled in such a way that the reflected signal doesn't hit the sensor. They also have difficulty detecting objects comprised of soft materials which do not reflect sound well.
The normal way to interface with a three wire ultrasonic sensor is to use a single gpio pin and a timer to record the length of the output pulse.
Take the circuit and code shown below:
This implementation is very simple. The processor drives the Sonic_Sensor pin high to start the sensor and then drives it low to and waits for the return pulse. Once the return pulse is detected, the processor starts the timer. Once the return pulse ends, the processor reads the value from the timer and uses it to compute the distance. The processor then waits for the timer to run down and the process starts again.
This method is the typical way for interfacing with such a sensor and pretty much any microcontroller can perform this operation. The section below demonstrates another way to achieve the same functionality using a the PSOC 5LP's Universal Digital Blocks (UDBs), a feature unique to Cypress's PSOC platform.
Univeral Digital Block (UDB) Based Approach:
Instead of using the simple timer based a approach shown above, we can use the PSOC's UDB's to create a circuit that will accomplish the same task.
Take the circuit shown below:
The function of each circuit component is as follows:
- Sonic Sensor Pin: Bidirectional IO pin used to send the start pulse to the ultrasonic sensor and receive the return pulse.
- Enable Pin: IO Pin used to enable the flip-flop and counter
- Counter: Used to record the length of the return pulse.
- Toggle Flip-Flop: Converts the output pulse from the ultrasonic sensor to a pulse train which increments the counter.
- AND gate: Ensures the flip-flop is only incremented when the Enable line is high.
- NOT gate: Resets the counter when the Enable line toggles low.
- Clock_1: Clock that drives the counter, runs at twice the frequency of Clock_2
- Clock_2: Clock that drives the flip-flop, runs at half the frequency of Clock_1
- Sync_1: Synchronizes Clock_1 and Clock_2 i.e, ensures that Clock_1 and Clock_2 have the same phase.
- LED: Used for indication.
The process of the circuit recording a distance measurement is as follows:
- The processor drives the Sonic_Sensor Pin high for 10 microseconds to start the ultrasonic sensor.
- Once 10 microseconds have passed, the processor instructs the Sonic_Sensor pin to act as a input in order to receive the return pulse.
- The Enable pin is then driven high to enable the flip-flop and counter.
- The output pulse from the ultrasonic sensor will drive Sonic_Sensor pin high. Once the input of the flip-flop is high, its output will toggle between high and low states at a rate of 1 MHz (half it's clock frequency). This effectively means that there will be one count for every microsecond that the output pulse lasts.
- Once the output pulse stops, the output of the flip-flop will stay high and thus the counter will stop incrementing. The processor can now read the counter to obtain the distance measurement.
- Once the measurement is read, the processor then drives the Enable pin low to reset the counter.
The code needed to perform this procedure can be found below, the full psoc creator project can be found in the Example Projects section of this article.
The workspace below contains PSOC Creator projects for both the conventional and UDB based approaches shown above:
Contacting the Author:
Hopefully this article showed you how to interface a three ultrasonic sensor to a PSOC as well as gave you some insight into the flexibility that PSOCs provide. If you have any comments or questions, feel free to email at EEwiki@digikey.com