Problem with controlling 12v dc motor


const int motorIn1 = 7;
const int motorIn2 = 6;
const int enableA = 11;  // Pin 10 used for PWM

void setup() {
  // Initialize the motor direction control pins
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  
  // Set Pin 10 (enableA) for PWM output
  pinMode(enableA, OUTPUT);

  // Set motor direction
  digitalWrite(motorIn1, HIGH);
  digitalWrite(motorIn2, LOW);

  // Configure Timer 1 for 10 kHz frequency on pin 10 (OC1B)
  TCCR1A = 0;  // Clear Timer/Counter Control Register A
  TCCR1B = 0;  // Clear Timer/Counter Control Register B

  // Set Fast PWM mode with ICR1 as TOP
  TCCR1A = (1 << WGM11) | (1 << COM1B1); // Mode 14: Fast PWM, clear OC1B on compare match
  TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10); // No prescaler

  // Set the top value for 10 kHz PWM
  ICR1 = 1600;

  // Set initial duty cycle to 50%
  OCR1B = 800;  // 50% duty cycle is half speed
}

void loop() {
  // The loop is empty because the setup configuration runs the motor
}

So basically I can't even get the Dc motor to run at all. I asked the manufacturer and they told me I need a PWM frequency of 10khz for it to work and my code did that I think. However nothing works. Can anyone tell me what's wrong with my circuit/code? Thanks in advance.

What type of dc motor do you have?
Measure the voltage sent to the motor...

the motor specs is here

How do I measure the voltage to the motor exactly? Is using a voltmeter okay?

Can you knock out a schematic that clearly shows your connections and devices.

I'm looking right at that dinky little 12V battery. I'd be surprised if it has the amperage to run the motor controller, but there's no way it can supply the 540mA needed to run a single motor. It's just not going to happen.

1 Like

You have not connected +5 V on the driver board. It is needed for the logic.

1 Like

It looks like they've got the 5V enable jumper in, which, if I remember how those modules work correctly, means that it's using its onboard 5V regulator which is being supplied by the 12V battery. If you look carefully at the picture, you can just see the top of the jumper, and the glow of the LED which would indicate that the 5V supply is up and running.

This battery for that motor? LoL!

Your motor needs over 5A to start. I doubt that battery can supply 1/10th of that.

the battery package says it's a 12v23A battery so I thought it is enough

Can you post a link to that please.

Here Powercell Alkaline Batteries 23A 12V Pack of 20 Batteries A23 MN21 MN23 L1028 V23GA LRV08 8LR932 for Remote Control Gate Anti-Theft Bell Alarm: Amazon.de: Electronics & Photo

Bad news, I'm afraid. "23A" is a code for the physical size of a battery, not it's maximum current output.

You're gonna need a bigger battery...

1 Like

Damn... okay I am gonna buy a bigger battery, thanks for answering

The issue with the battery is not the only problem.

I tried out your code.
It worked on an Uno R3 after changing the 'enableA' pin back to 10 (as per the comments).
I got a 10kHz signal with 50% duty cycle on pin 10.
On a Uno R3, pin 10 is port B2.

But it did not work on a Mega2560.
On a Mega2560, pin 10 is port B4.
As far as I can see port B2 is not used on the Mega2560.

I am sorry, but what exactly is the difference between B2 and B4? Which part of the code do I have to change to make it work on the Mega2560?

Nevermind, I think this new code is now okay with mega2560, I have changed it to OC2A. Can you help me to test it again?

const int motorIn1 = 7;
const int motorIn2 = 6;
const int enableA = 10;  // Pin 10 used for PWM

void setup() {
  // Initialize the motor direction control pins
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  
  // Set Pin 10 (enableA) for PWM output
  pinMode(enableA, OUTPUT);
  
  // Set motor direction
  digitalWrite(motorIn1, HIGH);
  digitalWrite(motorIn2, LOW);
  
  // Configure Timer 2 for 10 kHz frequency on pin 10 (OC2A)
  TCCR2A = 0;  // Clear Timer/Counter Control Register A
  TCCR2B = 0;  // Clear Timer/Counter Control Register B
  
  // Set Fast PWM mode with OCR2A as TOP
  TCCR2A = (1 << WGM20) | (1 << WGM21) | (1 << COM2A1); // Fast PWM, clear OC2A on compare match
  TCCR2B = (1 << WGM22) | (1 << CS20);  // No prescaler
  
  // Set the top value for 10 kHz PWM
  OCR2A = 159;  // For Fast PWM mode with OCR2A as TOP
  
  // Set initial duty cycle to 50%
  OCR2B = 80;  // 50% duty cycle is half speed
}

void loop() {
  // The loop is empty because the setup configuration runs the motor
}

I don't think that is true. The usual reason for using a higher PWM frequency is to avoid unpleasant audible noise from the motor. But the motor itself doesn't care much what the PWM frequency is.

I suppose it's possible that a lower PWM frequency could interfere with the encoder circuit in some way.

1 Like

You seem to have marked the topic as solved. That may discourage forum members from responding because they think you don't need any more help. Better un-mark it!

1 Like

I've tried your latest code without success.

You've gone from using timer 1 to using timer 2.
Each timer is associated with particular pins, so you can't just change timers and expect it to work on the same pin.

I find the following website useful for learning about operation of the timers:

Thanks for testing again. If that's the case, it should work when I use pin12 right? As pin12 on Arduino mega is OC1B, which would be the same as pin10 on Arduino uno.

const int motorIn1 = 7;
const int motorIn2 = 6;
const int enableA = 12;  // Pin 12 used for PWM

void setup() {
  // Initialize the motor direction control pins
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  
  // Set Pin 10 (enableA) for PWM output
  pinMode(enableA, OUTPUT);

  // Set motor direction
  digitalWrite(motorIn1, HIGH);
  digitalWrite(motorIn2, LOW);

  // Configure Timer 1 for 10 kHz frequency on pin 12 (OC1B)
  TCCR1A = 0;  // Clear Timer/Counter Control Register A
  TCCR1B = 0;  // Clear Timer/Counter Control Register B

  // Set Fast PWM mode with ICR1 as TOP
  TCCR1A = (1 << WGM11) | (1 << COM1B1); // Mode 14: Fast PWM, clear OC1B on compare match
  TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS10); // No prescaler

  // Set the top value for 10 kHz PWM
  ICR1 = 1600;

  // Set initial duty cycle to 50%
  OCR1B = 800;  // 50% duty cycle is half speed
}

void loop() {
  // The loop is empty because the setup configuration runs the motor
}