Sensor-Servo Bracket Fits HC-SR04 + SG90

Details

Sensor-Servo Bracket — fits HC-SR04 ultrasonic sensor and SG90 micro servo

Sensor-Servo Bracket — Fits HC-SR04 + SG90

This compact Sensor-Servo Bracket mounts an HC-SR04 ultrasonic sensor onto an SG90 9g micro-servo to create a pan-scan distance module for robots and turrets. Sweep left-to-right for mapping, obstacle avoidance, or room scanning.

Tip: For clean readings, pause briefly after each servo move before triggering the ultrasonic ping.

Mechanical & Mounting
  • Fits Sensor: HC-SR04 (standard 45×20 mm face with 2 transducers)
  • Fits Servo: SG90 (horn + screws)
  • Assembly: Attach HC-SR04 to bracket face (screws/nuts); fix bracket to SG90 horn; secure horn to servo.
  • Orientation: Keep transducers level; route wires to avoid fouling the horn.
  • Standoffs: Use spacers if the sensor PCB touches horn screws.

Use thread-locker sparingly on metal screws. Do not overtighten into plastic.

Assembly & Wiring Diagram

Power the SG90 from a separate 5–6 V rail. HC-SR04 runs at 5 V; add a divider on ECHO for 3.3 V MCUs.

Bracket assembly with HC-SR04 on SG90; separate servo power; TRIG/ECHO wiring
Arduino Code

Scan with 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);
  }
}

Note: Adjust sweep limits (30–150°) to avoid cable strain and mechanical stops.

Raspberry Pi

Use pigpio for stable servo pulses and precise echo timing. Level-shift HC-SR04 ECHO to 3.3 V.

Scan with pigpio

# sudo apt install pigpio python3-pigpio
# sudo systemctl start pigpiod
import time, pigpio

PIN_SERVO=18   # hardware PWM
PIN_TRIG=23
PIN_ECHO=24

pi = pigpio.pi()
pi.set_mode(PIN_TRIG, pigpio.OUTPUT)
pi.set_mode(PIN_ECHO, pigpio.INPUT)

def set_servo_deg(deg):
    deg = max(0, min(180, deg))
    us = int(500 + (deg/180.0)*2000)  # 500–2500 µs
    pi.set_servo_pulsewidth(PIN_SERVO, us)

def ping_us():
    pi.gpio_trigger(PIN_TRIG, 10, 1)
    t0 = time.time()
    while pi.read(PIN_ECHO)==0:
        if time.time()-t0>0.03: return 0
    start = time.time()
    while pi.read(PIN_ECHO)==1:
        if time.time()-start>0.03: return 0
    end = time.time()
    return (end-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()
        cm = us/58.0 if us else -1
        print(f"A:{a}  cm:{cm:.1f}")
finally:
    pi.set_servo_pulsewidth(PIN_SERVO, 0); pi.stop()
  • Power: Use a separate 5–6 V supply for the servo; tie grounds with the Pi.
  • Cable relief: Leave slack at center; avoid twisting during sweeps.
ESP Coding

ESP32 example (Arduino core). Level-shift HC-SR04 ECHO to 3.3 V. Power the servo from a separate 5–6 V rail; share GNDs.

ESP32: Pan + Ping Scan

#include <Arduino.h>

const int PIN_SERVO = 17;
const int PIN_TRIG  = 5;
const int PIN_ECHO  = 18;

const int CH = 0, FREQ = 50, RES = 16;
uint32_t degToDuty(int deg){
  deg = constrain(deg, 0, 180);
  float us = 500 + (deg/180.0f)*2000.0f;
  return (uint32_t)(us * FREQ * (1 << RES) / 1000000.0f);
}

long pingUS(){
  digitalWrite(PIN_TRIG, LOW); delayMicroseconds(3);
  digitalWrite(PIN_TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(PIN_TRIG, LOW);
  return pulseIn(PIN_ECHO, HIGH, 30000UL);
}

void setup(){
  Serial.begin(115200);
  pinMode(PIN_TRIG, OUTPUT); pinMode(PIN_ECHO, INPUT);
  ledcSetup(CH, FREQ, RES); ledcAttachPin(PIN_SERVO, CH);
  ledcWrite(CH, degToDuty(90)); delay(500);
}
void loop(){
  for(int a=30;a<=150;a+=5){
    ledcWrite(CH, degToDuty(a)); delay(180);
    long us = pingUS(); float cm = us ? us/58.0f : -1;
    Serial.printf("A:%d  cm:%.1f\n", a, cm);
  }
  for(int a=150;a>=30;a-=5){
    ledcWrite(CH, degToDuty(a)); delay(180);
    long us = pingUS(); float cm = us ? us/58.0f : -1;
    Serial.printf("A:%d  cm:%.1f\n", a, cm);
  }
}

ESP-IDF: Use ledc PWM and rmt/capture for echo timing.

Drivers

No driver is needed for the bracket itself. If you connect a USB-serial bridge for debugging:

Resources
Specifications
Compatibility HC-SR04 ultrasonic sensor; SG90 micro-servo
Materials Plastic/nylon or acrylic; metal screws
Pan Range Up to ~180° (servo dependent); recommend 30–150° sweep
Mount Pattern HC-SR04 face holes; SG90 horn screws
Hardware Bracket + screw set (varies by kit)
Use Cases Scanning rangefinder, robot mapping, obstacle avoidance

Note: Exact hole spacing and hardware may vary by batch; check your kit contents.

FAQ
Servo jitters during scans
Use a dedicated 5–6 V supply for the servo, add decoupling near the servo, and pause ~150–200 ms before each ping.
Inconsistent distance readings
Avoid pings while moving; average multiple samples; keep clear of soft/angled targets and nearby ultrasonic sensors.
Range seems short on 3.3 V systems
Power the HC-SR04 from 5 V and level-shift the ECHO pin down to 3.3 V. TRIG from 3.3 V is usually fine.
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