Introduction
In this tutorial we will sweeps the shaft of a RC servo motor back and forth across 180 degrees.
Servo Sweep using evive and Arduino IDE
Circuit Diagram
In the following example, I will be showing you how to control servo through channel 1. Given below is the circuit diagram:
A Servo pin has three wires (Order to be connected in evive, left to right)
- Brown wire: GND
- Red wire: VCC
- Orange wire: Signal
Logic Diagram
Angle at the start of the program should be 0 degrees. Then it should increase until it reasches 180 degree angle, then it should decrease until it reaches 0 degree angle.
Code
[pastacode lang=”c” manual=”%23include%20%3Cservo.h%3E%0A%0Adouble%20Angle%3B%0AServo%20servo_44%3B%0A%0Avoid%20setup()%7B%0A%20Angle%20%3D%200%3B%0A%20servo_44.attach(44)%3B%20%2F%2F%20init%20pin%0A%7D%0A%0Avoid%20loop()%7B%0A%20while(!(((Angle)%3D%3D(180))))%0A%20%7B%0A%20Angle%20%2B%3D%205%3B%0A%20servo_44.write(Angle)%3B%20%2F%2F%20write%20to%20servo%0A%20%7D%0A%20while(!(((Angle)%3D%3D(0))))%0A%20%7B%0A%20Angle%20%2B%3D%20-5%3B%0A%20servo_44.write(Angle)%3B%20%2F%2F%20write%20to%20servo%0A%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]
Servo Sweep using Arduino Uno
Arduino uno has 6 PWM pins: 3, 5, 6, 9, 10, and 11 which provide 8-bit PWM output with the analogWrite() function.
We will controlling serov using Pin 3.
Circuit Diagram
A Servo pin has three wires (Order to be connected in evive, left to right)
- Brown wire: GND
- Red wire: VCC
- Orange wire: Signal
Code
[pastacode lang=”c” manual=”%23include%20%3Cservo.h%3E%0A%0Adouble%20Angle%3B%0AServo%20servo_3%3B%0A%0Avoid%20setup()%7B%0A%20Angle%20%3D%200%3B%0A%20servo_3.attach(3)%3B%20%2F%2F%20init%20pin%0A%20servo_3.write(Angle)%3B%20%2F%2F%20write%20to%20servo%0A%7D%0A%0Avoid%20loop()%7B%0A%20while(!(((Angle)%3D%3D(180))))%0A%20%7B%0A%20Angle%20%2B%3D%205%3B%0A%20servo_3.write(Angle)%3B%20%2F%2F%20write%20to%20servo%0A%20delay(20)%3B%0A%20%7D%0A%20while(!(((Angle)%3D%3D(0))))%0A%20%7B%0A%20Angle%20%2B%3D%20-5%3B%0A%20servo_3.write(Angle)%3B%20%2F%2F%20write%20to%20servo%0A%20delay(20)%3B%0A%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]