Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!

Controlling Two DC Motors with Aptinex Dagaya Pulse Duo

Dagaya Pulse Duo makes it easy to control two bidirectional, high-power, brushed DC motors with any microcontroller or any other dev board.

BeginnerProtip733
Controlling Two DC Motors with Aptinex Dagaya Pulse Duo

Things used in this project

Hardware components

Dagaya Pulse Duo – VNH5019 Based Dual Motor Driver
×1
Lakduino Dwee 1284 Mini Pro
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Connection Diagram

Make connections as shown in below

Connection for Control of two motors according to user given commands.

Connect two motors with DAGAYA Surge Duo Motor Driver and Arduino. Use USB cable to send data to Arduino through GUI application

Code

Code for the test module

Arduino
Upload this code to test breaks, direction change and PWM of the motor
// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
void setup()
{
// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void demoOne()
{
// this function will run the motors in both directions at a fixed speed
// turn on motor A
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enA, 200);
// turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 200);
delay(2000);
// now change motor directions
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void demoTwo()
{
// this function will run the motors across the range of possible speeds
// note that maximum speed is determined by the motor itself and the operating voltage
// the PWM values sent by analogWrite() are fractions of the maximum speed possible
// by your hardware
// turn on motors
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
// accelerate from zero to maximum speed
for (int i = 0; i < 256; i++)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
// decelerate from maximum speed to zero
for (int i = 255; i >= 0; --i)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop()
{
demoOne();
delay(1000);
demoTwo();
delay(1000);
}

Arduino Code for accept user given commands.

Arduino
Upload the code and give commands through serial monitor to control two motors as you want.
/ Pin assignments
const int motorAPin = 9;    // PWM pin for Motor A (motor 2)
const int motorBPin = 10;   // PWM pin for Motor B (motor 1)
const int dirAPin = 7;      // Forward direction pin for Motor A
const int revAPin = 8;      // Reverse direction pin for Motor A
const int dirBPin = 11;     // Forward direction pin for Motor B
const int revBPin = 12;    // Reverse direction pin for Motor B
//const int brakePinA = 6;    // Brake pin for Motor A
//const int brakePinB = 5;    // Brake pin for Motor B
int speed=0;


void setup() {
  pinMode(motorAPin, OUTPUT);
  pinMode(motorBPin, OUTPUT);
  pinMode(dirAPin, OUTPUT);
  pinMode(revAPin, OUTPUT);
  pinMode(dirBPin, OUTPUT);
  pinMode(revBPin, OUTPUT);
  pinMode(6, OUTPUT); //pinMode(brakePinA, OUTPUT);
  pinMode(5, OUTPUT); //pinMode(brakePinB, OUTPUT);
  

  Serial.begin(9600);
  Serial.println("Enter commands to control motors:");
}

void loop() {
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\n');
    command.trim();
    command.toUpperCase(); // Ensure command is in uppercase
   
    if (command == "AON") {
      digitalWrite(motorAPin, HIGH); // Max speed
      //digitalWrite(brakePinA, LOW); // Release brake
      Serial.println("Motor A ON");
    }
    else if (command == "AOFF") {
     
      digitalWrite(motorAPin, LOW); // Stop motor
      Serial.println("Motor A OFF");
    }
    else if (command == "ADIRFWD") {
       digitalWrite(motorAPin, HIGH);
      digitalWrite(dirAPin, HIGH); // Set direction to forward
      digitalWrite(revAPin, LOW);
       analogWrite(motorAPin, speed);
      Serial.println("Motor A Forward");
    }
    else if (command == "ADIRREV") {
       digitalWrite(motorAPin, HIGH);
      digitalWrite(revAPin, HIGH); // Set direction to reverse
      digitalWrite(dirAPin, LOW);
       analogWrite(motorAPin, speed);
      Serial.println("Motor A Reverse");
    }
    /*else if (command == "ABRAKE") {
      digitalWrite(brakePinA, HIGH); // Engage brake
      Serial.println("Motor A Brake");
    }*/
    else if (command.startsWith("ASPEED")) {
       speed = command.substring(6).toInt();
      speed = constrain(speed, 0, 255); // Ensure speed is within 0-255
      analogWrite(motorAPin, speed);
      Serial.println("Motor A Speed: " + String(speed));
    }
   
    // Similar controls for Motor B
    else if (command == "BON") {
      digitalWrite(motorBPin, HIGH);
      //digitalWrite(brakePinB, LOW);
      Serial.println("Motor B ON");
    }
    else if (command == "BOFF") {
      digitalWrite(motorBPin, LOW);
      Serial.println("Motor B OFF");
    }
    else if (command == "BDIRFWD") {
      digitalWrite(motorBPin, HIGH);
      digitalWrite(dirBPin, HIGH);
      digitalWrite(revBPin, LOW);
      analogWrite(motorBPin, speed);
      Serial.println("Motor B Forward");
    }
    else if (command == "BDIRREV") {
      digitalWrite(motorBPin, HIGH);
      digitalWrite(revBPin, HIGH);
      digitalWrite(dirBPin, LOW);
      analogWrite(motorBPin, speed);
      Serial.println("Motor B Reverse");
    }
    /*else if (command == "BBRAKE") {
      digitalWrite(brakePinB, HIGH);
      Serial.println("Motor B Brake");
    }*/
    else if (command.startsWith("BSPEED")) {
      int speed = command.substring(6).toInt();
      speed = constrain(speed, 0, 255); // Ensure speed is within 0-255
      analogWrite(motorBPin, speed);
      Serial.println("Motor B Speed: " + String(speed));
    }
    else {
      Serial.println("Invalid Command");
    }
  }
}

Python code for build the GUI application on windows to control two Motors.

Python
Give commands to the Arduino board using this GUI application.
import tkinter as tk
from tkinter import messagebox
import serial
import threading
import time

SERIAL_PORT = 'COM3'
BAUD_RATE = 9600
ser = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1)
def send_command(command):
    ser.write((command + '\n').encode())

