Servo Motor(SG90)

아두위키 : Arduwiki

A product that can control the rotation angle according to the signal.

It is used in applications like robot joints due to its precise position control.

The rotation angle range is from 0 degrees to 180 degrees.

Specifications

  • Operating Voltage: 5V
  • Current Consumption: 0.2A ~ 0.7A
  • Angle Range: 0° ~ 180°

Required Hardware

  • SG90 Servo Motor
  • Arduino Board
  • Jumper Cables

Connection

Servor motor Arduino
S D7
+ 5V
- GND

Connection

  • Servo (Standard Library)

Example Code

// Using the servo library
#include <Servo.h>
// Using pin 7
#define pin 7

// Declare a servo object
Servo servo;

// Declare a variable for the angle
int pos = 60;

void setup() {
  Serial.begin(9600);
  // Set the pin number for the servo motor
  servo.attach(pin);
  // Set the initial angle for the servo motor
  servo.write(pos);
  Serial.println("Serial start");
  Serial.print("default degree : ");
  Serial.println(pos);
}

void loop() {
  if (Serial.available()) {
    char contrl = Serial.read();
    if (contrl == 'a') { // When 'a' is input in the serial monitor, increase by 60 degrees
      if (pos == 180)
        Serial.println("max angle");
      else {
        pos = pos + 60;
        Serial.print("degree : ");
        Serial.println(pos);
        servo.write(pos);
      }
    } else if (contrl == 'b') { // When 'b' is input in the serial monitor, decrease by 60 degrees
      if (pos == 0)
        Serial.println("min angle");
      else {
        pos = pos - 60;
        Serial.print("degree : ");
        Serial.println(pos);
        servo.write(pos);
      }
    }
  }
}

Execution Result