Example Code for Arduino-Motor Debugging

Last revision 2026/01/05

This article presents a detailed guide for Arduino motor debugging, including hardware setup, wiring diagrams, and sample code for controlling motor movements efficiently. It outlines the process of robot assembly and provides practical programming examples for motor control, ensuring smooth and effective robot operation.

Hardware Preparation

  • Motors: 4pcs (included in the kit)
  • Romeo BLE controller: 1pc (included in the kit)
  • Battery box: 1pc (included in the kit)
  • Black and red wires (15 cm long): 4 pairs (included in the kit)
  • Screws and washers: Included in the kit

Software Preparation

  • Arduino IDE (download from click here)
  • No additional libraries required for this code.

Wiring Diagram

Connect the motors to the microcontroller board as follows:

  • Left motor’s red and black wires to M2
  • Right motor’s red and black wires to M1
  • Battery pack’s black wire to GND, red wire to VND

Assemble_Mobile_Platform_STEP7_1.png

Other Preparation Work

  1. Complete the robot assembly as per STEP1: Assemble Robot (including motor assembly, cable soldering, Romeo BLE controller installation, battery box assembly, power switch crafting, car base assembly, and motor-microcontroller connection).

Sample Code

int speedPin_M1 = 5;     //M1 Speed Control
int speedPin_M2 = 6;     //M2 Speed Control
int directionPin_M1 = 4;     //M1 Direction Control
int directionPin_M2 = 7;     //M1 Direction Control

void setup(){

}

void loop(){
    carAdvance(150,150);
    delay(1000);
    carBack(150,150);
    delay(1000);
    carTurnLeft(150,150);
    delay(1000);
    carTurnRight(150,150);
    delay(1000);
}

void carStop(){                 //  Motor Stop
  digitalWrite(speedPin_M2,0);
  digitalWrite(directionPin_M1,LOW);
  digitalWrite(speedPin_M1,0);
  digitalWrite(directionPin_M2,LOW);
}

void carTurnLeft(int leftSpeed,int rightSpeed){         //Turn Left
  analogWrite (speedPin_M2,leftSpeed);              //PWM Speed Control
  digitalWrite(directionPin_M1,LOW);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M2,HIGH);
}

void carTurnRight(int leftSpeed,int rightSpeed){        //Turn Right
  analogWrite (speedPin_M2,leftSpeed);
  digitalWrite(directionPin_M1,HIGH);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M2,LOW);
}

void carBack(int leftSpeed,int rightSpeed){             //Move backward
  analogWrite (speedPin_M2,leftSpeed);
  digitalWrite(directionPin_M1,LOW);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M2,LOW);
}
void carAdvance(int leftSpeed,int rightSpeed){          //Move forward
  analogWrite (speedPin_M2,leftSpeed);
  digitalWrite(directionPin_M1,HIGH);
  analogWrite (speedPin_M1,rightSpeed);
  digitalWrite(directionPin_M2,HIGH);
}

Result

The robot will cycle through four actions: move forward for 1 second, move backward for 1 second, turn left for 1 second, and turn right for 1 second.

Additional Information

  • Ensure all wiring connections are secure and match the diagram.
  • Verify the battery is installed correctly and the power switch is on.

Was this article helpful?

TOP