PWM controls for bi-directional motor

I am very new to electronics and working on a code for a work project. I need to add PWM to my code to control both the duty cycle and frequency. I need it ran in the 10kHz range. Currently the code contains two push buttons that control the motor’s direction. (One button for forward and one for reverse). The motor is normally off and then activated by the use of the push buttons. I do not have great code tags because I am not sure I thoroughly understand the code. I do not have a very technical background so please try to keep things simple. Thanks. I copied and pasted the code as well as attached it.

#define IS_1 A0
#define IS_2 A1
#define IN_1 3
#define IN_2 11
#define INH_1 12
#define INH_2 13
#define TCONST 100 // Delay Time between Steps
int buttonApin = 7;
int buttonBpin = 8;
int val = 0;

byte leds = 0;

void setup()
{
pinMode(buttonApin, INPUT_PULLUP); //declare push buttons as an input
pinMode(buttonBpin, INPUT_PULLUP); //declare push button as an input
pinMode (IN_1, OUTPUT );
pinMode (IN_2, OUTPUT );
pinMode (INH_1, OUTPUT );
pinMode (INH_2, OUTPUT );

reset_ports ();

digitalWrite (INH_1,1);
digitalWrite (INH_2,1);
}

void motorOn (int vel)
{

analogWrite (11, 0);
analogWrite (3, vel);}

void motorBack (int vel) //vel is the input value of the function
{
analogWrite (3, 0);
analogWrite (11, vel);
}
void reset_ports ()
{
digitalWrite (IN_1,0);
digitalWrite (IN_2,0);
}
void loop()
{
if (digitalRead(buttonApin) == LOW)
{
motorBack(200);
}
if (digitalRead(buttonBpin) == LOW)
{
motorOn(200);
}
}

mirror_code_final.ino (979 Bytes)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What motor contol board do you have and what model Arduino.

Thanks… Tom… :slight_smile:

Tom,
I am using an Infineon BTN8982TA and Arduino Uno. I think i added the code tags right.

#define IS_1 A0
#define IS_2 A1
#define IN_1 3
#define IN_2 11
#define INH_1 12
#define INH_2 13
#define TCONST 100       // Delay Time between Steps
int buttonApin = 7;
int buttonBpin = 8;
int val = 0;

byte leds = 0;

void setup() 
{
  pinMode(buttonApin, INPUT_PULLUP); //declare push buttons as an input  
  pinMode(buttonBpin, INPUT_PULLUP); //declare push button as an input
  pinMode (IN_1, OUTPUT );
  pinMode (IN_2, OUTPUT );
  pinMode (INH_1, OUTPUT );
  pinMode (INH_2, OUTPUT ); 
  
  reset_ports ();
  
  digitalWrite (INH_1,1);
  digitalWrite (INH_2,1);
}

void motorOn (int vel)
{
  
analogWrite (11, 0);
analogWrite (3, vel);}

void motorBack (int vel) //vel is the input value of the function
{
analogWrite (3, 0);
analogWrite (11, vel);
}
void reset_ports ()
{
  digitalWrite (IN_1,0);
  digitalWrite (IN_2,0);
}
void loop() 
{
  if (digitalRead(buttonApin) == LOW)
  {
    motorBack(200);
  }
  if (digitalRead(buttonBpin) == LOW)
  {
    motorOn(200);
  }
}

Attached are the schematics I was provided with for the Arduino and shield. And also the manual for the shield.

Hi,
Your code already has PWM commands in it.

analogWrite are PWM output commands.

https://www.arduino.cc/en/Reference/AnalogWrite

Tom... :slight_smile:

Further reading: PWM tutorial

Why are you giving names to the I/O pins and then not using them in

analogWrite (11, 0);
analogWrite (3, vel);}

Using names everywhere makes code much easier to debug.

You can change the frequency of the PWM pulses by writing different values to the Atmega328 hardware timer registers. The Atmega 328 datasheet has all the details. If you want to change the frequency don't use the PWM pins that are associated with Timer0 as that timer is used for micros() and millis().

...R