Acrome-SMD Docs
All Acrome ProductsReferencesBlogCase StudiesContact Us
  • ACROME SMD
  • Electronics
    • 🔴SMD Red
      • Coding Guide
      • Robot with Raspberry Pi Setup Guide
      • Robot with Arduino Setup Guide
      • Troubleshooting Guide
    • 🔵SMD Blue
    • 🟢SMD Green
    • Gateway Modules
      • Arduino Gateway Module
      • USB Gateway Module
    • Electrical Motors
      • Brushed DC Motors (BDC)
      • Stepper DC Motors (SDC)
      • Brushless DC Motor (BLDC)
      • Linear Actuator with Feedback
    • Add-on Modules
      • Ambient Light Sensor Module
      • Button Module
      • Buzzer Module
      • IMU Module
      • Joystick Module
      • Potentiometer Module
      • Reflectance Sensor Module
      • RGB LED Module
      • Servo Module
      • Ultrasonic Distance Sensor Module
  • SMD Kits
    • Starter Kit
      • What You Can Build
    • Education Kit
      • What You Can Build
    • Motion Kit
      • What You Can Build
  • Software
    • Libraries
      • Python Library
      • Arduino Library
      • Java Library
      • Matlab Library
    • SMD UI
    • SMD Blockly
      • Introducing Customized Blockly Blocks
  • SMD Applications
    • Basics
      • Blink
      • Action - Reaction
      • Autonomous Lighting
      • Smart Doorbell
      • Security System
      • Distance Buzzer Warning
      • Distance Auto Stop
      • Smart Light Control
    • Interactive
      • Automatic Trash Bin
      • Radar
      • Chrome Dino Game Player
      • Play Chrome Dino Game With Joystick
      • Snake Game With Joystick
      • Pan-Tilt with Joystick Module
      • Joystick Mouse Control
      • Rev Up the Engine
      • Motor Rotation Based on Turn Input Value
      • Basic Motor Speed Control Application
      • Basic Motor Control Application Using PWM Input
      • Basic Motor Position Control Application
      • Basic Motor Torque Control Application
      • Motor Rotation Based on Joystick Counting
    • Robotics
      • Differential Robot Projects
      • Mouse Cursor Tracker Motion Robot
      • Waypoint tracker robot
      • Braitenberg Robot
      • Line-Follower Robot
      • Teleoperation Robot
      • Obstacle Avoidance Robot
      • ESP32 Wireless Controlled Mobile Robot
  • AI
    • Object Tracking Robot
    • Groq Chatbot-Controlled Robot
  • ROS
    • Teleoperation Robot with ROS
  • Mechanics
    • Building Set
      • Plates
        • 2x2 Plate Package
        • 2x3 120° Plate Package
        • 3x3 Plate Package
        • 3x5 Plate Package
        • 3x9 Plate Package
        • 11x19 Plate
        • 9x19 Plate
        • 5x19 Plate
        • 3x19 Plate
        • 9x11 Plate
        • 5x13 Plate
      • Joints
        • 60° Joint Package
        • 90° Joint Package
        • 120° Joint Package
        • Slot Joint M2 Package
        • Slot Joint M3 Package
        • U Joint Package
      • Mounts
        • Add-on Mount Package
        • Motor L Mount Package
        • Pan-Tilt Package
      • Wheels
        • Ball Wheel Package
        • Caster Wheel Package
        • Wheel Package
      • Cables
        • Power Cable 10 cm Package
        • Power Cable 20 cm Package
        • Power Cable 35 cm Package
        • RJ-11 Cable 7.5 cm Package
        • RJ-11 Cable 20 cm Package
        • RJ-11 Cable 35 cm Package
        • RJ-45 Cable 7.5 cm Package
        • RJ-45 Cable 20 cm Package
        • RJ-45 Cable 35 cm Package
      • Fasteners
        • M2x5 Allen Hex Screw Package
        • M3x6 Allen Hex Screw Package
        • M3x8 Allen Hex Screw Package
        • M3 Hex Nut Package
  • Help
    • Manual
    • Shops
    • Reach Us
Powered by GitBook
On this page
  • Step 1: Hardware & Software Overview
  • Step 2: Assemble
  • Step 3: Run & Test
  • Codes
  1. SMD Applications
  2. Robotics

Obstacle Avoidance Robot

PreviousTeleoperation RobotNextESP32 Wireless Controlled Mobile Robot

Last updated 20 days ago

The Obstacle Avoidance Robot project demonstrates an autonomous mobile robot capable of detecting and avoiding obstacles in its environment. Built on a differential drive platform and powered by Acrome’s Motion Kit, this robot uses an ultrasonic distance sensor mounted on a pan-tilt servo mechanism to scan the environment in multiple directions. By integrating sensory feedback with real-time motion decisions, the robot intelligently navigates through dynamic or cluttered spaces without external control.

