pxt-calliope/docs/reference/pins/pulse-in.md

54 lines
1.4 KiB
Markdown
Raw Permalink Normal View History

# pulse In
2016-08-17 20:18:15 +02:00
Get the duration, in microseconds, of a pulse (high or low) from one of the pins.
2016-08-17 20:18:15 +02:00
```sig
pins.pulseIn(DigitalPin.P0, PulseValue.High)
```
## ~avatar
2016-08-17 20:18:15 +02:00
Some pins are also used by the [LED screen](/device/screen).
Please read the [page about pins](/device/pins) carefully.
## ~
### ~ hint
**Simulator**: This function needs real hardware to work with. It's not supported in the simulator.
2016-08-17 20:18:15 +02:00
### ~
## Parameters
2016-08-17 20:18:15 +02:00
* ``name`` the name of the pin (``P0``, ``P1``, or ``P2``, up through ``P20``).
* ``value`` the value of the pulse, either ``high`` or ``low``.
* ``maxDuration``, maximum duration to wait for the pulse in microseconds. If no pulse is received, the duration returned is `0`.
2016-08-17 20:18:15 +02:00
## Returns
2016-08-17 20:18:15 +02:00
* a [number](/types/number) that is the pulse duration in microseconds.
2016-08-17 20:18:15 +02:00
## Example: Measuring distance with a sonar
2016-08-17 20:18:15 +02:00
Send a pulse on ``P0`` and read a pulse returned by a HC-SR04 sonar ultrasonic sensor. The sensor determines the distance of the object in front of it.
2016-08-17 20:18:15 +02:00
```blocks
basic.forever(() => {
// send pulse
pins.digitalWritePin(DigitalPin.P0, 0)
control.waitMicros(2)
pins.digitalWritePin(DigitalPin.P0, 1)
control.waitMicros(10)
pins.digitalWritePin(DigitalPin.P0, 0)
// read pulse
led.plotBarGraph(pins.pulseIn(DigitalPin.P1, PulseValue.High) / 58, 0)
basic.pause(100)
})
```
## See also
2016-08-17 20:18:15 +02:00
[digital write pin](/reference/pins/digital-write-pin)