Arduino Nano Servo and Motor Control
Checked Aliexpress today and the shipping status of all ordered parts for Remote Control project are shipped π, wait for approximately 2 weeks to arrive π. In the mean time, let learn Arduino Nano programming. First thing I have in mind is how to control servo using Arduino. This will help Remote Control project later on since it will need to control servo.
Objective:
- Sweep servo motor using potentiometer
- Control motor speed using potentiometer
- Arduino Nano
- Servo motor
- Brushless motor + ESC (Electronics Speed Controller)
- 10K potentiometer
By the way, I'm Arduino newbie, the code work for me but there may be other way to doing it better π
The circuit connection is shown on picture below, connect 10K (can be other value) potentiometer on analog input A0 and connect servo motor on digital output D9. 2x18650 Lithium Ion battery will be connected to ESC and ESC will provide 5V to the Arduino Nano so you do not need to provide separate power to Arduino Nano.
The analog input will return 0 to 1023 value, and using map function to convert it to 0 to 180 degree angle. Send converted result to servo using write() and done.
#include <Servo.h>Servo myservo;
int potpin = 0;
int val;
void setup() {
myservo.attach(9);
}
void loop() {
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 180);
myservo.write(val);
delay(15);
}
You can also use writeMicronseconds() instead. The value is 1000us for fully counter clockwise, 1500us in the middle and 2000us for fully clockwise. My understanding, the microseconds is referring to the duration of the pulse as shown on the picture below from Wikipedia.
Short video of servo control by potentiometer:
Short video of motor speed control by potentiometer, same connection and code, just replace servo with motor + ESC:
For reference, I found this picture from Arduino forum, it tell you which pin for PWM, ADC, etc. The official pinout document for Arduino Nano does not tell which pin for PWM. On picture below, those pins with sine wave are the one with PWM function (pin 3, 5, 6, 9, 10 and 11).
Comments
Post a Comment