About Tools and Materials:

3x ()

()

()

2x ()

2x ()

()

Step 1: Hardware & Software Overview

Project Key Components

Used to control the left motor, right motor, and the pan-tilt + ultrasonic sensor unit.

Drive the differential base for forward, backward, and turning motion.

Pan-Tilt Allows the ultrasonic sensor to scan the environment horizontally and vertically.

Detects obstacles by measuring distance and provides real-time feedback.

(USB or Arduino) Connects the SMD network to a computer or controller for command transmission.

Key Features:

  • Autonomous Navigation The robot moves independently by detecting and avoiding obstacles in real time.

  • Pan-Tilt Environmental Scanning The ultrasonic sensor is mounted on a pan-tilt servo system, enabling multi-directional scanning for enhanced obstacle detection.

  • Modular and Expandable Architecture Built entirely using Acrome’s Motion Kit, the system is easy to expand with additional modules.

  • Multiple Programming Interfaces Supports both Python and Blockly, allowing users to implement logic visually or through code.

  • Real-Time Feedback and Decision Making The system continuously analyzes distance data and makes path decisions accordingly.

  • Educational and Practical Ideal for learning about autonomous systems, sensor fusion, servo control, and reactive robotics.

Step 2: Assemble

  1. Hardware Setup

Secure all components to the robot chassis, including the motors, servos, and sensor. Make sure the ultrasonic sensor is positioned at the front of the robot with a clear field of view.

Power the SMD modules using a compatible external power supply or battery pack. Confirm that all indicator LEDs are active and that the wiring is neat, tight, and correctly placed.

Project Wiring diagram

Step 3: Run & Test

Software Execution and Behavior Testing

Once all hardware connections are complete, proceed with testing the system using either Blockly or Python.

Option 1: Blockly Interface

  • Open the Acrome Blockly platform and load the obstacle avoidance project blocks.

  • Use logic blocks to scan using the pan-tilt servo and read distance from the ultrasonic sensor.

  • Add movement commands to turn or stop the robot based on sensor data.

  • Upload the code and run it. The robot should begin autonomous navigation, avoiding obstacles by turning left, right, or stopping when necessary.

Option 2: Python API

  • Open your development environment and run the provided Python script or write your own using Acrome’s Python SDK.

  • Use the get_ultrasonic() function to read distance values from the sensor.

  • Move the pan-tilt servos using set_position() to scan the environment.

  • Based on distance readings, control the motors with set_duty_cycle() to avoid obstacles.

  • Run the script and observe how the robot scans, decides, and reacts in real-time.

What to Expect During Testing:

  • The robot will continuously scan left, center, and right using the pan-tilt mechanism.

  • If an obstacle is detected within a predefined distance, the robot will stop or change direction.

  • Smooth and reactive motion indicates successful configuration and programming.

Codes

from smd.red import *
import time
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout, QLabel, QMessageBox
from PyQt5.QtCore import QTimer
from serial.tools.list_ports import comports
from platform import system

# Detect and return the correct USB port based on OS
def USB_Port():
    ports = list(comports())
    usb_names = {
        "Windows": ["USB Serial Port"],
        "Linux": ["/dev/ttyUSB"],
        "Darwin": [
            "/dev/tty.usbserial", "/dev/tty.usbmodem", "/dev/tty.SLAB_USBtoUART",
            "/dev/tty.wchusbserial", "/dev/cu.usbserial", "/dev/cu.usbmodem",
            "/dev/cu.SLAB_USBtoUART", "/dev/cu.wchusbserial"
        ]
    }
    os_name = system()
    for port in ports:
        if any(name in port.device or name in port.description for name in usb_names.get(os_name, [])):
            print(f"USB device detected on port: {port.device}")
            return port.device
    print("No suitable USB device found!")
    return None

