Creating a Servo-Controlled Automated Sorting Conveyor with Raspberry Pi

Micro Servo Motor with Raspberry Pi / Visits:1

Why This Project Matters in Modern Automation

Automation is no longer confined to massive factories with million-dollar budgets. Today, makers, students, and small-scale entrepreneurs can build surprisingly capable automated systems using affordable components and a Raspberry Pi. One of the most satisfying and instructive projects in this space is a servo-controlled automated sorting conveyor. It combines mechanical design, electronics, and software into a single system that actually does something useful: moving objects, detecting them, and sorting them into designated bins.

At the heart of this system lies the micro servo motor. These tiny, inexpensive, yet remarkably precise actuators have become the darlings of the maker community. Unlike their larger industrial counterparts, micro servos are lightweight, draw modest current, and can be controlled directly from a Raspberry Pi with minimal additional hardware. Their ability to rotate to specific angles with repeatable accuracy makes them ideal for applications where you need controlled motion rather than continuous rotation.

This article walks you through the design, construction, and programming of a complete servo-controlled sorting conveyor. Whether you are a hobbyist looking for a weekend challenge or an educator seeking a compelling STEM project, you will find practical guidance, code snippets, and design considerations here.

Understanding the Micro Servo Motor

What Makes a Micro Servo Different

A standard hobby servo, such as the SG90 or MG90S, weighs only a few grams and measures roughly 23 x 12 x 29 millimeters. Despite its small size, it contains a DC motor, a gear train, a potentiometer for position feedback, and a control circuit. This integrated package allows you to command the output shaft to a specific angle, typically between 0 and 180 degrees.

The micro servo distinguishes itself from larger standard servos primarily in torque and size. While a standard servo might deliver 3 to 10 kg-cm of torque, a micro servo typically offers 1 to 2.5 kg-cm. For sorting lightweight objects such as small plastic tokens, candy pieces, or electronic components, this is more than sufficient.

How Pulse-Width Modulation Controls Position

The micro servo responds to a pulse-width modulation (PWM) signal. A pulse of approximately 1 millisecond corresponds to 0 degrees, 1.5 milliseconds to 90 degrees, and 2 milliseconds to 180 degrees. The servo expects this pulse to repeat every 20 milliseconds, which is a frequency of 50 Hz.

The Raspberry Pi has hardware PWM on only a couple of GPIO pins, but thanks to libraries like gpiozero and pigpio, you can generate stable PWM signals on almost any GPIO pin. This flexibility is crucial because a sorting conveyor may require multiple servos: one to actuate the diverter arm, another to feed objects, and perhaps a third to rotate a carousel of bins.

Why Micro Servos Are a Hot Topic Right Now

Several trends have pushed micro servos into the spotlight. First, the rise of desktop robotics and small-scale automation has created demand for compact actuators. Second, the proliferation of 3D printing allows makers to design custom brackets and linkages that fit micro servos perfectly. Third, the cost of micro servos has plummeted; you can buy a pack of five for less than the price of a single Raspberry Pi Pico. Finally, educational initiatives around STEM and robotics frequently use micro servos because they are safe, easy to interface, and visually satisfying.

System Overview and Design Goals

Project Scope

Our sorting conveyor will perform the following sequence:

  1. A conveyor belt, driven by a DC motor or a continuous rotation servo, moves objects from left to right.
  2. An infrared (IR) proximity sensor or a color sensor detects an object and determines its category (for example, red vs. blue).
  3. A micro servo mounted beside the conveyor rotates a diverter arm. If the object is red, the arm stays retracted and the object falls into Bin A. If the object is blue, the arm extends and pushes the object into Bin B.
  4. The Raspberry Pi coordinates the timing, reads the sensor, and commands the servo.

Design Goals

  • Reliability: The system should sort at least 10 objects per minute with minimal errors.
  • Simplicity: Use off-the-shelf components and avoid custom PCBs.
  • Scalability: The code should allow adding more servos or sensors without a complete rewrite.
  • Safety: The micro servo should not stall or overheat; use software limits and current limiting where possible.

Hardware Components and Assembly

