Ultrasonic STEM Component Pack
Details
Summary
The Ultrasonic STEM Component Pack bundles an HC-SR04 ultrasonic sensor, SG90 micro-servo, mounting hardware & servo horns, 2.54 mm jumper cables, and a printed instruction booklet. Build a scanning rangefinder for robots and STEM projects.
Tip: Power the SG90 from a separate 5–6 V rail and tie grounds to your controller for stable readings.
Box Contents›
- Ultrasonic Sensor Module (HC-SR04)
- SG90 9g Micro-Servo + assorted servo horns
- Mounting hardware (standoffs/screws) + cable tie
- 2.54 mm jumper wires (multi-colour)
- Instruction Booklet (PDF + print)
Quick Wiring›
- HC-SR04: VCC→5V, GND→GND, TRIG→D6, ECHO→D5 (Arduino example)
- SG90: Red→5–6V, Brown/Black→GND, Orange/Yellow→D9 (signal)
- Raspberry Pi: Level-shift or divide ECHO down to 3.3 V
Route wires to avoid fouling the servo horn; pause ~150–200 ms after each move before pinging.
Assembly & Wiring Diagram›
Mount HC-SR04 to the bracket, attach the bracket to the SG90 horn, and connect as shown.
Arduino Code›
Scan + Distance (SG90 + HC-SR04)
#include <Servo.h>
Servo pan;
const int SERVO_PIN=9, TRIG=6, ECHO=5;
long pingUS(){
digitalWrite(TRIG, LOW); delayMicroseconds(3);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
return pulseIn(ECHO, HIGH, 30000UL); // 30 ms timeout
}
void setup(){
Serial.begin(115200);
pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
pan.attach(SERVO_PIN); pan.write(90); delay(500);
}
void loop(){
for(int a=30; a<=150; a+=5){
pan.write(a); delay(180);
long us = pingUS();
float cm = us ? us/58.0 : -1;
Serial.print("A:");Serial.print(a); Serial.print(" cm:");Serial.println(cm,1);
}
for(int a=150; a>=30; a-=5){
pan.write(a); delay(180);
long us = pingUS();
float cm = us ? us/58.0 : -1;
Serial.print("A:");Serial.print(a); Serial.print(" cm:");Serial.println(cm,1);
}
}
HC-SR04: Minimal Test
// TRIG→D6, ECHO→D5
void setup(){ Serial.begin(115200); pinMode(6,OUTPUT); pinMode(5,INPUT); }
void loop(){
digitalWrite(6,LOW); delayMicroseconds(3);
digitalWrite(6,HIGH); delayMicroseconds(10); digitalWrite(6,LOW);
long us=pulseIn(5,HIGH,30000UL);
if(us){ Serial.print(us/58.0); Serial.println(" cm"); } else { Serial.println("No echo"); }
delay(200);
}
Raspberry Pi›
Use a divider/level shifter on ECHO. pigpio recommended for precise timing and servo pulses.
pigpio: Scan + Distance
# sudo apt install pigpio python3-pigpio
# sudo systemctl start pigpiod
import time, pigpio
PIN_SERVO=18; TRIG=23; ECHO=24
pi = pigpio.pi()
pi.set_mode(TRIG, pigpio.OUTPUT); pi.set_mode(ECHO, pigpio.INPUT)
def set_servo_deg(deg): pi.set_servo_pulsewidth(PIN_SERVO, int(500 + (max(0,min(180,deg))/180.0)*2000))
def ping_us():
pi.gpio_trigger(TRIG, 10, 1)
t0=time.time()
while pi.read(ECHO)==0:
if time.time()-t0>0.03: return 0
start=time.time()
while pi.read(ECHO)==1:
if time.time()-start>0.03: return 0
return (time.time()-start)*1e6
try:
for a in list(range(30,151,5))+list(range(150,29,-5)):
set_servo_deg(a); time.sleep(0.18)
us=ping_us(); print(f"A:{a} cm:{(us/58.0):.1f}" if us else f"A:{a} No echo")
finally:
pi.set_servo_pulsewidth(PIN_SERVO,0); pi.stop()
Espressif (ESP32 / ESP8266)›
- Voltage: ESP uses 3.3 V logic. Keep ECHO ≤3.3 V (divider/level shifter) if HC-SR04 is at 5 V.
-
Servo power: Drive SG90 from 5–6 V with shared GND. Prefer ESP32
ledcPWM for smooth motion.
ESP32 (Arduino Core): HC-SR04 + Servo Scan
#include <Servo.h>
Servo pan;
const int SERVO_PIN=18, TRIG=22, ECHO=23; // adjust pins
long pingUS(){
digitalWrite(TRIG, LOW); delayMicroseconds(3);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
return pulseIn(ECHO, HIGH, 30000UL);
}
void setup(){
Serial.begin(115200);
pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
pan.attach(SERVO_PIN); pan.write(90); delay(500);
}
void loop(){
for(int a=30; a<=150; a+=5){
pan.write(a); delay(180);
long us = pingUS(); float cm = us ? us/58.0 : -1;
Serial.printf("A:%d cm:%.1f\n", a, cm);
}
for(int a=150; a>=30; a-=5){
pan.write(a); delay(180);
long us = pingUS(); float cm = us ? us/58.0 : -1;
Serial.printf("A:%d cm:%.1f\n", a, cm);
}
}
ESP8266 (Arduino Core): Minimal HC-SR04
// TRIG->D6, ECHO->D5 (with divider to 3.3V)
void setup(){ Serial.begin(115200); pinMode(D6,OUTPUT); pinMode(D5,INPUT); }
void loop(){
digitalWrite(D6,LOW); delayMicroseconds(3);
digitalWrite(D6,HIGH); delayMicroseconds(10); digitalWrite(D6,LOW);
long us=pulseIn(D5,HIGH,30000UL);
if(us){ Serial.printf("%.1f cm\n", us/58.0); } else { Serial.println("No echo"); }
delay(200);
}
ESP32: ledc PWM for SG90 (snippet)
// Use hardware PWM for stable servo pulses
const int SERVO_PIN=18, CH=0, FREQ=50, RES=16;
void setup(){
ledcSetup(CH, FREQ, RES);
ledcAttachPin(SERVO_PIN, CH);
}
void writeDeg(int deg){
deg = constrain(deg, 0, 180);
int us = 500 + (deg * 2000) / 180; // 500–2500 µs
int duty = map(us, 0, 20000, 0, 65535); // 20ms period -> 65535 @ 16-bit
ledcWrite(CH, duty);
}
Notes: Keep cables clear of the servo sweep. Average multiple pings per angle if you need a smoother scan.
Drivers›
No PC drivers needed for the sensors/servo. If you use a USB-serial bridge during development:
Resources›
Specifications›
| Sensor | HC-SR04 (5 V, TRIG/ECHO, ~2–400 cm) |
|---|---|
| Servo | SG90 (4.8–6 V, ~180° nominal travel) |
| Cables | 2.54 mm Dupont jumpers, multi-colour |
| Hardware | Bracket/standoffs/screws + servo horns |
| Guide | Quick-start & safety booklet (PDF + print) |
Note: Exact hardware mix may vary slightly by batch.
FAQ›
No distance readings / “No echo” ›
Servo jitters while scanning ›
Using with Raspberry Pi (3.3 V) ›
Safety & Compliance›
Important Notice For educational, prototype, experimental, laboratory and R&D use only. (non-RoHS)
WEEE
Not subject to WEEE marking; obligations apply to final equipment.
Manufacturer
Little Muffins Things Limited
166 River Heights, 90 High Street
London E15 2GQ
littlemuffinsthings.com
EU Responsible Person
eucomply OU
Parnu mnt. 139b–141
13117 Tallinn, Estonia
hello@eucompliancepartner.com