> ## Documentation Index
> Fetch the complete documentation index at: https://docs.roboconoxon.org.uk/llms.txt
> Use this file to discover all available pages before exploring further.

# Ultrasonic Sensor

Ultrasonic sensors can be used for calculating the distance from a sensor to the nearest solid object.

They do this through emitting a pulse of ultrasonic sound and recording the time taken for the echo to return.

We can control ultrasonic sensors through GPIO on the Brain Box.

## Python

You can create an ultrasonic sensor through creating an `UltrasonicSensor` object.

The arguments to the initialisation function are first the trigger pin, followed by the echo pin. For example this configures a sensor with trigger on GPIO0 and echo on GPIO1.

```python theme={null}
sensor = robot.UltrasonicSensor(R.gpio[0], R.gpio[1])
```

We can then read the value of this sensor through calling the read() function which returns a distance in meters.

```python theme={null}
distance = sensor.read()
```

Here's a complete example:

```python theme={null}
import robot

r = robot.Robot()

sensor = robot.UltrasonicSensor(r.gpio[0], r.gpio[1])
distance = sensor.read()
```

### I want to use more than one ultrasound sensor but don't have enough GPIO

The BrainBox can support (theoretically) up to 7 ultrasonic sensors. We do this through daisy-chaining the echo pin of one sensor to the trigger pin of another.

For example for two sensors, sensor A has trigger pin GPIO0, and echo GPIO1, and sensor B has trigger pin GPIO1 and echo GPIO2.

You can create an ultrasonic sensor combining these two physical sensors, by listing the echo pins in order.

```python theme={null}
sensors = robot.UltrasonicSensor(r.gpio[0], r.gpio[1], r.gpio[2])
```

> [!WARNING]
>
> If the echo pins are listed in the wrong order incorrect values may be returned


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.