๐ง Components Needed
- Arduino (e.g., Uno)
- SG90 Servo Motor
- Potentiometer (e.g., 10kฮฉ)
- Two Push Buttons
- Jumper Wires & Breadboard
๐ Wiring Diagram
1. Servo Connection (SG90)
- VCC (Red): โ Arduino 5V
- GND (Brown/Black): โ Arduino GND
- Signal (Orange): โ Digital Pin 9
2. Potentiometer Connection
- One Outer Pin: โ Arduino 5V
- Other Outer Pin: โ Arduino GND
- Middle Pin (Wiper): โ Analog Pin A0
3. Button Connections
- Button 1 (Increase Angle):
- One terminal โ Digital Pin 2
- Other terminal โ GND
- Button 2 (Decrease Angle):
- One terminal โ Digital Pin 3
- Other terminal โ GND
Tip: Use the Arduinoโs internal pull-up resistors for the buttons by setting their mode to INPUT_PULLUP.
๐ป Example Code
Below is an example sketch that uses the potentiometer as a base angle and allows adjustment with two buttons:
#include <Servo.h>
Servo myServo; // Create a servo object
// Pin Definitions
const int servoPin = 9; // Servo signal pin
const int potPin = A0; // Potentiometer analog input
const int buttonUpPin = 2; // Button to increase angle
const int buttonDownPin = 3; // Button to decrease angle
int servoAngle = 90; // Starting at the middle position (90ยฐ)
void setup() {
Serial.begin(9600);
myServo.attach(servoPin); // Attach the servo to the defined pin
// Set pin modes for the potentiometer (default INPUT) and buttons
pinMode(potPin, INPUT);
pinMode(buttonUpPin, INPUT_PULLUP); // Enable internal pull-up resistor
pinMode(buttonDownPin, INPUT_PULLUP);
myServo.write(servoAngle); // Set initial servo position
}
void loop() {
// 1๏ธโฃ Read the potentiometer value and map it to a 0-180ยฐ range
int potValue = analogRead(potPin);
int mappedAngle = map(potValue, 0, 1023, 0, 180);
// Use the potentiometer value as the base servo angle
servoAngle = mappedAngle;
// 2๏ธโฃ If the "increase" button is pressed, add 90ยฐ to the angle
if (digitalRead(buttonUpPin) == LOW) {
servoAngle += 90;
if (servoAngle > 180) servoAngle = 180;
delay(200); // Debounce delay
}
// 3๏ธโฃ If the "decrease" button is pressed, subtract 90ยฐ from the angle
if (digitalRead(buttonDownPin) == LOW) {
servoAngle -= 90;
if (servoAngle < 0) servoAngle = 0;
delay(200); // Debounce delay
}
// 4๏ธโฃ Update the servo with the new angle
myServo.write(servoAngle);
// Debug: Print the current angle to the Serial Monitor
Serial.print("Angle: ");
Serial.println(servoAngle);
delay(20); // Small delay for smooth operation
}
๐ How It Works
- Potentiometer:
Reads an analog value (0โ1023) and maps it to a servo angle (0ยฐโ180ยฐ). - Buttons:
- When the increase button is pressed, the angle increases by 90ยฐ.
- When the decrease button is pressed, the angle decreases by 90ยฐ.
(Internal pull-up resistors and a debounce delay help ensure reliable button presses.)
- Servo Control:
The servoโs position is continuously updated based on the potentiometer reading and button adjustments. - Serial Monitor:
The current servo angle is printed for debugging purposes.
๐ฏ Conclusion
This setup lets you control an SG90 servo using both a potentiometer (for smooth, continuous adjustment) and two buttons (for quick, incremental changes). Itโs a great project for learning how to combine analog and digital inputs on the Arduino!
Enjoy your project! ๐๐