class ObstacleAvoidanceRobot(QMainWindow):
    def __init__(self, port):
        super().__init__()

        # Initialize and attach SMD Red modules
        self.master = Master(port)
        self.master.attach(Red(0))  # Left motor
        self.master.attach(Red(1))  # Right motor
        self.master.attach(Red(2))  # Pan-Tilt + Ultrasonic module

        # Module and component IDs
        self.left_motor = 0
        self.right_motor = 1
        self.ultrasonic_id = 1
        self.pan_servo = 5
        self.tilt_servo = 4

        # Motion parameters
        self.running = False
        self.threshold = 30          # Distance threshold (in cm)
        self.turn_speed = 70
        self.forward_speed = 80

        # Set up GUI
        self.setWindowTitle("Obstacle Avoidance Robot")
        self.setGeometry(100, 100, 400, 300)

        widget = QWidget()
        layout = QVBoxLayout(widget)
        self.setCentralWidget(widget)

        # Status display and buttons
        self.status_label = QLabel("Status: Idle")
        self.distance_label = QLabel("Distance: -- cm")
        self.start_button = QPushButton("Start")
        self.stop_button = QPushButton("Stop")
        self.start_button.clicked.connect(self.start_robot)
        self.stop_button.clicked.connect(self.stop_robot)

        layout.addWidget(self.status_label)
        layout.addWidget(self.distance_label)
        layout.addWidget(self.start_button)
        layout.addWidget(self.stop_button)

        # Timer for periodic obstacle checking
        self.timer = QTimer()
        self.timer.timeout.connect(self.check_obstacle)

    def check_obstacle(self):
        """Read ultrasonic sensor and decide whether to move or avoid."""
        try:
            distance = self.master.get_ultrasonic(smd_id=2, module_id=self.ultrasonic_id)
            self.distance_label.setText(f"Distance: {distance:.2f} cm")

            if distance < self.threshold:
                self.status_label.setText("Status: Obstacle Detected!")
                self.avoid()
            else:
                self.status_label.setText("Status: Moving Forward")
                self.forward()
        except Exception as e:
            print("Sensor Error:", e)
            self.status_label.setText("Status: Sensor Error")

    def forward(self):
        """Move forward by setting opposite polarities to motors."""
        self.master.set_duty_cycle(self.left_motor, -self.forward_speed, smd_id=0)
        self.master.set_duty_cycle(self.right_motor, self.forward_speed, smd_id=1)

    def stop_robot(self):
        """Stop robot motion and update UI."""
        self.running = False
        self.timer.stop()
        self.master.set_duty_cycle(self.left_motor, 0, smd_id=0)
        self.master.set_duty_cycle(self.right_motor, 0, smd_id=1)
        self.status_label.setText("Status: Stopped")

    def avoid(self):
        """Obstacle avoidance logic using pan-tilt scanning."""
        self.stop_robot()
        time.sleep(0.3)

        # Scan left
        self.master.set_position(self.pan_servo, -40, smd_id=2)
        time.sleep(0.3)
        left = self.master.get_ultrasonic(smd_id=2, module_id=self.ultrasonic_id)

        # Scan right
        self.master.set_position(self.pan_servo, 40, smd_id=2)
        time.sleep(0.3)
        right = self.master.get_ultrasonic(smd_id=2, module_id=self.ultrasonic_id)

        # Reset pan position
        self.master.set_position(self.pan_servo, 0, smd_id=2)

        # Decide which way to turn based on distances
        if left > right and left > self.threshold:
            self.turn_left()
        elif right > left and right > self.threshold:
            self.turn_right()
        else:
            self.turn_right()  # Default to right

        time.sleep(0.5)
        self.forward()

    def turn_left(self):
        """Rotate robot left by reversing both motors."""
        self.master.set_duty_cycle(self.left_motor, -self.turn_speed, smd_id=0)
        self.master.set_duty_cycle(self.right_motor, -self.turn_speed, smd_id=1)
        time.sleep(0.4)

    def turn_right(self):
        """Rotate robot right by forwarding both motors."""
        self.master.set_duty_cycle(self.left_motor, self.turn_speed, smd_id=0)
        self.master.set_duty_cycle(self.right_motor, self.turn_speed, smd_id=1)
        time.sleep(0.4)

    def start_robot(self):
        """Start obstacle detection loop."""
        if not self.running:
            self.running = True
            self.timer.start(200)  # Check every 200 ms
            self.status_label.setText("Status: Running")
            self.forward()

def main():
    app = QApplication(sys.argv)
    port = USB_Port()

    if not port:
        QMessageBox.warning(None, "USB Error", "No USB port detected.")
        return

    window = ObstacleAvoidanceRobot(port)
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
// Some code

Connect the to your PC or Arduino using either the or to establish communication.

Attach the with encoders to the motor output ports of the left and right SMD Red modules using RJ-45 cables.

Connect the pan-tilt and to the third SMD Red module. Ensure that the pan servo (horizontal) and tilt servo (vertical) are connected to the correct module IDs, and the ultrasonic sensor is attached via the appropriate port.

SMD Red
Purchase Here
SMD USB Gateway
Purchase Here
Arduino Gateway Module
Purchase Here
BDC Motor
Purchase Here
Servo Module
Purchase Here
Ultrasonic Distance Sensor
Purchase Here
SMD Red
BDC Motors
Servo Module
Ultrasonic Distance Sensor
Gateway Module
SMD Red
USB Gateway Module
Arduino Gateway Module
100 RPM brushed DC motors
servo modules
ultrasonic distance sensor