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

Basic Motor Speed Control Application

PreviousMotor Rotation Based on Turn Input ValueNextBasic Motor Control Application Using PWM Input

Last updated 1 month ago

This program demonstrates a basic motor control application that adjusts the motor's speed based on user input. It communicates with the motor controller via USB and sets the motor speed (velocity) according to the user's specified value. This allows for simple motor speed control, where the user can input a desired speed, and the motor will rotate accordingly.

About Tools and Materials:

()

()

()

()

Step 1: Hardware & Software Overview

Project Key Components

  1. The SMD acts as a bridge between the script and the modules. It is responsible for interpreting the commands sent by the script and translating them into actions that read input from the and actuate the BDC motor, the and the .

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

Project Key Features:

  1. USB Port Detection:

    1. The program starts by detecting the available USB serial port for communication with the motor. It uses the serial module to find connected serial devices.

    2. The program supports multiple platforms (Windows, Linux, and macOS) and automatically identifies the correct port based on the operating system.

  2. Motor Control with SMD Red:

    1. The motor is controlled through the smd.red library, which allows sending commands to the motor via the Red controller.

    2. A Master object is used to establish communication with the motor, and the motor is attached using the Red(ID) method.

  3. Motor Speed Configuration:

    1. The program allows the user to input a speed value (in RPM or another suitable unit). The motor's velocity is set to this value using the m.set_velocity() method.

    2. The motor's other parameters, such as the revolutions per minute (RPM) and control settings, are preconfigured for optimal operation.

  4. User Input:

    1. The program prompts the user to input the desired motor speed, and then it sets the motor’s velocity accordingly. The speed is used to control how fast the motor will rotate.

  5. Feedback to User:

    1. After setting the motor speed, the program confirms the set speed with a printed message: "The engine rotates at speed {speed}."

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

1. Upload the Arduino code to the SMD Master Controller using the Arduino IDE.

2. Open the Serial Monitor (set baud rate to 115200).

3. Enter a speed value (0 to 5000) to adjust the motor speed.

Experience Speed Control

• Observe how the motor starts at 50 RPM (default) and adjusts based on the entered speed.

• Enter a higher speed value to increase the motor velocity.

• Enter zero (0) to stop the motor.

Codes

from smd.red import *  # Import the SMD Red motor control library
from serial.tools.list_ports import comports  # Import serial communication tools
from platform import system  # Import system information module
import math
import os

# Function to detect and return the correct USB port for communication
def USB_Port():
    ports = list(comports())  # Get a list of available serial ports
    usb_names = {
        "Windows": ["USB Serial Port"],  # Windows-specific port names
        "Linux": ["/dev/ttyUSB"],  # Linux-specific port names
        "Darwin": [  # macOS-specific port names
            "/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()  # Detect the operating system
    if ports:  # If ports are available
        for port, desc, hwid in sorted(ports):  # Iterate through detected ports
            # Check if the port name or description matches the expected USB names
            if any(name in port or name in desc for name in usb_names.get(os_name, [])):
                return port  # Return the detected port
        
        # If no matching port was found, print the available ports
        print("Current ports:")
        for port, desc, hwid in ports:
            print(f"Port: {port}, Description: {desc}, Hardware ID: {hwid}")
    else:
        print("No port found")  # Print message if no ports are detected
    return None  # Return None if no suitable port is found

# Get the detected USB port
port = USB_Port()

# Initialize the motor controller using the detected port
m = Master(port)

# Define motor ID
ID = 0

# Attach the motor to the master controller
m.attach(Red(ID))

# Configure motor settings
m.set_shaft_cpr(ID, 6533)  # Set encoder counts per revolution (CPR)
m.set_shaft_rpm(ID, 100)  # Set shaft rotation speed in RPM
m.set_operation_mode(ID, OperationMode.Velocity)  # Set motor to velocity control mode
m.set_control_parameters_velocity(ID, 30.0, 5.0, 0.0)  # Set PID control parameters (P, I, D)
m.enable_torque(ID, True)  # Enable motor torque

# Get user input for speed
speed = input("Speed: ")

# Set the velocity of the motor based on user input
m.set_velocity(ID, float(speed))

# Print the motor's current speed
print("The motor rotates at speed " + speed + ".")
#include <Acrome-SMD.h>  // Include the Acrome-SMD library

#define ID         0  // Define motor ID
#define CPR        6533  // Encoder counts per revolution
#define RPM        100  // Shaft speed in RPM
#define BAUDRATE   115200  // Baud rate for serial communication

Red master(ID, Serial, BAUDRATE);  // Initialize the motor controller

void setup() {
    Serial.begin(BAUDRATE);  // Start Serial Monitor
    delay(500);  // Wait to stabilize serial communication

    master.begin();  // Initialize SMD motor communication

    Serial.println("Detecting SMD motor...");
    
    // Set motor settings
    master.setMotorCPR(CPR);  // Set encoder CPR
    master.setMotorRPM(RPM);  // Set motor RPM
    master.setOperationMode(2);  // Set motor to velocity control mode (2 = Velocity Mode)
    
    // Set PID control parameters: (OperationMode, P, I, D, Deadband, Feedforward)
    master.setControlParameters(2, 30.0, 5.0, 0.0, 0.0, 0.0);
    
    // Enable motor torque
    master.torqueEnable(1);

    Serial.println("Motor is ready.");
}

void loop() {
    Serial.println("Enter the speed:");

    // Wait for user input
    while (Serial.available() == 0) {
        delay(100);
    }

    // Read input speed from Serial Monitor
    int speed = Serial.parseInt();
    Serial.println("Speed entered: " + String(speed));

    // Set motor speed
    master.setpoint(2, speed);

    // Print confirmation message
    Serial.println("The motor rotates at speed " + String(speed) + ".");
    
    delay(100);  // Small delay to avoid overloading the system
}

Connect the SMD to the PC or Arduino board using or .

Connect the 100 RPM with Encoder to the motor ports of the SMD using an RJ-45 cable.

SMD Red
Purchase Here
SMD USB Gateway
Purchase Here
Arduino Gateway Module
Purchase Here
BDC Motor
Purchase Here
SMD
Button Module
RGB LED Module
Buzzer Module
BDC Motor
USB Gateway Module
Arduino Gateway Module
BDC Motor