Using servo library in tinkercad causing weird behaviour

328 views Asked by At

I am using analogWrite() in my code. Everything works fine when I am not using the servo library. In other words, everything is fine when I disable this line //servo_9.attach(9); If I enable this line, the PWM will be wrong and weird. Any idea why the Tinkercad simulator is acting weird?

#include <Servo.h>
int led =9;
Servo servo_9;

void setup() {
servo_9.attach(9);

Serial.begin(9600);

}


 void loop() {
 for(int n=0;n<255;n++){
     analogWrite(led,n);
     delay(5);
   }
 }

This video shows what is happening: click here

1

There are 1 answers

9
Delta_G On

The documentation for the servo library clearly states.

On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.