Control a servo - mako

Mako
Mako
nome
Go to content
Arduino > Tutorials
Servomotor
The servomotor is a particular type of motor widely used both in industrial contexts and in the field of model making. It is used in all applications that provide for the control of the position of a direct current motor and the reaching of a certain angle in a precise manner regardless of the initial position.
Usually the shaft can rotate from 0 to 180 degrees and using Arduino, we can tell a servo to go to a specified position.

Here's the sketch:
Sketch
#include <Servo.h>
Servo myServo;
int Position = 0;
int PotentiometerPin = A0;
int OutputValue = 0;
int PotentiometerValue = 0;

void setup(){
 myServo.attach(9);
 myServo.write(Position);
delay(100);
}

void loop() {
PotentiometerValue = analogRead(PotentiometerPin);
Postion = map(PotentiometerValue, 0, 1023, 0, 180);
 myServo.write(Position);
delay(100);
}
Sketch analysis
Servo myServo: servo object
int Position: servo initial position
int PotentiometerValue: vlue read by potentiometer
int OutputValue: value mapped by potentiometer
myServo.attach(9): servo pin
myServo.write(Position): servo initial position at 0°
PotentiometerValue = analogRead(PotentiometerPin), Position = map(PotentiometerValue, 0, 1023, 0, 180): flow mapping, potentiometer reading between 0 and 1023 and converted via the map instruction to a value between 0° and 180 °.
There are no reviews yet.
0
0
0
0
0
Back to content