Can someone put some PWM motor control samples in the IDE?

All, I had trouble finding some simple example code for controlling a standard DC motor using an Arduino.

This must be a popular thing to do. In the end I wrote my own example, which works OK, here it is.

It would be helpful to have something like this as a standard example.

//Standard PWM DC control
int pwmPinM1 = 5;   //M1 Speed Control
int pwmPinM2 = 6;   //M2 Speed Control
int dirPinM1 = 4;   //M1 Direction Control
int dirPinM2 = 7;   //M1 Direction Control

void controlM1(boolean forward, char sp)
{
  char pwmVal = forward == false ? sp : 255 - sp;
  digitalWrite(dirPinM1, forward);
  analogWrite(pwmPinM1, pwmVal); 
}

void controlM2(boolean forward, char sp)
{
  char pwmVal = forward == false ? sp : 255 - sp;
  digitalWrite(dirPinM2, forward);
  analogWrite(pwmPinM2, pwmVal); 
}

void forwardM1(char sp)
{
  controlM1(true, sp);
}

void forwardM2(char sp)
{
  controlM2(true, sp);
}

void backwardM1(char sp)
{
  controlM1(false, sp);
}

void backwardM2(char sp)
{
  controlM2(false, sp);
}

void stopM1(void)
{
  // Stop using backward because of a known issue
  // with low(ish) PWM values on pin 5 and 6
  backwardM1(0);
}
 
void stopM2(void)
{
  // Stop using backward because of a known issue
  // with low(ish) PWM values on pin 5 and 6
  backwardM2(0);
}

void setup(void) 
{ 
  pinMode(dirPinM1, OUTPUT);
  pinMode(pwmPinM1, OUTPUT); // May not be necessary
  pinMode(dirPinM2, OUTPUT);
  pinMode(pwmPinM2, OUTPUT); // May not be necessary
}

void loop(void) 
{
  // Both forward the hard way
  controlM1(true, 255);
  controlM2(true, 255);
  
  delay(5000);
  
  // Both backwards the hard way
  controlM1(false, 255);
  controlM2(false, 255);
  
  delay(5000);
  
  // Use a helper to turn in opposite directions
  forwardM1(100);
  backwardM2(100);
  
  delay(5000);
  
  // Use a helper to turn in opposite directions
  backwardM1(100);
  forwardM2(100);
  
  delay(5000);
  
  // Use a helper to stop both motors
  stopM1();
  stopM2();
  
  delay(5000);
}

Bi-directional DC motor control requires a motor control shield. In most cases you can find a library for your motor control shield and that library will come with examples.

A motor need not be driven by a shield. However, as stated, it is better that example software is maintained by the vendor.

As you have noted, example software should be succinct and lend itself to experimentation.
It should also make pitfalls clear. In this case, not applying both forward and backward signals at the same time.

Hardware can overcome this problem allowing one line to select direction, the other control speed. This might be done via TTL/CMOS logic (integrated drivers, or using discreet components - the latter being a better way to learn) or a neat solution is to use a hybrid H-Bridge made of a DPDT (or two DPST) relay and a FET. An advantage of using relays is that they (tend) to be break-before-make, reducing the need to manage timing between applying back or forward signals. The positive leg of the relay H-Bridge is supplied via a FET to manage the PWM signal. Selection of the relay contact material is important, and often whetted or self-cleaning types may be advantageous.

Slightly off topic - have you noticed that some sealed relays have a little pip on them, usually in the corner? This is to improve shelf-life [edit] and to protect the contacts during manufacture [/edit], this pip should be broken open when in service to improve contact life.

Back EMF Breaking [BEMFB] may be required. BEMFB usually requires a further control line (although simple BEMFB can be implemented with two lines), and can be extremely violent! For this reason, PWM may be used to manage the motor shunt (short circuit).

As you can see, bi-directional motor control can range from crude, full speed fore & aft, right the way through to variable speed control and braking control. The more you invest in control logic, the easier it is to control safely.

Thanks for your replies, for a normal DC motor I had assumed than the best method would be to swing one terminal high or low to control direction and apply a direction compensated PWM signal to the other terminal, this seems to work OK but I couldn't find any example. I was not suggesting connecting the motor directly to an Arduino, I have a simple driver chip isolating my motors.