Code

Both the Arduino IDE and Web Editor come with a servo library that we can use simply by including the header file. The following code will do this:

#include <Servo.h>

Next, we need to define the pin that the servo motor and the potentiometer are connected to. The following code will connect the signal wire to the digital 3 pin and the potentiometer to the analog 0 pin on the Arduino:

#define SERVO0_POT 0
#define SERVO0_OUT 3 

Now we need to define an instance of the Servo type as shown in the following line:

Servo servo0;

Within the setup function, we need to call the attach() method from the servo instance to initialize the instance and to tell it what pin the servo is attached to. The following code shows this:

void setup() { servo0.attach(SERVO0_OUT); ...

Get Mastering Arduino now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.