测试代码
/* Controlling a servo position using a potentiometer (variable resistor) modified on 8 Nov 2013 by Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Knob */ #include 《Servo.h》 Servo myservo; // create servo object to control a servo int potpin = A0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it for use with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there } Arduino的ADC采集电位器的电压值(0-5V)转换成对应舵机旋转角度(0-180度),手动调节电位器时,舵机跟着旋转一定的角度,电位器停止调节时,舵机停留在固定的角度停止转动。
总结
大致了解下模拟与数字舵机的区别和原理,根据自己的应用情况选取合适的舵机,从模拟或数字,塑料或金属、转速、转矩、最大旋转角度等考虑。由于电机类产品驱动电流较大,舵机最好用舵机驱动板连接。重点专注于使舵机旋转的输入信号的控制。
测试代码
/* Controlling a servo position using a potentiometer (variable resistor) modified on 8 Nov 2013 by Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Knob */ #include 《Servo.h》 Servo myservo; // create servo object to control a servo int potpin = A0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it for use with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there } Arduino的ADC采集电位器的电压值(0-5V)转换成对应舵机旋转角度(0-180度),手动调节电位器时,舵机跟着旋转一定的角度,电位器停止调节时,舵机停留在固定的角度停止转动。
总结
大致了解下模拟与数字舵机的区别和原理,根据自己的应用情况选取合适的舵机,从模拟或数字,塑料或金属、转速、转矩、最大旋转角度等考虑。由于电机类产品驱动电流较大,舵机最好用舵机驱动板连接。重点专注于使舵机旋转的输入信号的控制。