In: Electrical Engineering
Write a program in Arduino that, when it takes in a voltage range (0 to 5 volts), gives a corresponding servo motor angle with a range from 0 to 180 degrees.
To give voltage input we will use a potentiometer that has a range (0-5v), by turning the knob the input voltage will vary. We will display the corresponding servo motor angle for every position change in potentiometer
The Arduino Uno has a 10-bit analog to digital converter. So the analog input values for the range 0 to 5 volt is converted into corresponding decimal values from 0 to 1023 ( 10-bit means 210 values). In the program, we map the values between 0 – 1023 to 0° – 180°. Thus the angle of servo proportionally increments and decrements with the increase and decrease in input value. That is when the knob is at the center position the servo arm will be at 90o.
Arduino Code
#include <Servo.h> //including servo motor libraries
Servo servo1; //creating a servo object to control servo
int potin
void setup()
{
servo1.attach(9); //attaching the signal pin of servo to pin9 of
arduino
Serial.begin(9600); //send and receive at 9600baud
}
void loop()
{
potin = analogRead(A0); //reading potentiometer values
at pin A0
potin = map(potin, 0, 1023, 0, 180); //mapping
voltage values to angles
servo1.write(potin); //command to
servo motor
Serial.print("Angle");
Serial.println(potin); //printing the angle
}
I am attaching image also.