Bill of Materials

  • Raspberry Pi 4 Model B (2GB or more) or Raspberry Pi Zero 2 W
  • Micro servo motor (SG90 or MG90S) – at least one, preferably two
  • Conveyor belt mechanism (can be 3D printed or built from rollers and a rubber band)
  • DC gear motor for the conveyor (or a continuous rotation servo)
  • IR reflective sensor module (e.g., TCRT5000) or TCS34725 color sensor
  • L298N or TB6612FNG motor driver for the conveyor motor
  • External 5V power supply capable of at least 2A
  • Jumper wires, breadboard, and a 3D-printed or laser-cut frame
  • Sorting bins (small cardboard boxes work fine)

Mechanical Arrangement

Mount the conveyor belt horizontally on the frame. Place the IR sensor just above the belt, about 5 centimeters before the diverter arm. The micro servo should be mounted perpendicular to the belt, with a 3D-printed arm that swings across the belt surface. When the arm is at 0 degrees, it is parallel to the belt and does not interfere. When it rotates to 90 degrees, it blocks the object's path and deflects it sideways into the alternate bin.

Ensure the servo horn is securely attached to the diverter arm. A loose horn will cause positioning errors and jitter. Use a small screw to lock the horn onto the servo spline.

Power Considerations

A common mistake is powering the micro servo directly from the Raspberry Pi's 5V pin. While this may work for one tiny servo with no load, the moment the servo encounters resistance, it draws a current spike that can cause the Pi to brown out and reboot. Always use a separate 5V power supply for the servos and the conveyor motor. Connect the grounds of the external supply and the Raspberry Pi together.

Wiring and Electrical Connections

Connecting the Micro Servo

The micro servo has three wires: brown or black for ground, red for positive 5V, and orange or yellow for the PWM signal. Connect the ground to the external power supply ground, the red wire to the external 5V rail, and the signal wire to a GPIO pin on the Raspberry Pi, for example, GPIO 17 (physical pin 11).

Connecting the Sensor

For an IR reflective sensor, connect VCC to 3.3V or 5V depending on the module, GND to ground, and the digital output to a GPIO input pin, such as GPIO 27 (physical pin 13). If you use a color sensor like the TCS34725, connect it via I2C: SDA to GPIO 2, SCL to GPIO 3, and power to 3.3V.

Connecting the Conveyor Motor

If you use a DC gear motor, connect it to a motor driver such as the TB6612FNG. The driver's inputs (AIN1, AIN2, PWMA) connect to GPIO pins on the Pi. The motor outputs connect to the motor. Power the driver from the external 5V or 6V supply.

Software Implementation on Raspberry Pi

Setting Up the Environment

Start with a fresh Raspberry Pi OS installation. Enable I2C and remote GPIO if needed via raspi-config. Install the required Python libraries:

bash sudo apt update sudo apt install python3-gpiozero python3-pigpio pip3 install adafruit-circuitpython-tcs34725

Enable the pigpio daemon for better PWM timing:

bash sudo systemctl enable pigpiod sudo systemctl start pigpiod

Controlling the Micro Servo with gpiozero

The gpiozero library provides a simple Servo class. Here is a minimal example that sweeps the servo from 0 to 180 degrees:

python from gpiozero import Servo from time import sleep

servo = Servo(17, minpulsewidth=0.5/1000, maxpulsewidth=2.5/1000)

while True: servo.min() sleep(1) servo.mid() sleep(1) servo.max() sleep(1)

For precise angle control, use the value property, where -1 corresponds to 0 degrees, 0 to 90 degrees, and 1 to 180 degrees. You can map angles to values with a simple linear function.

Reading the Sensor and Sorting Logic

The main loop should poll the sensor. When an object is detected, record the time. After a fixed delay (calculated from the belt speed and the distance between sensor and diverter), command the servo to the appropriate position. Hold the position for 300 milliseconds to ensure the object is diverted, then return the servo to its neutral position.

python from gpiozero import Servo, DigitalInputDevice from time import sleep, time

servo = Servo(17) sensor = DigitalInputDevice(27) beltspeedcmpersec = 10 distancecm = 5 delay = distancecm / beltspeedcmpersec

def sort_object(color): if color == "red": servo.value = -1 # 0 degrees, retracted else: servo.value = 1 # 180 degrees, extended sleep(0.3) servo.value = 0 # neutral

