Introduction :
The SG90 is a popular micro servo motor commonly used in robotics and DIY electronics projects due to its compact size, lightweight, and affordability.
Key Features
- Operating Voltage: 4.8V to 6V
- Stall Torque: 1.8 kg·cm at 4.8V
- Operating Speed: 0.1 sec/60° at 4.8V
- Rotation Range: 0 to 180 degrees
- Control Signal: PWM (Pulse Width Modulation)
- Dimensions: 22.2 x 11.8 x 31 mm
- Weight: 9 grams
Pin Configuration
- Orange/Yellow Wire: Signal (PWM input)
- Red Wire: VCC (Power supply, 4.8V to 6V)
- Brown/Black Wire: GND (Ground)
CODE :
#include <Servo.h>
Servo myServo; // Create a servo object to control the servo
const int servoPin = 9; // Pin connected to the signal pin of the servo
void setup() {
myServo.attach(servoPin); // Attach the servo to the specified pin
}
void loop() {
// Move the servo to 0 degrees
myServo.write(0);
delay(1000); // Wait for 1 second
// Move the servo to 90 degrees
myServo.write(90);
delay(1000); // Wait for 1 second
// Move the servo to 180 degrees
myServo.write(180);
delay(1000); // Wait for 1 second
// Move back to 90 degrees
myServo.write(90);
delay(1000); // Wait for 1 second
}
https://github.com/makertribe/IoT-Codes/tree/69696eb1ee7cc3caa9cfc90ed1d9622c10fd10a1
Library install :
Install the Servo Library (if not already installed):
- Open the Arduino IDE.
- Go to Sketch > Include Library > Servo.
Output: