##### Child pages
• Interfacing a Three Wire Ultrasonic Sensor to the PSOC 5LP
Skip to end of banner
Go to start of banner

# Interfacing a Three Wire Ultrasonic Sensor to the PSOC 5LP

Skip to end of metadata
Go to start of metadata

## Required Hardware:

PSOC 5LP prototyping kit

Three wire ultrasonic sensor, for this article I used the Parallax 28015

## 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.

1. 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.
2. 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.

## Conventional Approach:

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:

Conventional Approach Main.c
```/* ========================================
*
* Copyright YOUR COMPANY, THE YEAR
* All Rights Reserved
* UNPUBLISHED, LICENSED SOFTWARE.
*
* CONFIDENTIAL AND PROPRIETARY INFORMATION
* WHICH IS THE PROPERTY OF your company.
*
* ========================================
*/
#include <project.h>
uint16 counts = 0;
float  feet   = 0;
uint16 i      = 0;
int main()
{
CyGlobalIntEnable;

for(;;)
{
//Timer_1_Init();  //Initiate timer
Pin_1_Write(1);
CyDelayUs(10);
Pin_1_Write(0);

while(Pin_1_Read() != 1) //wait until return pulse starts
{

}

Timer_1_Start();

while(Pin_1_Read() != 0) //wait unit return pulse stops
{

}

counts = 0xFFFF-Timer_1_ReadCounter();

feet = counts * 0.00056325; //Convert pulse width to feet, to convert to meters, multiply by 0.0001717 instead.

if (feet < 1.0 )
{

LED_Write(1);

}

else
{

LED_Write(0);
}

CyDelay(100);
Timer_1_Stop();

}
}

/* [] END OF FILE */

```

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:

1. The processor drives the Sonic_Sensor Pin high for 10 microseconds to start the ultrasonic sensor.
2. Once 10 microseconds have passed, the processor instructs the Sonic_Sensor pin to act as a input in order to receive the return pulse.
3. The Enable pin is then driven high to enable the flip-flop and counter.
4. 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.
5. 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.
6. 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.

UDB Approach Main.c
```/* ========================================
*
* Copyright YOUR COMPANY, THE YEAR
* All Rights Reserved
* UNPUBLISHED, LICENSED SOFTWARE.
*
* CONFIDENTIAL AND PROPRIETARY INFORMATION
* WHICH IS THE PROPERTY OF your company.
*
* ========================================
*/
#include <project.h>
int main()
{
CyGlobalIntEnable; /* Enable global interrupts. */
Counter_Start();
for(;;)
{
uint16 counts = 0; // value to store results from the counter
float  feet = 0;

Enable_Write(0);
Sonic_Sensor_Write(1);   //Output the sensor pulse

CyDelayUs(10); //wait for the sensor pulse to finish

Sonic_Sensor_Write(0); //shut off the sensor pulse
Enable_Write(1);

Sonic_Sensor_Read(); //receive return pulse
CyDelay(50); // wait for the maximum value of the return pulse

counts = Counter_ReadCounter();// read counter value
feet = counts * 0.00056325; //convert pulse width to feet, to convert to meters, multiply by 0.0001717 instead.
CyDelayUs(10);

if (feet < 1.0 )
{

LED_Write(1);

}

else
{

LED_Write(0);
}

Enable_Write(0);// reset the counter

}

}
/* [] END OF FILE */

```

## Example Projects:

The workspace below contains PSOC Creator projects for both the conventional and UDB based approaches shown above:

PSOC_Ultrasonic.zip

## 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

• No labels