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

55 lines
1.4 KiB
Markdown
Raw Normal View History

2016-08-17 20:18:15 +02:00
# Pulse In
Returns the duration of a pulse (high or low) from a [pin](/device/pins) on
2016-11-02 01:44:37 +01:00
the @boardname@ board in microseconds.
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.
## ~
2016-08-17 20:18:15 +02:00
## Parameters
2016-08-17 20:18:15 +02:00
2017-03-16 15:57:41 +01:00
* ``name`` is a [string](/types/string) that stores the name of the pin (``P0``, ``P1``, or ``P2``, up through ``P20``)
2016-08-17 20:18:15 +02:00
* ``value`` is the value of the pulse, ``high`` or ``low``
2016-08-17 20:35:54 +02:00
* ``maxDuration``, maximum duration in micro-seconds. If no pulse is received
2016-08-17 20:18:15 +02:00
## Returns
2016-08-17 20:18:15 +02:00
2017-03-16 15:57:41 +01:00
* a [number](/types/number) that represents the pulse duration in micro-seconds
2016-08-17 20:18:15 +02:00
## Example: Measuring distance with a sonar
2016-08-17 20:18:15 +02:00
The following script sends a pulse on ``P0`` and reads the pulse returned by a HC-SR04 sonar to determine the distance of the object in front of the sensor.
```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)
})
```
### ~hint
2016-08-17 20:18:15 +02:00
This function is not supported in the simulator.
### ~
2016-08-17 20:18:15 +02:00
## See also
2016-08-17 20:18:15 +02:00
[digital write pin](/reference/pins/digital-write-pin),