Coding Guide

This detailed guide teaches you exactly how to write your first code for SMD Red from scratch, suitable for beginners with no coding experience. You'll learn step-by-step, clearly and thoroughly, how to program motors and modules.


What is SMD Red?

SMD Red is a smart controller designed to simplify controlling Brushed DC motors, servos, and various sensors. It receives commands from your computer (Raspberry Pi using Python) or microcontroller (Arduino using Arduino IDE) and translates these commands into actions performed by your robot.


Important: Understanding IDs

  • SMD Red ID: Every SMD Red controller has a unique ID number. Initially, these IDs might be the same, but you must assign unique IDs when using multiple controllers in one project.

  • Module ID: Every connected sensor, servo, or other module has a predefined ID, which is generally fixed but can be physically rearranged.

  • Motor ID: Motors do not have individual IDs. Instead, they use the ID of the SMD Red controller they're attached to.

To manage and test your SMD Red boards and modules, It is recommended to use the SMD UI. This software allows you to:

  • Verify the IDs of connected modules and controllers

  • Change SMD Red IDs to unique values for project usage

  • Test functionality of motors and modules


Part 1: Coding with Python (Using VS Code)

Step 1: Install Required Tools

  1. Python: Download from python.org. Choose the latest version and install it.

  2. Visual Studio Code (VS Code): Download from code.visualstudio.com. This will be your coding editor.

Step 2: Install Python Packages

Open VS Code, then open a new terminal ("Terminal" → "New Terminal"). Run this command:

pıp3 install acrome-smd
pip3 install pyserial flask flask-cors acrome-smd

Step 3: Writing Your First Python Program (Detailed Explanation)

Open VS Code, create a file called robot_control.py. Below is a simple and detailed starter program:

# Import necessary libraries from SMD Red
from smd_red import Master

# Establish connection to SMD Red controller via USB
master = Master("/dev/ttyUSB0")

# Motor Control Example:
# Set motor speed (50 means half speed forward, -50 means half speed backward)
master.set_motor(0, 50)  # Controls motor connected to SMD Red ID 0

# Wait for 2 seconds, motors keep moving
import time
time.sleep(2)

# Stop motor
master.set_motor(0, 0)

# Servo Control Example:
# Move servo motor to specific angle (90 degrees)
master.set_servo(0, 4, 90)  # Servo module ID 4 on SMD Red ID 0

# Sensor Reading Example:
# Read data from sensor connected with module ID 5
sensor_data = master.get_sensor_data(0, 5)
print("Sensor data:", sensor_data)

Explanation of Code:

  • Master connection: Links your computer to the SMD Red controller.

  • Motor commands: Uses the ID of SMD Red to send commands directly to connected motors.

  • Servo commands: Require specifying the SMD Red ID and servo module ID.

  • Sensor commands: Require specifying the SMD Red ID and sensor module ID to get sensor data.


Part 2: Coding with Arduino IDE

Step 1: Install Arduino IDE

Download Arduino IDE from arduino.cc, install and open it.

Step 2: Install SMD Red Arduino Library

  • In Arduino IDE, navigate to Sketch → Include Library → Manage Libraries

  • Search for "Acrome-SMD", then install it.

Step 3: Wiring Arduino and SMD Red

  • Connect Arduino to SMD Red using an Arduino Gateway module (RX/TX pins).

  • Connect SMD Red boards via RJ11 cables and modules via RJ45 cables.

Step 4: Detailed Arduino Programming Example

Open Arduino IDE and create a new sketch:

// Include Acrome SMD Red library
#include <Acrome-SMD.h>
#define BAUDRATE 115200

// Initialize communication with SMD Red board ID 0
Red smd(0, Serial, BAUDRATE);

void setup() {
  // Start serial communication at the defined baud rate
  Serial.begin(BAUDRATE);

  // Start communication with SMD Red
  smd.begin();

  // Set operation mode for controlling motor speed (PWM mode)
  smd.setOperationMode(PWMControl);

  // Enable motor torque (power)
  smd.torqueEnable(1);
}

void loop() {
  // Move motor forward at half speed
  smd.setpoint(0, 50);
  delay(2000);  // Wait for 2 seconds

  // Stop the motor
  smd.setpoint(0, 0);
  delay(2000);

  // Servo example: Move servo (module ID 4) to 90 degrees
  smd.setServo(0, 4, 90);
  delay(1000);

  // Read data from a sensor (module ID 5)
  int sensor_value = smd.getSensorData(0, 5);
  Serial.print("Sensor data: ");
  Serial.println(sensor_value);
  delay(1000);
}

Explanation of Arduino Code:

  • Library Import: Adds the necessary commands to control SMD Red.

  • Setup: Initializes communication, sets operation modes, and enables power to motors.

  • Loop: Repeatedly executes motor movement, servo positioning, and sensor data reading.


Tips for Beginners

  • Clearly verify and set unique SMD Red IDs using the SMD UI app.

  • Test each module separately before combining them.

  • Regularly test your code and debug using Serial Monitor or Python print statements.


Troubleshooting Tips (SMD Red Troubleshooting Guide)

  • Double-check connections (USB, RJ11, RJ45).

  • Verify module and SMD Red IDs using the SMD UI app.

  • If errors occur, revisit installation and wiring steps.

Last updated