def motor_a_on():
    send_command('AON')

def motor_a_off():
    send_command('AOFF')

def motor_a_dir_fwd():
    send_command('ADIRFWD')

def motor_a_dir_rev():
    send_command('ADIRREV')

#def motor_a_brake():
    #send_command('ABRAKE')

def motor_a_speed(val):
    send_command(f'ASPEED{int(float(val))}')

def motor_b_on():
    send_command('BON')

def motor_b_off():
    send_command('BOFF')

def motor_b_dir_fwd():
    send_command('BDIRFWD')

def motor_b_dir_rev():
    send_command('BDIRREV')

#def motor_b_brake():
    #send_command('BBRAKE')

def motor_b_speed(val):
    send_command(f'BSPEED{int(float(val))}')

def read_serial():
    while True:
        if ser.in_waiting:
            line = ser.readline().decode().strip()
            if line:
                print(f"Arduino: {line}")

# Start a thread to read serial data
threading.Thread(target=read_serial, daemon=True).start()

# Build the GUI
root = tk.Tk()
root.title("Motor Control GUI")

# Motor A Controls
motor_a_frame = tk.LabelFrame(root, text="Motor A Controls", padx=10, pady=10)
motor_a_frame.grid(row=0, column=0, padx=10, pady=10)

tk.Button(motor_a_frame, text="ON", width=10, command=motor_a_on).grid(row=0, column=0, pady=5)
tk.Button(motor_a_frame, text="OFF", width=10, command=motor_a_off).grid(row=0, column=1, pady=5)
tk.Button(motor_a_frame, text="Forward", width=10, command=motor_a_dir_fwd).grid(row=1, column=0, pady=5)
tk.Button(motor_a_frame, text="Reverse", width=10, command=motor_a_dir_rev).grid(row=1, column=1, pady=5)
#tk.Button(motor_a_frame, text="Brake", width=10, command=motor_a_brake).grid(row=2, column=0, columnspan=2, pady=5)
tk.Label(motor_a_frame, text="Speed").grid(row=3, column=0, columnspan=2)
motor_a_speed_slider = tk.Scale(motor_a_frame, from_=0, to=255, orient=tk.HORIZONTAL, command=motor_a_speed)
motor_a_speed_slider.set(255)
motor_a_speed_slider.grid(row=4, column=0, columnspan=2, pady=5)

# Motor B Controls
motor_b_frame = tk.LabelFrame(root, text="Motor B Controls", padx=10, pady=10)
motor_b_frame.grid(row=0, column=1, padx=10, pady=10)

tk.Button(motor_b_frame, text="ON", width=10, command=motor_b_on).grid(row=0, column=0, pady=5)
tk.Button(motor_b_frame, text="OFF", width=10, command=motor_b_off).grid(row=0, column=1, pady=5)
tk.Button(motor_b_frame, text="Forward", width=10, command=motor_b_dir_fwd).grid(row=1, column=0, pady=5)
tk.Button(motor_b_frame, text="Reverse", width=10, command=motor_b_dir_rev).grid(row=1, column=1, pady=5)
#tk.Button(motor_b_frame, text="Brake", width=10, command=motor_b_brake).grid(row=2, column=0, columnspan=2, pady=5)
tk.Label(motor_b_frame, text="Speed").grid(row=3, column=0, columnspan=2)
motor_b_speed_slider = tk.Scale(motor_b_frame, from_=0, to=255, orient=tk.HORIZONTAL, command=motor_b_speed)
motor_b_speed_slider.set(255)
motor_b_speed_slider.grid(row=4, column=0, columnspan=2, pady=5)

root.mainloop()

Credits

Pasan Dharmasiri
17 projects • 3 followers
Contact
Chathura Yapa Bandara
19 projects • 5 followers
Contact
Bhuvisara Dharmasena
5 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.