while True: if sensor.isactive: # Replace with actual color detection color = "red" if time() % 2 < 1 else "blue" sleep(delay) sortobject(color) sleep(0.5) # debounce

Adding Color Detection

For a more robust system, use the TCS34725 color sensor. Read the RGB values and classify the object. A simple threshold-based classifier works well for distinct colors. Normalize the readings and compare the red and blue channels.

python import board import busio import adafruit_tcs34725

i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_tcs34725.TCS34725(i2c)

def detectcolor(): r, g, b = sensor.colorrgb_bytes if r > b and r > g: return "red" elif b > r and b > g: return "blue" else: return "unknown"

Calibration and Tuning

Servo Angle Calibration

Every micro servo has slight variations. Use a protractor or a printed angle template to find the pulse widths that correspond to 0, 90, and 180 degrees. Adjust min_pulse_width and max_pulse_width in the Servo constructor accordingly. A typical SG90 might need 0.5 ms for 0 degrees and 2.4 ms for 180 degrees.

Timing Calibration

The delay between sensor detection and servo actuation depends on belt speed and distance. Measure the belt speed by placing a marker on the belt and timing one full revolution. Alternatively, use a second sensor at the diverter location to trigger the servo directly, eliminating timing guesswork.

Reducing Jitter

Micro servos can jitter when the PWM signal is unstable. Using the pigpio backend instead of the default RPi.GPIO backend often solves this. You can also add a small capacitor (100 µF) across the servo's power terminals to smooth voltage spikes.

Advanced Features and Improvements

Multiple Sorting Bins with a Carousel

Instead of a single diverter arm, mount three or four micro servos on a rotating platform. Each servo controls a gate that drops objects into a different bin. This approach scales to many categories without increasing belt length.

Object Tracking with Computer Vision

Replace the color sensor with a Raspberry Pi Camera and OpenCV. Detect objects, classify them by shape or color, and trigger the servo at the right moment. This adds complexity but dramatically increases flexibility.

Closed-Loop Feedback

Add a potentiometer or an encoder to the diverter arm to verify its position. If the servo fails to reach the commanded angle, the software can retry or raise an alert. This is especially useful in high-reliability applications.

Web Interface and Data Logging

Use Flask or FastAPI to create a simple web dashboard. Display counts of sorted objects, servo status, and error rates. Log data to a CSV file for later analysis. This transforms a simple project into a showcase-worthy demonstration.

Common Pitfalls and How to Avoid Them

Powering Servos from the Pi

As mentioned earlier, never power a micro servo from the Raspberry Pi's 5V pin if you value stability. Use a separate 5V 2A supply. The Pi and the servo should share only ground.

Ignoring Servo Stall Current

A micro servo can draw 500 mA or more when stalled. If your power supply cannot deliver this, the voltage will sag, and the servo will behave erratically. Choose a supply with at least 2A capacity.

Overlooking Mechanical Stress

Repeatedly driving the servo against a hard stop will strip gears. Always leave a few degrees of margin at each end of travel. Use software limits to prevent the servo from commanding beyond its mechanical range.

Poor Sensor Placement

Reflective IR sensors are sensitive to ambient light and surface color. Mount the sensor close to the belt (3 to 5 mm) and shield it from overhead lights. Test with the actual objects you plan to sort.

Final Thoughts on Building with Micro Servos

The micro servo motor is a gateway component. It teaches you about PWM, feedback control, mechanical linkage design, and power management—all within a project that fits on a desktop. The sorting conveyor described here is not just a toy; it is a platform for learning and experimentation. You can swap sensors, change sorting logic, add more servos, and even connect it to the cloud.

What makes this project particularly rewarding is the tangible outcome. You see objects moving, the servo twitching, and the bins filling up. That immediate feedback loop is invaluable for understanding embedded systems. And because micro servos are so inexpensive, you can afford to make mistakes, burn one out, and try again.

So gather your parts, fire up your Raspberry Pi, and start building. The world of automated sorting is literally at your fingertips.

Copyright Statement:

Author: Micro Servo Motor

Link: https://microservomotor.com/micro-servo-motor-with-raspberry-pi/automated-sorting-conveyor-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