Why is DC motor not rotating? I am using Arduino uno and BTS7960 Motor driver

const int ENCA1 = 2;
const int ENCB1 = 3;
const int LPWM = 10;
const int RPWM = 9;
const int REN = 4;
const int LEN = 5;
const int RIS = 6;
const int LIS = 7;
int pos1 = 0;
void setup() {
  Serial.begin(9600);
  pinMode(RIS, OUTPUT);
  pinMode(REN, OUTPUT);
  pinMode(RPWM, OUTPUT);
  pinMode(LIS, OUTPUT);
  pinMode(LEN, OUTPUT);
  pinMode(LPWM, OUTPUT);
  pinMode(ENCA1, INPUT);
  pinMode(ENCB1, INPUT);
  attachInterrupt(digitalPinToInterrupt(ENCA1), readEncoder1, RISING);
  Serial.println(pos1);
}
void loop() {
  analogWrite(RPWM, 240);
  analogWrite(LPWM, 0);
  Serial.print(" ");
  Serial.print(pos1);
  Serial.println();
}
void readEncoder1() {
  int b1 = digitalRead(ENCB1);
  if (b1 > 0) {
    pos1++;
  } else {
    pos1--;
  }
}

Please post a schematic of your project and details of how it is powered and which Arduino you are using

1 Like

I am using Arduino Uno and BTS7960 motor driver. I have connected Arduino board to PC by using USB cable. I have given 12V power supply to driver by 12V adapter.

Label the pins with self-describing names to better understand the programme logic.

Please post a schematic (aka "circuit diagram").
A schematic is a far better way to show connections than text - eg, what does "given 12V power" actually mean?

Also, some good clear photos of your setup would help.

I think you need trying to use high and low output for the motor output not with pwm, so you can check your diagram or wiring mistake. Check your wiring and component before using it.

I am supplying different voltage in terms PWM. If I gave HIGH and LOW, then it means 12V and 0V.

I got the answer. By the way thanks you all. Code is

const int ENCA1 = 2;
const int ENCB1 = 3;
const int LPWM = 10;
const int RPWM = 9;
const int REN = 4;
const int LEN = 5;
const int RIS = 6;
const int LIS = 7;
int pos1 = 0;
void setup() {
  Serial.begin(9600);
  pinMode(RIS, OUTPUT);
  pinMode(REN, OUTPUT);
  pinMode(RPWM, OUTPUT);
  pinMode(LIS, OUTPUT);
  pinMode(LEN, OUTPUT);
  pinMode(LPWM, OUTPUT);
  pinMode(ENCA1, INPUT);
  pinMode(ENCB1, INPUT);
  digitalWrite(RIS, LOW);
  digitalWrite(LIS, LOW);
  digitalWrite(REN, HIGH);
  digitalWrite(LEN, HIGH);

  attachInterrupt(digitalPinToInterrupt(ENCA1), readEncoder1, RISING);
  Serial.println(pos1);
}
void loop() {
  analogWrite(RPWM, 240);
  analogWrite(LPWM, 0);
  Serial.print(" ");
  Serial.print(pos1);
  Serial.println();
}
void readEncoder1() {
  int b1 = digitalRead(ENCB1);
  if (b1 > 0) {
    pos1++;
  } else {
    pos1--;
  }
}

And the answer was what ?

pos1 should be qualified "volatile", and must be read and copied in non-interrupt context with interrupts disabled, because it cannot be read atomically.

He updated the condition high and low, please update this topic being solved

Thank you. Can you explain in detail? OR can you post code here?

Can you look at once? Is it right?

const int ENCA1 = 2;
const int ENCB1 = 3;
const int LPWM = 10;
const int RPWM = 9;
const int REN = 4;
const int LEN = 5;
const int RIS = 6;
const int LIS = 7;
volatile long pos1 = 0;
void setup() {
  Serial.begin(9600);
  pinMode(RIS, OUTPUT);
  pinMode(REN, OUTPUT);
  pinMode(RPWM, OUTPUT);
  pinMode(LIS, OUTPUT);
  pinMode(LEN, OUTPUT);
  pinMode(LPWM, OUTPUT);
  pinMode(ENCA1, INPUT);
  pinMode(ENCB1, INPUT);
  digitalWrite(RIS, LOW);
  digitalWrite(LIS, LOW);
  digitalWrite(REN, HIGH);
  digitalWrite(LEN, HIGH);

  attachInterrupt(digitalPinToInterrupt(ENCA1), readEncoder1, RISING);
  Serial.println(pos1);
}
void loop() {
  analogWrite(RPWM, 240);
  analogWrite(LPWM, 0);
  Serial.print(" ");
  Serial.print(pos1);
  Serial.println();
}
void readEncoder1() {
  int b1 = digitalRead(ENCB1);
  if (b1 > 0) {
    pos1++;
  } else {
    pos1--;
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.