HC-SR04 Distance Sensor

The HC-SR04 distance sensor uses ultrasonic pulses to detect objects.

Table of contents

  1. Physical Operating Principle
  2. Wiring Schematics
  3. Sample Codes
  4. Hook-up Notes for Circuit Playground
  5. Vendor Information

Physical Operating Principle

The HC-SR04 ultrasonic distance sensor infers distance to an object by measuring the time it takes an ultrasonic pulse to travel from a transmitter (speaker) to a receiver (microphone).

HC-SR04 connected to Circuit Playground

The sensor measures the time between the initiation of the the sound and the arrival of its echo. The distance is determined by dividing the one half of the time difference by the speed of sound.

where is the distance to the object, is the time of flight and is the speed of sound. The factor of 2 in the denominator is necessary because the time of flight is for the out-and-back trip of the sound pulse.

Wiring Schematics

HC-SR04 connected to Circuit Playground

Sample Codes

Hook-up Notes for Circuit Playground

The standard HC-SR04 sensor requires a 5V input for Vcc. To make this sensor (powered at 5V) compatible with a Circuit Playground, the output voltage must be stepped down to no more than 3.3V. A simple voltage divider created with two 10 kΩ resistors on the Echo pin achieves that goal. With the voltage divider the Echo output is either 0 or 2.5V, which works with the 3.3V logic.

Voltage divider hook-up for Circuit Playground

Vendor Information