2

I would like to use the Radiohead library and control a servo from the same Leonardo board.

Now the Radiohead library occupies timer1 and therefore cant be used together with the servo library. (It does offer to use timer2 which the Leonardo does not have.)

Are there any other Servo libraries that use timer3 or timer4 which the Leonardo has?

I have already tried:

0

3 Answers 3

1

I wonder if you could use this PWM version of the Servo library written for the Arduino MEGA? Looking at the code, it looks like you would have to remove the "else if (pin == 44)" and "else if (pin == 45)" blocks of code in the attach() and detach() functions in Update Servo_Hardware_PWM.cpp. Just guessing here. I haven't tried it out.

https://github.com/dadul96/Arduino-Servo-Hardware-PWM-Library

1
  • Comment from an anon user: Sorry, this won't work. I checked the datasheet for the ATMega32u4 and Timers 3 and 4 can't be used to change the state of a pin. So no, hardware servo control using the above code on a Leonardo. Commented May 9, 2019 at 7:36
1

You could try manually sending the servo pulses with something like:

digitalWrite(SERVOPIN,HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(SERVOPIN,LOW);

As long as the duration of the pulses is consistent, it doesn't matter if they don't occur regularly.

Downsides will be that there may be jitter if there are interrupts running, although there is the advantage that this will work on just about any pin.

If you are looking for a simple proof of concept, this may enough to test it out.

1
+50

In file ServoTimers.hin standard Arduino Servo library change the timer

#elif defined(__AVR_ATmega32U4__)
#define _useTimer3
typedef enum { _timer3, _Nbr_16timers } timer16_Sequence_t;

on Windows the file ServoTimers.h is located in C:\Program Files (x86)\Arduino\libraries\Servo\src\avr

6
  • thanks, but this leads me to the compile error described here: forum.arduino.cc/index.php?topic=527356.0
    – clamp
    Commented May 16, 2019 at 19:03
  • you get that error only if you don't change the timer number to 3 as I write in the Answer
    – Juraj
    Commented May 16, 2019 at 19:15
  • oki fixed the compile error. now the problem is that as soon as the servo is connected (pin 5) the radiohead library seems to stop working.
    – clamp
    Commented May 16, 2019 at 19:19
  • could it be a powering problem?
    – Juraj
    Commented May 16, 2019 at 19:31
  • could be, i will try to run it with external power supply and not the usb.
    – clamp
    Commented May 16, 2019 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.