กลับไปหน้ารวมไฟล์
servo-control-a4b297-en.md

Precision Motion: Mastering Servo Control via PWM

Servo motors are the standard for precise angular positioning in robotics. This project strips away the complexity of expensive motor shields, demonstrating how to control two independent SG90 Micro-servos using an Arduino Uno and standard 10k Potentiometers, providing a tactile introduction to the world of mechatronics.

Pulse Width Modulation (PWM): The Control Signal

Servos do not operate on raw DC voltage; they require a specific type of digital signal called PWM (Pulse Width Modulation):

  • The Signal Loop: A servo expects a pulse every 20 milliseconds. The width of that pulse (varying from ~1ms to ~2ms) tells the internal servo controller where to position the output shaft (from 0 to 180 degrees).
  • Firmware Abstraction: By using the #include <Servo.h> library, the Arduino handles the complex timing of these pulses behind the scenes, allowing the developer to simply write servo.write(angle).
  • Hardware Pins: On the Arduino Uno, pins 9 and 6 are utilized as they have dedicated hardware-level PWM timers, ensuring that the motor's position remains steady even while the main CPU is busy calculation other logic.

Tactical Mapping: Analog-to-Digital Interfacing

The project uses a simple but powerful "Map" logic to transform human input into robotic motion:

  1. Analog Sensing: The potentiometers are connected to analog pins A0 and A1. These return a value from 0 to 1023 based on the knob's position.
  2. Data Transformation: The map(val, 0, 1023, 0, 180) function is used to scale this 10-bit analog reading down to the 180-degree physical limit of the servo motor.
  3. Real-Time Responsiveness: The inclusion of a small delay(15) gives the servo enough time to physically reach its new target position before the next command is sent, preventing mechanical jitter.

Foundation for Complexity

While this project is a "Basics" tutorial, it forms the bedrock for advanced robotics. This same principle—potentiometer-to-servo mapping—is the foundation for building robotic prosthetic hands, remote-controlled gimbal systems, and "Mimic" robots that copy human arm movements in real-time.

I attended a program called YTS(Young Technology Scholars) from 1 June to 15 June (2019). It was a terrific program. They taught me many things about Arduino and Raspberry Pi. It was pretty easy for me for a few days as I already knew coding and a little hardware and software debugging. Later on I realized that many people did not know the basics so I started doing this little project.

Actually this project is a little cut out of a very large project I am doing. This explains the basic functions of servo motor which we control with our pot. This is the pin configuration of this project:

  • Middle pin of pot 1 - A0
  • Middle pin of pot 2 - A1
  • Red pin of servo 1 and 2 - 5v
  • Brown pin of servo 1 and 2 - GND
  • Yellow pin of servo 1 - 9
  • Yellow pin of servo 2 - 6

We connect the yellow pin (pulse pin) to 9 and 6 as they are PWM pins. Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED. The same principle which we use with our servo.

Explanation of Code:

#include - Include the servo library. As I used Arduino web editor, I did not install the servo library.

Servo servo1 and Servo servo2 - defines the servos

int Pot1 and int Pot2 defines the potentiometers.

int valPot1 and int valPot2 - defines the variables to take input from pot.

servo1.attach(9); - this attaches the servo to the arduino pin 9 (PWM)

[ valPot1 = analogRead(pot1);

valPot1 = map(valPot1, 0, 1023, 0, 180);

servo1.write(valPot1);

delay(15); ]

This reads the analog signal from the pot and gives it to the servo.

ข้อมูล Frontmatter ดั้งเดิม

title: "Servo Control"
description: "Easily controlling servos without any requirement of shields."
author: "Bravesoldiers"
category: ""
tags:
  - "basics"
  - "servo"
views: 1796
likes: 1
price: 299
difficulty: "Easy"
components:
  - "1x Arduino UNO"
  - "2x SG90 Micro-servo motor"
  - "2x Rotary potentiometer (generic)"
  - "1x Jumper wires (generic)"
  - "1x 9V battery (generic)"
tools: []
apps:
  - "1x Arduino IDE"
downloadableFiles: []
documentationLinks: []
passwordHash: "6e0fbcca9e85c280878e755fdf16b7f1f56831fd0b8cbb130cb64f437998da44"
encryptedPayload: "U2FsdGVkX1/z56PsjAKhmu4FWeN31cZY/jvTM6QC+S4T7RDrB/OIbZ1Vj4D2KpZxcBKcV06mZcGqfJ6VMJyyRAbIpkWQtoKiDObh6Kig0L8="
seoDescription: "Learn how to control Servo motors easily without any shields. Simple and cost-effective Arduino project guide."
videoLinks: []
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/servo-control-a4b297_cover.jpg"
lang: "en"