Acrome-SMD Docs
All Acrome ProductsReferencesBlogCase StudiesContact Us
  • ACROME SMD
  • Electronics
    • 🔴SMD Red
      • 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 – 75 lbs
    • 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. Interactive

Motor Rotation Based on Joystick Counting

PreviousBasic Motor Torque Control ApplicationNextRobotics

Last updated 1 month ago

This Python code demonstrates a joystick-controlled motor rotation system that enables a user to count joystick inputs and control the motor's rotations accordingly. The application uses a USB-connected motor controller to read joystick inputs and perform precise motor movements based on the input count. Below is a step-by-step explanation of the code.

About Tools and Materials:

()

()

()

()

Step 1: Hardware & Software Overview

Key Components:

  1. The acts as the communication hub between the and the computer. It collects data, processes user input, and translates it into mouse movements and clicks.

  2. The serves as the main input device. The user can move the to control the mouse pointer’s X and Y coordinates on the screen. Additionally, the button can be used to trigger mouse clicks.

  3. The motor is controlled using velocity commands, allowing for smooth acceleration and deceleration.

Project Features:

  • Button Press Detection: Reads the joystick button state from the motor controller.

  • Entering Counting Mode: Holding the button for 5 seconds activates counting mode.

  • Incrementing the Counter: In counting mode, pressing and releasing the button increases the counter.

  • Triggering Motor Rotation: Holding the button for another 5 seconds rotates the motor according to the counter value.

Step 2: Assemble

Getting Started

  1. Hardware Setup

    • Make sure that the SMD is powered and all connections are correct

Project Wiring Diagram

Step 3: Run & Test

Run the Script

• Execute the script to initiate the Motor Rotation Based on Joystick Counting project.

• The script will automatically detect the USB port and establish communication with the SMD Master Controller.

• Move the joystick to control the motor’s rotation and observe the response.

Experience Joystick-Based Motor Control

• Move the joystick forward to rotate the motor clockwise (CW).

• Move the joystick backward to rotate the motor counterclockwise (CCW).

• Return the joystick to the center position to stop the motor.

• Observe how the motor speed and direction change dynamically based on joystick movement.

Codes

from smd.red import Master, Red
import time
from serial.tools.list_ports import comports
from platform import system

# Detect USB Port
def detect_usb_port():
    ports = list(comports())
    usb_names = {
        "Windows": ["USB Serial Port"],
        "Linux": ["/dev/ttyUSB"],
        "Darwin": ["/dev/tty.usbserial", "/dev/tty.usbmodem", "/dev/cu.usbserial"]
    }
    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, [])):
            return port.device
    return None

# Initialize SMD Red Connection
serial_port = detect_usb_port()
if not serial_port:
    print("No compatible USB port found.")
    exit(1)

master = Master(serial_port)
motor_id = 0
joystick_id = 5  # Joystick module ID

master.attach(Red(motor_id))
master.set_operation_mode(motor_id, 0)  # Set motor to PWM mode
master.enable_torque(motor_id, True)

# Control Motor with Joystick
def control_motor():
    while True:
        joystick_data = master.get_joystick(motor_id, joystick_id)
        if joystick_data is None:
            print("Joystick data could not be read.")
            time.sleep(0.1)
            continue

        x_axis, y_axis = joystick_data[0], joystick_data[1]

        # Convert joystick Y-axis input to PWM signal
        speed = int(abs(y_axis) * 100 / 100)  
        if y_axis > 0:
            master.set_duty_cycle(motor_id, speed)   # Move forward
        elif y_axis < 0:
            master.set_duty_cycle(motor_id, -speed)  # Move backward
        else:
            master.set_duty_cycle(motor_id, 0)       # Stop

        print(f"Joystick: X={x_axis}, Y={y_axis} | PWM: {speed}")
        time.sleep(0.05)

# Start Motor Control
control_motor()

Connect the SMD to the PC or Arduino board using .

Connect the and the to the SMD using an RJ-45 cable.

SMD Red
Purchase Here
SMD USB Gateway
Purchase Here
Joystick Module
Purchase Here
BDC Motor
Purchase Here
ACROME SMD
ACROME SMD
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
Joystick Module
BDC Motor
USB Gateway Module
BDC Motor
Joystick Module