Summary of How to control a Servo using Arduino
This project involves controlling a servo motor using two buttons: one to turn the servo left and another to turn it right. When the servo moves, an LED corresponding to the direction lights up, providing visual feedback. The servo position is managed within defined angle limits, leveraging Arduino's built-in servo library for easy control. The setup is designed to rotate an IR sensor continuously, with buttons influencing the servo's direction.
Parts used in the Servo Control with Directional LEDs:
- Arduino board
- Servo motor
- Two push buttons (for left and right control)
- Two LEDs (direction indicators)
- Resistors (for LED current limiting)
- Connecting wires
- Breadboard or PCB for circuit assembly
In this one, servo is programmed to be controlled by two buttons, one turns servo to the left and the other one turns it to the right. When the servo is turning, corresponding LED will be switched on to indicate the operation.
Result:
// Oscar’s Project // // There are 2 input buttons (turn left and right), when button is pressed, the servo turns and corresponding LED is lit up. |
#include
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 90; // variable to store the servo position
const int maxDeg = 160;
const int minDeg = 5;
const int leftPin = 3;
const int rightPin = 2;
const int led1Pin = 6; // indicator
const int led2Pin = 5; // indicator
const int outputPin = 9; // pwm function will be disabled on pin 9 and 10 if using servo
int leftPressed = 0;