Skip to content

1010Technologies/pxt-makerbit-ultrasonic

Repository files navigation

MakerBit Ultrasonic

Build Status

MakeCode extension for ultrasonic distance measurement with an HC-SR04 sensor.

MakerBit Board

The MakerBit connects to the BBC micro:bit to provide easy connections to a wide variety of sensors, actuators and other components.

http://makerbit.com/

MakerBit MakerBit+R
MakerBit MakerBit+R with motor controller

Ultrasonic

Support for an external HC-SR04 ultrasonic distance sensor to steer your robots. The blocks return immediately with the most recent value because measurements are done continuously in the background.

MakerBit connectUltrasonicDistanceSensor

Configures the ultrasonic distance sensor and measures continuously in the background. The HC-SR04 needs to be connected to a 5V source (HC-SR04 VCC) and to ground (HC-SR04 GND).

makerbit.connectUltrasonicDistanceSensor(DigitalPin.P5, DigitalPin.P8)

MakerBit onUltrasonicObjectDetected

Do something when an object is detected within a range.

makerbit.onUltrasonicObjectDetected(10, DistanceUnit.CM, () => {})

MakerBit getUltrasonicDistance

Returns the distance to an object in a range from 1 to 300 centimeters or up to 118 inch. The maximum value is returned to indicate when no object was detected. -1 is returned when the device is not connected.

makerbit.getUltrasonicDistance(DistanceUnit.CM)

MakerBit isUltrasonicDistanceLessThan

Returns true if an object is within the specified distance. false otherwise.

makerbit.isUltrasonicDistanceLessThan(10, DistanceUnit.CM)

Ultrasonic Example: Distance Graph

let distance = 0
makerbit.connectUltrasonic(DigitalPin.P5, DigitalPin.P8)
basic.forever(() => {
    distance = makerbit.getUltrasonicDistance(DistanceUnit.CM)
    led.plotBarGraph(distance, 0)
})

License

Licensed under the MIT License (MIT). See LICENSE file for more details.

Supported targets

  • for PXT/microbit
  • for PXT/calliope