Using Raspberry Pi to Control Servo Motors in Automated Quality Control Systems

Micro Servo Motor with Raspberry Pi / Visits:13

Automated quality control (QC) has moved far beyond the realm of million-dollar industrial installations. Today, a $35 Raspberry Pi paired with a handful of micro servo motors can power a surprisingly capable inspection station — one that sorts parts, actuates test probes, flips samples for multi-angle imaging, and rejects defective units in real time. This article walks through how to architect such a system, why micro servos are the breakout component of the moment, and what practical pitfalls you will hit along the way.

Why Micro Servo Motors Are Having a Moment

The SG90 and its metal-gear cousins (MG90S, MG996R at the larger end) have become the default actuators for hobbyist and small-scale automation for one simple reason: they package a motor, gearbox, driver, and position feedback loop into a 9-gram cube that costs less than a cup of coffee.

The Three Traits That Matter for QC

Repeatability within a few degrees. A decent micro servo holds position reliably enough for tasks like pushing a part against a stop gauge or rotating a camera cradle to a fixed inspection angle. You are not doing micron-level metrology with a $3 servo, but you absolutely can do "is this connector seated?" or "does this cap sit flush?" checks.

Direct PWM control. No stepper driver, no encoder wiring, no closed-loop controller board. One GPIO pin, one pulse train, done. This collapses your bill of materials and your debugging surface area simultaneously.

Torque-to-weight ratio. Because the gearbox is integrated, a 9g servo can move loads that would require a much heavier direct-drive motor. In a multi-station QC cell where every gram of moving mass affects cycle time, this matters.

Where Micro Servos Fall Short — and How to Design Around It

They buzz. They jitter under load. Their plastic gears strip. Their position feedback is a cheap potentiometer, not an encoder, so they drift with temperature and wear. The honest engineering answer is: use them for binary and coarse-positioning tasks, add mechanical hard stops wherever the final position must be exact, and never ask a micro servo to hold a load continuously against gravity for hours. If your QC station needs to press a probe down for 30 seconds per part, use a solenoid or a cam driven by the servo, not the servo horn directly.

Wiring a Servo to the Pi Without Cooking Either One

The single most common failure in Pi-servo projects is drawing servo current from the Pi's 5V rail. A micro servo can pull 500–700 mA in a stall, and a Raspberry Pi's regulator will not thank you.

Power Topology

Run the servo power rail from a separate 5V supply — a 2A UBEC, a bench supply, or even a quality USB power bank — and tie the grounds together with the Pi's ground. Signal wire goes to a GPIO pin (GPIO18 is the classic choice because it supports hardware PWM). Add a 470–1000 µF electrolytic capacitor across the servo's power and ground pins near the servo itself to absorb inrush spikes.

The Back-EMF Problem

When a servo decelerates, it dumps energy back onto its rail. Without the capacitor, this shows up as voltage dips that can reset the Pi or corrupt the PWM signal. If you are running several servos, give each one its own decoupling cap and consider a Schottky diode in line with the positive rail.

Driving Servos from Python: Beyond the Naive Approach

The classic gpiozero.Servo or RPi.GPIO PWM snippet works for a single demo servo. It falls apart the moment you need smooth motion, multiple simultaneous servos, or precise timing.

Use Hardware PWM and pigpio

The pigpio library (with its daemon) generates PWM in hardware with microsecond-level timing accuracy, which means far less jitter than software-timed pulses. For a QC station where a camera triggers at a specific servo angle, that stability is not a luxury — it is the difference between a usable image and a blurry one.

python import pigpio pi = pigpio.pi() pi.set_servo_pulsewidth(18, 1500) # center

Ramping Instead of Snapping

Never command a servo from 500 µs to 2500 µs in one step. The gear train slams, the current spikes, and the part you are manipulating gets flung. Write a small ramp function that steps the pulse width by 10–20 µs every few milliseconds. This single change will extend servo life dramatically and make your machine look and sound professional.

A Simple Motion Queue

For a QC cycle — home, extend probe, hold, retract, index turntable — model each motion as a tuple of (target pulse width, duration) and run them through a queue. This keeps your inspection logic clean and separates "what the machine does" from "how the servo gets there."

A Concrete QC Station Architecture

Picture a small cell that inspects 3D-printed brackets for a missing mounting boss.

The Physical Layout

A micro servo (MG90S, metal gears) drives a swing arm that sweeps each part from the feed track onto a glass inspection platform. A second servo rotates a mirror to give the camera a side view. A third servo actuates a reject flap. Total actuator cost: under $15.

The Control Loop

  1. IR break-beam sensor detects a part at the pick point.
  2. Pi commands servo 1 through a ramped sweep; a mechanical stop defines the final position.
  3. Camera captures top and side images (side view via servo 2's mirror).
  4. OpenCV or a lightweight TFLite model classifies pass/fail.
  5. Servo 3 either holds the flap closed (pass, part continues) or flicks it open (fail, part drops into the reject bin).
  6. Servo 1 returns home; cycle repeats.

Timing Budget

A well-tuned micro servo sweep of 90 degrees takes about 300–400 ms. With ramping and settling, budget 500 ms per actuation. Three actuations plus imaging puts you at roughly 2.5 seconds per part — about 1,400 parts per hour, which is genuinely useful for small-batch production.

Making It Robust: The Stuff Nobody Blogs About

Calibration Drift

Micro servo potentiometers drift as they warm up. Build a homing routine that drives each servo gently against a mechanical stop at startup, then records the pulse width that corresponds to "home." Re-run this every N cycles. Your positional accuracy will thank you.

Stall Detection via Current Sensing

Put a small shunt resistor (0.1 Ω) in the servo ground path and read the voltage drop with an INA219 or the Pi's ADC (via an external MCP3008). A sudden current spike means the servo hit something it should not have — a jammed part, a misaligned fixture. Trigger an e-stop and log it. This turns a $3 servo into a crude force sensor and is the single best reliability upgrade you can make.

Logging and Traceability

Every QC decision should be timestamped and tied to a part ID, even in a hobby-scale system. Write a CSV line per part: timestamp, servo angles commanded, image hash, verdict, current draw at each actuation. When a customer complains about a batch, you will have data instead of guesses.

Scaling Up Without Rewriting Everything

When one station becomes four, the Pi's single-core PWM limitations and Python's GIL start to bite. The clean escape route is to keep the Pi as the vision and orchestration brain, and offload servo timing to a microcontroller — a Raspberry Pi Pico running a tiny command protocol over UART. The Pi sends "servo 2 to 1800 µs over 400 ms," the Pico handles the pulse train with rock-solid timing, and your Python code stays almost unchanged. This hybrid architecture is how most serious small-scale automation shops actually build these cells.

Micro servo motors are not precision instruments, and that is precisely their charm: they are cheap, abundant, well-documented, and good enough for a huge class of real inspection tasks. Pair them with a Raspberry Pi, respect their electrical and mechanical limits, and you have an automated quality control system that costs less than a single industrial sensor — and teaches you more than any off-the-shelf unit ever could.

Copyright Statement:

Author: Micro Servo Motor

Link: https://microservomotor.com/micro-servo-motor-with-raspberry-pi/automated-quality-control-servo-raspberry-pi.htm

Source: Micro Servo Motor

The copyright of this article belongs to the author. Reproduction is not allowed without permission.

About Us

Lucas Bennett avatar
Lucas Bennett
Welcome to my blog!

Tags