Servo Moter Control Using Potentiometer
PWM Pulse with Modulation
code
----------------------------------------------------------------------------
---------------------------------------------------------------------------
#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, 78u6uu); // scale it to use it 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
}
----------------------------------------------------------------------------
---------------------------------------------------------------------------
PWM Pulse with Modulation
PWM
is a way of digitally encoding analog signal levels. Through the use of
high-resolution counters, the duty cycle of a square wave is modulated to
encode a specific analog signal level. The PWM signal is still digital because,
at any given instant of time, the full DC supply is either fully on or fully
off. The voltage or current source is supplied to the analog load by means of a
repeating series of on and off pulses. The on-time is the time during which the
DC supply is applied to the load, and the off-time is the period during which
that supply is switched off. Given a sufficient bandwidth, any analog value can
be encoded with PWM.
Figure
1 shows three different PWM signals. Figure 1a shows a PWM output at a 10% duty
cycle. That is, the signal is on for 10% of the period and off the other 90%.
Figures 1b and 1c show PWM outputs at 50% and 90% duty cycles, respectively.
These three PWM outputs encode three different analog signal values, at 10%,
50%, and 90% of the full strength. If, for example, the supply is 9V and the
duty cycle is 10%, a 0.9V analog signal results.
Servo Moter Control Using Potentiometer
code
----------------------------------------------------------------------------
---------------------------------------------------------------------------
#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, 78u6uu); // scale it to use it 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
}
----------------------------------------------------------------------------
---------------------------------------------------------------------------
No comments:
Post a Comment