I'm working on code that will control micro servo. I use generic servo and built-in library for controling servo. Microcontroller is generic STM32. Controlling servo Works as expected. But I don't want next behavior - attach() function sets servo to 180°(maybe 0°, not sure). In my project I want next:

  • Servo stays at angle before going off/reboot
  • Servo goes to 90° after turning on/reboot

I googled about this problem and tried with write(90); before attach() in setup() function. Servo goes to 90° but after like 50ms attach() stops write(90) function and it goes back to 180°. So this didn't solve my problem. Next I've open Servo.cpp and found that attach() function contains next code:

    bool Servo::attach(uint8 pin,
                   uint16 minPW,
                   uint16 maxPW,
                   int16 minAngle,
                   int16 maxAngle) {
    timer_dev *tdev = PIN_MAP[pin].timer_device;

    if (tdev == NULL) {
        // don't reset any fields or ASSERT(0), to keep driving any
        // previously attach()ed servo.
        return false;

    if (this->attached()) {

    this->pin = pin;
    this->minPW = minPW;
    this->maxPW = maxPW;
    this->minAngle = minAngle;
    this->maxAngle = maxAngle;

    pinMode(pin, PWM);

    timer_set_prescaler(tdev, SERVO_PRESCALER - 1); // prescaler is 1-based
    timer_set_reload(tdev, SERVO_OVERFLOW);

    return true;

What should I do to fix this problem? Servo will adjust angle of PCB which holds LEDs. PCB will be in case and angle of 90° is default angle. 0°/180° will either destory servo or brake case/PCB/something else.

  • 1
    If there are limits to the angles it can travel, I think you need to look at minAngle and maxAngle
    – Gerben
    Mar 16, 2019 at 14:06
  • You showed us the code from the core for the Servo library. That code is KNOWN to work. It works for everyone else. Most likely the mistake is in the code that you wrote that you haven't shown us. Please post the code you are having trouble with if you really want help. It is impossible to help you fix code that I can't see.
    – Delta_G
    Mar 17, 2019 at 16:43
  • @Delta_G Sorry for delayed answer. HLServo.attach(PA8) is in setup() function. Immediately after reboot servo goes to "neutral" angle. So problem lays in servo lib I guess. I'll try with min and max angle parameters.
    – Pararera
    Apr 8, 2019 at 18:13
  • You still aren't showing the code that you are having problems with. It's like showing up at the mechanic to ask what's wrong with your car and not bringing the car with you. Or asking the doctor to tell you what's wrong with your friend that you didn't bring with you. It is impossible to diagnose what we can't see. If you can't post the code that you have that you say isn't working then there isn't anything anyone can really do to help you. Go edit your post and remove the Servo.cpp code. We all have that code already. And instead paste in the code that you have that doesn't work.
    – Delta_G
    Apr 11, 2019 at 0:45

1 Answer 1


You can avoid using the attach(); method by programming without the servo library.

  for(x=0; x<150; x=x+1)
    digitalWrite(Servomotor, HIGH);
    delayMicroseconds(2350); //Change the delay here to change the angle it goes to
    digitalWrite(Servomotor, LOW);

This is an example code for how it worked for me. It sends a pulse to the servo which makes it turn a bit until it reaches a "barrier".

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.