Skip to content

Conflict when using PWM and Servo at the same time #27

@fcooper

Description

@fcooper

OS: Windows 10 64bit
IDE Version: Energia 1.8.7E21
Board: MSP_EXP432E401Y
Core: MSP432 EMT Red v5.25.0

Currently I'm having an issue when using both analogWrite and the Servo library. It appears that the Servo library isn't aware of what timers analogWrite is using. Therefore, the two can conflict which causes weird behavior on both the pwm and servo pins.

Pins I'm using for PWM
P2.7, P2.6, P2.4 and P5.7

Pins I'm using for Servo
P3.7 and P3.6

Base example I used for testing is below

// Sweep
// by BARRAGAN <http://barraganstudio.com> 
// Modified for Energia/Stellaris Launchpad by Kevin Balke <fughilli@gmail.com>
// This example code is in the public domain.

#include <Servo.h> 

#define P2_4  38
#define P2_6  39
#define P2_7  40
#define P5_7  17

#define P3_6  11
#define P3_7  31

Servo myservo1;  // create servo object to control a servo 
Servo myservo2;             // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 

  myservo1.attach(P3_6);  // attaches the servo on Port F, pin 1 (Red LED pin) to the servo object
  myservo2.attach(P3_7);  // attaches the servo on Port F, pin 1 (Red LED pin) to the servo object

  analogWrite(P2_4,50);
  analogWrite(P2_6,100);
  analogWrite(P2_7,150);
  analogWrite(P5_7,200);      
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo1.write(pos);              // tell servo to go to position in variable 'pos' 
    myservo2.write(pos);   
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo1.write(pos);   
    myservo2.write(pos);       // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
} 

Based on the above example
image

Based on the above example but I made P5_7 a servo pin and P3_7 a pwm pin.
image

Based on the above example but I made P5_7 a servo pin and P3_6 a pwm pin.
image

As you can see at times depending on the order that pwm and servo pins are first used the output can vary. All six pins should have some kind of output but that isn't the case with the above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions