Classroom Public page

Week 12: Sensors Deep

540 words

The HC-SR04 ultrasonic distance sensor. By the end of the week you have a sketch that measures distance in centimeters and triggers an action when something gets closer than a threshold. The pattern transfers to every other sensor; you generalize from this one example.


Reading (~45 min)

  • The HC-SR04 datasheet. Read the "timing diagram" section carefully; the sensor's protocol is to receive a 10 μs pulse on its trigger pin, then emit a pulse on its echo pin proportional to the round-trip time of an ultrasonic ping
  • Optional: the "speed of sound" Wikipedia page. The HC-SR04 assumes ~343 m/s; correct for temperature if you want precision

Lecture (~1.5 hr)

  • What an ultrasonic sensor does. Emits a high-frequency sound pulse (40 kHz; above human hearing); waits for the echo from any nearby object; reports the round-trip time
  • Distance from time. Speed of sound × time = distance traveled. Round-trip means you divide by 2. At 343 m/s, every microsecond of travel is ~0.343 mm; every centimeter is ~58.3 μs of round-trip time
  • The HC-SR04 protocol. You pulse the trigger pin HIGH for 10 μs; the sensor sends a ping; the echo pin goes HIGH and stays high for the duration of the round trip. You measure the high time with pulseIn()
  • pulseIn(pin, state, timeout). Returns the duration in microseconds of the next pulse on the named pin in the named state. Timeout prevents waiting forever if no echo arrives
  • Generalizing the pattern. Most sensors have a similar protocol: send a command to begin a reading; wait for the result; convert the result to a real-world quantity. Specific protocols vary; this pattern is universal

Lab exercises (~2 hr)

Lab 12.1: Ultrasonic. Wire the HC-SR04. Read distance in centimeters. Print to Serial. Trigger a buzzer or LED when distance drops below 20 cm. ~90 minutes.

Independent practice (~3 hr)

  • Calibrate your distance reading against a ruler. Place objects at known distances; record what the sensor returns; tabulate. Note the accuracy and the failure modes (very close, very far, oblique surfaces)
  • Combine the ultrasonic sensor with PWM-controlled LED: brighter when something is closer; dimmer when farther. Map distance (0 to ~200 cm) to PWM (255 to 0)
  • If your kit has a different sensor (temperature, humidity, motion, etc.) read its datasheet and write a sketch that reads it. The HC-SR04 was an example; your skill is in generalizing the example

Reflection prompts

  1. The HC-SR04 protocol uses pulseIn, a blocking call that waits for an echo. While the Arduino is waiting, it cannot do anything else. When would this matter, and how would you work around it?
  2. Speed of sound varies with temperature (~0.6 m/s per °C). For HW-101 precision needs, the variation does not matter. When would it?
  3. Your code converts microseconds to centimeters using a hardcoded constant. What other constants are hidden in the sketches you have written so far?

What's next

Week 13 is capstone scoping. You pick an applied project. Most students will use sensors + actuators + the Arduino R4 to build something tangible: an alarm, a mood lamp, a sensor logger, a motion-triggered toy. The 11 weeks of skills converge on one personal artifact.