ServoTimer2 Library Compilation Error

Hi, I am wondering if someone can help. I have a problem using arduino Servo library. I have arduino Uno R3 board. To inject PWM to ESC to run a brushless motor.

When I use FreqCount library + Servo Library , it does not work. I been searching every corner of the internet to find a solution. The problem is that, the timer1 is used on both library that causing the issue.

I have found few posts on the internet suggesting to use ServoTimer2 library. Which I have installed and was trying to use it like Servo library. If I use the same code for Servo.h , it works fine, but does not work for ServoTimer2.h.

But there is a compilation error.

here is my code

#include <ServoTimer2.h>

#define esc_pin 6
#define pot_input_pin A0

int pot_value;
int pwm_frequency;
int min_pulse_width = 1000;
int max_pulse_width = 2000;

ServoTimer2 PWM;

void setup() {
  // put your setup code here, to run once:

  pinMode(esc_pin, OUTPUT);
  pinMode(pot_input_pin, INPUT);

  PWM.attach(esc_pin, min_pulse_width, max_pulse_width);

  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:

      pot_value = analogRead(pot_input_pin);
      pwm_frequency = map(pot_value, 0, 1023, 0, 180);
      PWM.write(pwm_frequency);

      Serial.print(pwm_frequency);
      delay(500);
}

errors

-------------------------------------------------

C:\Users\OLI~1.AHM\AppData\Local\Temp\cc5XJxoo.ltrans0.ltrans.o: In function `setup':
C:\Users\Misc\Arduino Giga R1\Sketch\ServoTimer2_Sketch/ServoTimer2_Sketch.ino:19: undefined reference to `ServoTimer2::attach(int, int, int)'
collect2.exe: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

Hello.

From the error message, it sounds like the ServoTimer2 library you’re using might not support setting the min and max pulse width using the attach() function (e.g. it might only accept the pin number as an argument). However, I am not familiar with that library, so I suggest you check its documentation to double check. For general Arduino support like this, I recommend posting on Arduino’s forum.

In case it helps, we have some example code for our Zumo robot that shows now to control a single servo from an Arduino Uno using Timer 2 (without using a library), which you can find in the “Controlling a servo with an Arduino Uno” section of the Zumo shield’s user’s guide.

Brandon

1 Like

Hi, Much appreciated for your reply. Looking in the documentation, I think you right that it does not support additional argument.

The reason I have posted here, the google algorithm lead me to this forum which seems much more active and helpful.

1 Like