General

Brushless DC Motor with Encoder 12V 159RPM from DFRobot

userHead Bernhard.Kreutzer 2024-05-18 21:18:00 109 Views2 Replies

Hi everybody,

 

I have a Brushless DC Motor with Encoder 12V 159RPM from DFRobot, and I would like to control it using an Arduino. My goal is to have the motor's shaft rotate 45 degrees every 2 seconds. Could someone please help me with the code to achieve this?

 

Thank you!

2024-06-19 23:24:04

You can try this code. This is to rotate the motor shaft by 45 degrees every 2 seconds:

 

// Include the necessary libraries
#include <Encoder.h>

// Define constants
const int encoderPinA = 2;    // Encoder channel A connected to digital pin 2
const int encoderPinB = 3;    // Encoder channel B connected to digital pin 3
const int motorControlPin = 9; // Example pin for motor control, adjust as needed

// Create an Encoder object
Encoder myEncoder(encoderPinA, encoderPinB);

// Variables to track motor position
volatile long currentPosition = 0;  // Current position in encoder counts
long targetPosition = 0;            // Target position in encoder counts
int stepsPerRevolution = 360;       // Number of encoder counts per revolution

// Variables for timing
unsigned long previousTime = 0;     // Time storage for elapsed time calculation
unsigned long interval = 2000;      // Interval between movements in milliseconds (2 seconds)

void setup() {
 // Initialize serial communication
 Serial.begin(9600);
 
 // Attach interrupt for encoder channel A to update position
 attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoder, CHANGE);

 // Initialize motor control pin as output
 pinMode(motorControlPin, OUTPUT);
 
 // Move the motor to initial position (rotate 45 degrees from current position)
 targetPosition = currentPosition + (stepsPerRevolution / 8); // 45 degrees in encoder counts
 moveMotorToPosition(targetPosition);
}

void loop() {
 // Calculate elapsed time since last movement
 unsigned long currentTime = millis();
 unsigned long elapsedTime = currentTime - previousTime;
 
 // Check if it's time to move the motor
 if (elapsedTime >= interval) {
   // Update previous time to current time
   previousTime = currentTime;
   
   // Update target position for the next movement
   targetPosition = currentPosition + (stepsPerRevolution / 8); // 45 degrees in encoder counts
   
   // Move the motor to the target position
   moveMotorToPosition(targetPosition);
 }
 
 // Other loop tasks can be placed here
}

// Function to update encoder position
void updateEncoder() {
 // Read the direction of rotation
 int encoderValue = digitalRead(encoderPinB);
 
 // Update current position based on direction
 if (encoderValue == HIGH) {
   currentPosition++;
 } else {
   currentPosition--;
 }
 
 // Debugging: print current position
 Serial.print("Current Position: ");
 Serial.println(currentPosition);
}

// Function to move the motor to a specified encoder position
void moveMotorToPosition(long target) {
 // Determine direction of rotation
 if (target > currentPosition) {
   // Rotate clockwise
   digitalWrite(motorControlPin, HIGH);  // Example: Activate motor clockwise
 } else {
   // Rotate counterclockwise
   digitalWrite(motorControlPin, LOW);   // Example: Activate motor counterclockwise
 }
 
 // Optionally, you can implement PID control or other methods to control motor movement
 
 // Example: Delay for motor to reach the target position (adjust as necessary)
 delay(500);  // Adjust based on motor response time
 
 // Stop the motor (you may need to implement this based on your motor driver)
 digitalWrite(motorControlPin, LOW);
}
 

userHeadPic lia.ifat
2024-06-19 19:17:07

Hello, 

Try my code https://www.dfrobot.com/forum/topic/334636

userHeadPic maxxlife