Issue with PWM signal if Servo class is used in a class

I got a strange problem with my program. I want to control a servo (SG92R). To clean up my code, I created a class and moved the Servo object within the new class. This, however, let the servo jiggle around. It moves some degrees with a frequency >10Hz.

I reduced my code to a minimum that works well (the servo doesn’t move):

#include <Servo.h>
Servo servo;
void setup() {
servo.attach(5);
servo.write(0);
}
void loop() {}

If I shift this servo object in a class called Shutter, the jiggeling starts:

main file:

#include "Shutter.h"
Shutter shutter(5);
void setup() {}
void loop() {}

with the Shutter.h

#ifndef Shutter_h
#define Shutter_h
#include "Arduino.h"
#include <Servo.h>

class Shutter
{
  public:
    Shutter(int pwmPin);
  private:
    Servo _servo;
    int _pwm;
};
#endif

and Shutter.cpp

#include "Arduino.h"
#include "Shutter.h"
#include <Servo.h>
Servo _servo;
Shutter::Shutter(int pwm)
{
  _pwm =pwm;
  _servo.attach(_pwm); 
  _servo.write(0);
}

I used a scope to look at the PWM signal and found a small dip. You find the picture attached. This dip doesn’t appear in the first case and is most likely to cause my problem.

Can anyone explain to me what happens here and why the two approaches are different?

Thanks!

The _servo declaraion in the Servo.cpp is definitely wrong. Remove that line.

Don’t call the attach() and write() methods in the Shutter constructor. Move that code to a begin() or init() method and call that inside setup(). Report results.

Thanks! It was the "Servo _servo;" line.