# Basic Motor Control Application Using PWM Input

This program demonstrates a fundamental motor control application, where the motor’s behavior is controlled through Pulse Width Modulation (PWM) input specified by the user. The script sets up the motor, establishes communication, and allows the user to input a PWM value to control the motor's rotation speed and direction. PWM is a common method to adjust motor speed in motor control applications.

**About Tools and Materials:**

[SMD Red](https://docs.acrome.net/electronics/smd-red) ([Purchase Here](https://www.robotshop.com/products/acrome-smd-red-smart-brushed-motor-driver-with-speed-position-and-current-control-modes))

[SMD USB Gateway](https://docs.acrome.net/electronics/gateway-modules/usb-gateway-module) ([Purchase Here](https://www.robotshop.com/products/acrome-usb-gateway-module-acrome-smd-products))

[Arduino Gateway Module](https://docs.acrome.net/electronics/gateway-modules/arduino-gateway-module) ([Purchase Here](https://www.robotshop.com/products/acrome-arduino-gateway-shield-module-acrome-smd-products))

[BDC Motor](https://docs.acrome.net/electronics/electrical-motors/brushed-dc-motors-bdc) ([Purchase Here](https://www.robotshop.com/products/acrome-12v-brushed-dc-motor-with-built-in-encoder-100-rpm-speed))

## **Step 1: Hardware & Software Overview** <a href="#step-1-hardware-and-software-overview" id="step-1-hardware-and-software-overview"></a>

#### **Project Key Components**

1. [**SMD**](https://docs.acrome.net/electronics/smd-red)**:** 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 [Button Module](https://docs.acrome.net/electronics/add-on-modules/button-module) and actuate the BDC motor, the [RGB LED Module](https://docs.acrome.net/electronics/add-on-modules/rgb-led-module) and the [Buzzer Module](https://docs.acrome.net/electronics/add-on-modules/buzzer-module).
2. [BDC Motor](https://docs.acrome.net/electronics/electrical-motors/brushed-dc-motors-bdc): The motor is controlled using velocity commands, allowing for smooth acceleration and deceleration.

#### **Project Key Features**

1. **USB Port Detection**:
   1. The script first identifies and connects to the appropriate USB serial port based on the operating system. It uses the `serial` module to scan for connected serial devices.
   2. The program is compatible with Windows, Linux, and macOS, using different naming conventions for the USB port depending on the OS.
2. **Motor Initialization and Configuration**:
   1. The `Master` and `Red` objects from the `smd.red` library are used to set up the motor and establish communication.
   2. The motor's essential parameters are initialized, such as counts per revolution (`set_shaft_cpr`) and shaft RPM (`set_shaft_rpm`).
3. **PWM Input for Motor Control**:
   1. After configuring the motor, the program prompts the user to input a PWM value, which will control the motor’s rotation speed and direction.
   2. PWM values can vary, with positive values for one direction and negative values for the opposite. This input is passed to the motor using `m.set_duty_cycle(0, -int(pwm))`.
   3. By inputting a PWM value, the user directly controls the motor’s duty cycle, affecting both the speed and direction.
4. **User Feedback**:
   1. After setting the PWM, the program prints a confirmation message indicating that the motor is running at the specified PWM level.

## **Step 2: Assemble** <a href="#step-2-assemble" id="step-2-assemble"></a>

#### **Getting Started**

1. **Hardware Setup**
   * Connect the SMD to the PC or Arduino board using [USB Gateway Module](https://acrome.gitbook.io/acrome-smd-docs/electronics/gateway-modules/usb-gateway-module) or [Arduino Gateway Module](https://acrome.gitbook.io/acrome-smd-docs/electronics/gateway-modules/arduino-gateway-module).
   * Connect the 100 RPM [BDC Motor](https://docs.acrome.net/electronics/electrical-motors/brushed-dc-motors-bdc) with Encoder to the motor ports of the SMD Red.
   * Make sure that the SMD is powered and all connections are correct.

#### **Project Wiring Diagram**

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2F3fSkR64pSgDlFZHLQs28%2FBasic%20Motor%20Control%20Application%20Using%20PWM%20Input.png?alt=media&#x26;token=4a48a90d-2dbc-4b6a-bd52-c073e293a97d" alt=""><figcaption></figcaption></figure>

## Step 3: Run & Test <a href="#step-3-run-and-test" id="step-3-run-and-test"></a>

#### Run the Script

• Execute the script to initiate the Basic Motor Control Application Using PWM Input.

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

• Enter a PWM value (0 to 255) when prompted to adjust the motor speed.

#### Experience PWM-Based Motor Control

• Observe how the motor speed increases as the PWM value increases.

• Enter zero (0) to stop the motor completely.

• Notice how the PWM signal directly influences the motor speed, providing smooth and efficient control.

## Codes

{% tabs %}
{% tab title="Python Code" %}
{% code lineNumbers="true" %}

```python
from smd.red import *  # Import the SMD Red motor control library
import math
import os

from serial.tools.list_ports import comports  # Import serial communication tools
from platform import system  # Import system detection module

# 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)

# Attach the motor with ID 0
m.attach(Red(0))

# Set motor parameters
m.set_shaft_cpr(0, 6533)  # Set encoder counts per revolution (CPR)
m.set_shaft_rpm(0, 100)  # Set shaft speed in RPM

# Initialize control variables
motor_speed = 0
angle_degrees = 0
current_limit = 100  # Maximum allowable current
current_value = 0
previous_current = 0  # Store the previous current value for monitoring

# Configure motor operation modes and control parameters
m.set_operation_mode(0, OperationMode.Velocity)  # Set velocity control mode
m.set_control_parameters_velocity(0, 30.0, 5.0, 0.0)  # Set PID parameters for velocity control
m.set_control_parameters_position(0, 0.5, 0.0, 20.0)  # Set PID parameters for position control
m.set_control_parameters_torque(0, 3.0, 0.1, 0.0)  # Set PID parameters for torque control

# Enable motor torque to allow movement
m.enable_torque(0, True)

# Switch the motor to PWM control mode
m.set_operation_mode(0, OperationMode.PWM)

# Ask user to enter PWM value
pwm = input("PWM: ")

# Set motor PWM duty cycle (negative value for reverse direction)
m.set_duty_cycle(0, -int(pwm))

# Print confirmation message
print(f"The motor is running with a PWM value of {pwm}.")
```

{% endcode %}
{% endtab %}

{% tab title="Arduino Code" %}
{% code lineNumbers="true" %}

```cpp
#include <Acrome-SMD.h>

// Define motor parameters
#define MOTOR_ID 0       // SMD Red Motor ID
#define BAUDRATE 115200  // Serial Communication Baud Rate
#define CPR 6533         // Encoder Counts Per Revolution (CPR)
#define DEFAULT_RPM 100  // Default motor speed in RPM

// Initialize SMD Master and Motor
Master master(&Serial, BAUDRATE);
Red motor(MOTOR_ID);

void setup() {
    Serial.begin(BAUDRATE);
    Serial.println("SMD Red Motor Control Initialized.");

    // Attach motor to master
    master.attach(motor);

    // Configure motor settings
    motor.setShaftCpr(CPR);    // Set Encoder Counts Per Revolution (CPR)
    motor.setShaftRpm(DEFAULT_RPM);  // Set default motor speed in RPM

    // Configure control parameters
    motor.setOperationMode(OperationMode::Velocity);  // Set to Velocity Control Mode
    motor.setControlParametersVelocity(30.0, 5.0, 0.0);  // PID for Velocity Control
    motor.setControlParametersPosition(0.5, 0.0, 20.0);  // PID for Position Control
    motor.setControlParametersTorque(3.0, 0.1, 0.0);  // PID for Torque Control

    // Enable motor torque
    motor.enableTorque(true);

    // Switch motor to PWM control mode
    motor.setOperationMode(OperationMode::PWM);
}

void loop() {
    if (Serial.available() > 0) {
        String input = Serial.readStringUntil('\n');  // Read user input
        int pwmValue = input.toInt();  // Convert input to integer

        if (pwmValue >= -255 && pwmValue <= 255) {  // Limit PWM range
            motor.setDutyCycle(pwmValue);  // Apply PWM value
            Serial.print("The motor is running with a PWM value of ");
            Serial.println(pwmValue);
        } else {
            Serial.println("Invalid PWM value! Enter a number between -255 and 255.");
        }
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}
