1.4 KiB
1.4 KiB
pulse In
Get the duration, in microseconds, of a pulse (high or low) from one of the pins.
pins.pulseIn(DigitalPin.P0, PulseValue.High)
~avatar
Some pins are also used by the LED screen. Please read the page about pins carefully.
~
~ hint
Simulator: This function needs real hardware to work with. It's not supported in the simulator.
~
Parameters
namethe name of the pin (P0,P1, orP2, up throughP20).valuethe value of the pulse, eitherhighorlow.maxDuration, maximum duration to wait for the pulse in microseconds. If no pulse is received, the duration returned is0.
Returns
- a number that is the pulse duration in microseconds.
Example: Measuring distance with a sonar
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.
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)
})