Home Industry InsightBLDC
Looking for a suitable motor? Looking for a suitable motor?
Looking for a suitable motor?

basic arduino program for sg90 servo

小编

Published2025-10-17

When it comes to controlling a small servo motor like the SG90, a basic Arduino program can be a game-changer for many DIY projects. Whether you're building a robotic arm, a model airplane, or even just experimenting with automation, knowing how to program the SG90 servo will take your projects to the next level. It’s easier than it seems, and with just a few lines of code, you can have your servo turning, tilting, and moving in no time.

Let’s start with the essentials. The SG90 servo is a small, lightweight motor that’s perfect for tasks requiring precise movements. It’s widely used in hobby electronics, and because of its affordability and versatility, it’s the go-to choice for many Arduino enthusiasts. Now, let’s break down how you can control it with a simple Arduino program.

Setting Up Your Arduino and SG90 Servo

Before you dive into the code, let's take a quick look at the wiring. The SG90 has three wires: red for power (connect it to 5V on the Arduino), brown for ground (connect it to GND), and yellow for the signal (connect it to a PWM-capable pin like pin 9 on the Arduino). Simple enough, right?

The Code

Here’s where things get interesting. You don’t need to be a programming expert to get this working. The Arduino IDE (Integrated Development Environment) comes with a Servo library that makes controlling the SG90 straightforward. With just a few lines of code, you’ll be able to control the angle of the servo.

#include <Servo.h>

Servo myservo;  // Create a Servo object to control the SG90

void setup() {
  myservo.attach(9);  // Pin 9 is the signal pin for the servo
}

void loop() {
  myservo.write(0);   // Set servo to 0 degrees
  delay(1000);        // Wait for 1 second
  myservo.write(90);  // Set servo to 90 degrees
  delay(1000);        // Wait for 1 second
  myservo.write(180); // Set servo to 180 degrees
  delay(1000);        // Wait for 1 second
}

This code is as simple as it gets! The Servo.h library allows you to control the servo by attaching it to a specific pin (in this case, pin 9). Inside the loop(), the servo is moved to three different positions: 0°, 90°, and 180°. The delay(1000) function ensures the servo has enough time to reach each position before moving on.

Why Choose the SG90 for Your Projects?

The SG90 is not just a small servo, it’s reliable and easy to use, making it perfect for beginners. Plus, it has a decent range of movement (about 180°), which is enough for most small-scale applications. If you need more precise control or higher torque, you can always look into bigger servos, but for simple tasks, the SG90 is hard to beat.

The servo works well for a wide range of projects, including automation systems, basic robotics, and even in models or mechanical simulations. Its small size means you won’t have to worry about fitting it into tight spaces, and the low price tag makes it an attractive choice for hobbyists and students alike.

What If I Want More Control?

If you want to make things more exciting, you can add some additional code to fine-tune the servo's movements. For example, you could add a potentiometer to control the angle of the servo in real-time. With just a few more lines of code, you can turn your simple project into a dynamic, interactive experience.

int potPin = A0;  // Pin connected to the potentiometer
int val;           // Variable to store the potentiometer value

void loop() {
  val = analogRead(potPin);      // Read the potentiometer
  val = map(val, 0, 1023, 0, 180); // Map it to a range from 0 to 180
  myservo.write(val);            // Set the servo to the new position
  delay(15);                     // Small delay to allow the servo to reach the position
}

In this updated code, you use a potentiometer to control the servo. By turning the knob, you can smoothly move the servo to any position between 0° and 180°—a great way to add more interactivity to your project.

Wrapping Up

Mastering the SG90 servo with an Arduino doesn’t take long, and once you’ve got the basics down, you can start experimenting and expanding your projects. Whether it’s for fun or as part of a larger project, this small servo motor can do a lot with just a little code.

So, what are you waiting for? Grab your Arduino, hook up that SG90, and start creating. You’ll be amazed at how quickly you can bring your ideas to life with just a few simple steps!

Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.

Update:2025-10-17

Contact a motor expert for product recommendation.
Contact a motor expert for product recommendation.

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.