Skip to content
Home » Arduino Uno R3 Servo Control | Step 4: Procedures

Arduino Uno R3 Servo Control | Step 4: Procedures

How to Control a Servo With an Arduino

Step 2: Principle

Servo consists of shell, circuit board, non-core motor, gear and location detection. Its working principle is as follows: Arduino Uno board sends PWM signal to servo motor, and then this signal is processed by IC on circuit board to calculate rotation direction to drive the motor, and then this driving power is transferred to swing arm by reduction gear. At the same time, position detector returns location signal to judge whether set location is reached or not.

Sơ đồ đấu nối

Arduino Uno Động cơ Servo SG90
5V Dây màu đỏ
GND Dây màu đen
D9 Dây màu vàng

Các linh kiện cần thiết cho dự án

TÊN LINH KIỆN SỐ LƯỢNG NƠI BÁN
Arduino Uno R3 Shopee | Cytron
Động cơ Servo SG90 Shopee | Cytron
Dây cắm 10-20 Shopee | Cytron
Breadboard Shopee | Cytron
How to Control a Servo With an Arduino
How to Control a Servo With an Arduino

Step 4: Procedures

Step1:

Build the circuit.

Step 2:

Download the code from https://github.com/primerobotics/Arduino

Step 3:

Upload the sketch to the Arduino Uno board

Click the Upload icon to upload the code to the control board.

If “Done uploading” appears atthe bottom of the window, it means the sketch has been successfully uploaded.

Now, you can see the servo motor rotate 90 degrees (rotate once every 15 degrees). And then rotate in the opposite direction.

Step 5: Code

/***********************************************

* name:Servo

* function:you can see the servo motor rotate 90 degrees (rotate once every 15 degrees).

* And then rotate in the opposite direction.

************************************************/

//Email: [email protected]

//Website: www.primerobotics.in

#include

/************************************************/

Servo myservo;//create servo object to control a servo

/************************************************/

void setup()

myservo.attach(9);//attachs the servo on pin 9 to servo object

myservo.write(0);//back to 0 degrees

delay(1000);//wait for a second

/*************************************************/

void loop()

myservo.write(15);//goes to 15 degrees

delay(1000);//wait for a second

myservo.write(30);//goes to 30 degrees

delay(1000);//wait for a second.33

myservo.write(45);//goes to 45 degrees

delay(1000);//wait for a second.33

myservo.write(60);//goes to 60 degrees

delay(1000);//wait for a second.33

myservo.write(75);//goes to 75 degrees

delay(1000);//wait for a second.33

myservo.write(90);//goes to 90 degrees

delay(1000);//wait for a second

myservo.write(75);//back to 75 degrees

delay(1000);//wait for a second.33

myservo.write(60);//back to 60 degrees

delay(1000);//wait for a second.33

myservo.write(45);//back to 45 degrees

delay(1000);//wait for a second.33

myservo.write(30);//back to 30 degrees

delay(1000);//wait for a second.33

myservo.write(15);//back to 15 degrees

delay(1000);//wait for a second

myservo.write(0);//back to 0 degrees

delay(1000);//wait for a second

/**************************************************/

Servo Motor Basics with Arduino

Learn how to connect and control servo motors with your Arduino board.

The Servo Library is a great library for controlling servo motors. In this article, you will find two easy examples that can be used by any Arduino board.

The first example controls the position of an RC (hobby) servo motor with your Arduino and a potentiometer. The second example sweeps the shaft of an RC servo motor back and forth across 180 degrees.

You can also visit the Servo GitHub repository to learn more about this library.

Multiple Servo Control with Arduino Uno R3
Multiple Servo Control with Arduino Uno R3

Testing it out

After we have successfully uploaded the code to the board, the standard servo should now start moving from 0 – 180, and then start moving from 180 – 0. This is due to the two for loops in the program, which gradually increases the

variable, which is written to the servo.

pos

Congratulations! That was just a few easy steps to get started with standard servo motors. Now that you have this working, you can start exploring a lot of different cool projects that uses servo motors, and perhaps start making your very own robot!

Powering Servo Motors

Servo motors have different power requirements depending on their size and the workload they are experiencing. A common servo motor such as the Feetech Mini Servo Motor requires between 4.8 – 6 V at 5 – 6 mA when idle. It doesn’t take very much energy to stand still.

But as soon as the motor starts moving, it starts using more energy, and it gets that energy by pulling more current from the power source.

If it experiences heavier loads such as added weight or an object blocking its movement , it naturally needs to use even more energy to move the obstacle, and as a result the current consumption increases. The current consumption of the motor linked above can reach up to 800 mA.

This high current-draw is generally not safe to draw from an Arduino board. To avoid damaging our board we need to power the servo motor through an external power supply. Choosing the correct power supply depends on the servo motor you are using, so always check the specifications. Pay especially close attention to the:

  • operating voltage range
  • idle current – consumption when not moving
  • running current – consumption when moving freely
  • stall current – consumption under max load or when blocked

To power a 4.8 – 6 V servo you could use a 5 V 1 A AC Adapter, cut the cable, and connect the wires to the servo using e.g. a breadboard.

Note that USB wall chargers are limited to 500 mA (USB 2.0) or 900 mA (USB 3.0).

If your project needs to move around freely without being attached to a power outlet you can also choose batteries to power the servo. If you need 5 V exactly you can use two 18650 Li-Ion batteries together with a step-down converter.

A step-down converter is needed because 18650 Li-Ion batteries will give you around 7.4 V. The max current depends on the specific battery but most of them are designed to output above 1A which is enough to power our small servo.

If you are using bigger or more servos make sure to check your power requirements accordingly.

Capacitors are recommended for powering servo motors. They help stabilize the power supply, minimize voltage drops, and reduce electrical noise. The specific capacitor values may vary based on the servo motor’s requirements, but including them is good practice for better performance and reliability.

When using a Feetech Mini Servo Motor we recommend using a 100 µF capacitor.

Because some capacitors are polarised (meaning that they have a direction), you may need to be careful with how you connect them to your circuit. Make sure to connect them correctly by checking for markings such as a white stripe, a ‘+’ symbol, or a longer lead. If your capacitor has these, match the indicators of the capacitor with your circuit (pay attention to the + and – signs), and be careful not to exceed the voltage limits. This precaution helps prevent issues like leaks or damage that could harm your circuit.

You can read more about capacitors here.

Arduino UNO Tutorial #3 - Servo Motor Project (Set Up)
Arduino UNO Tutorial #3 – Servo Motor Project (Set Up)

Bài viết liên quan

  • Bài 9: Cảm biến ánh sáng (Quang trở) cách chia điện áp trong môi trường Arduino
  • Bài 8: Cảm biến góc nghiêng sử dụng ngắt (INTERRUPT) trong môi trường Arduino
  • Bài 7: Cảnh báo nhiệt độ (LM35) bằng còi báo sử dụng Arduino Uno
  • Bài 6: Tạo âm thanh (Còi) bằng Arduino
  • Bài 5: Thay đổi màu sắc Led RGB sử dụng Arduino
  • Bài 4: PWM | Thay đổi ánh sáng của LED trên Arduino
  • Bài 3: Sử dụng Arduino làm hệ thống đèn giao thông
  • Bài 2: Chớp tắt LED trên Arduino Uno (Phần 2)
  • Bài 1: Chớp tắt LED trên Arduino Uno

Basic servo control

In this tutorial, we will learn how to control a standard servo motor, to go back and forth across 180 degrees, using a `for loop()`. This is done with the help of the Servo library, which is pre-installed library in the Arduino IDE (both offline and online versions).

In this tutorial, we will learn how to control a standard servo motor, to go back and forth across 180 degrees, using a

. This is done with the help of the Servo library, which is pre-installed library in the Arduino IDE (both offline and online versions).

for loop()

Note: This tutorial uses an Arduino UNO, but you can use any official Arduino board.

Circuit

Servo motors have three wires: power, ground, and signal. The power wire is typically red, and should be connected to positive pole (+) of your power source. The ground wire is typically black or brown and should be connected to the negative pole (-) of your power source.

The signal pin is typically yellow or orange and should be connected to PWM pin on the board. In these examples, it is pin number 9.

Knob Circuit

For the Knob example, wire the potentiometer so that its two outer pins are connected to power (+5V) and ground, and its middle pin is connected to

on the board. Then, connect the servo motor as shown in the circuit below.

A0

Sweep Circuit

For the Sweep example, connect the servo motor as shown in the circuit below.

Arduino tutorial 7- How to control Servo motor with Arduino (code explained) | using servo library
Arduino tutorial 7- How to control Servo motor with Arduino (code explained) | using servo library

Examples

Knob

Controlling a servo position using a potentiometer (variable resistor).

1#include

23Servo myservo; // create servo object to control a servo45int potpin = 0; // analog pin used to connect the potentiometer6int val; // variable to read the value from the analog pin78void setup() {9 myservo.attach(9); // attaches the servo on pin 9 to the servo object10}1112void loop() {13 val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)14 val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)15 myservo.write(val); // sets the servo position according to the scaled value16 delay(15); // waits for the servo to get there17}

Sweep

Sweeps the shaft of a RC servo motor back and forth across 180 degrees.

1#include

23Servo myservo; // create servo object to control a servo4// twelve servo objects can be created on most boards56int pos = 0; // variable to store the servo position78void setup() {9 myservo.attach(9); // attaches the servo on pin 9 to the servo object10}1112void loop() {13 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees14 // in steps of 1 degree15 myservo.write(pos); // tell servo to go to position in variable ‘pos’16 delay(15); // waits 15ms for the servo to reach the position17 }18 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees19 myservo.write(pos); // tell servo to go to position in variable ‘pos’20 delay(15); // waits 15ms for the servo to reach the position21 }22}

Suggested changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.

Bài 10: Điều khiển động cơ RC Servo sử dụng Arduino

Tiếp tục trong chuỗi bài viết Khóa học lập trình Arduino Miễn Phí dành cho người nhập môn.

Trong bài viết hôm nay mình sẽ hướng dẫn các bạn làm thế nào để điều khiển góc của một động cơ Servo.

Để có thể hiểu một cách tốt nhất về động cơ RC Servo là gì? Cấu tạo và nguyên lý hoạt động ra làm sao. Các bạn xem bài viết bên dưới nhé.

Xem thêm: Động cơ RC Servo là gì?

Standard servo motors

Standard servo motors are actuators that allow for precise control of position (angle). A typical characteristic is that the angle of the motor is 0 – 180 degrees. With other words, it can make one half of a rotation.

A standard servo motor, just as other motors, are essentially just a DC motor, but with some extra features:

  • Control circuit for controlling the motor, e.g. setting the angle.
  • Gears that transform speed into torque, which makes it capable of doing “heavy lifting” at a slower speed, as opposed to a regular DC motor that just spins very fast!
  • Potentiometer that keeps track of its angle. This makes it possible for the servo to “know where it is”.

Take a look at the image below to see how a Servo looks like inside:

The different wires

Almost all servos come with a set of 3 wires. These are PWR, GND and Signal. For a very simple circuit, all that is needed is to connect each of these two pins on the Arduino:

  • PWR (RED) – connects to 5V on the Arduino.
  • GND (BLACK) – connects to GND on the Arduino.
  • Signal (WHITE) – connects to a digital pin on the Arduino (typically 9).

Note: The color combination varies from servo to servo, but typically the red and black remains.

Note: Depending on what Arduino you are using, the signal pin may vary.

How to Control Servo Motor Up To 16 with Arduino Uno R3
How to Control Servo Motor Up To 16 with Arduino Uno R3

Conclusion

Congratulations! That was just a few easy steps to get started with standard servo motors. Now that you have this working, you can start exploring a lot of different cool projects that uses servo motors, and perhaps start making your very own robot!

Feel free to explore the Servo further.

Suggested changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.

Usage

This library allows an Arduino board to control RC (hobby) servo motors. Servos have integrated gears and a shaft that can be precisely controlled. Standard servos allow the shaft to be positioned at various angles, usually between 0 and 180 degrees. Continuous rotation servos allow the rotation of the shaft to be set to various speeds.

The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables

analogWrite()

(PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.

To use this library:


#include

Circuit

Servo motors have three wires: power, ground, and signal. The power wire is typically red, and should be connected to the 5V pin on the Arduino board. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board. The signal pin is typically yellow, orange or white and should be connected to a digital pin on the Arduino board. Note that servos draw considerable power, so if you need to drive more than one or two, you’ll probably need to power them from a separate supply (i.e. not the 5V pin on your Arduino). Be sure to connect the grounds of the Arduino and external power supply together.

Uno R3 based Bluetooth & USB 18 Servo Controller compatible with Arduino is variant of our USB servo controller for wireless applications. This device can be operated wirelessly on Bluetooth or through USB connection. This also means that it can receive signals from any devices like PCs, Laptops, Mobile phones with Bluetooth and Java, Android and windows smartphones etc. Uno R3 based Bluetooth + USB 18 Servo Controller is ideal for making Autonomous and PC based systems which run over Hobby Servo motors. The software helps to develop the complex sequences in real time on the hardware like robotic arms, walkers, bipeds and any other servo controlled system. It also generates Arduino based code for the developed sequence which can be deployed on the controller on board thereby making the robot autonomous.

The compact module measures just 80 mm X 47 mm, and it offers both USB and asynchronous serial (UART) connectivity. No features are compromised for the small size, as our USB controller supports independent speed and range settings for each servo while delivering 0.5-microsecond resolution for smooth output across its broad output pulse range of 500 through 2500 microseconds.

Servo motor configurations like Center, Offsets, Maximum, Minimum, servo directions and speed can be individually set. The servo configurations set can be saved to the on board controller. Additionally servo sequence functions can be created through software itself. Moreover Groups of servos can be rotated simultaneously for creating accurate and easy angular movements.

The board holds two chips configured as Master and Slave. Major servo motor positioning and servo related calculations are done by the salve chip. The Master chip i.e. Atmega 328 which is preloaded with Arduino Uno bootloader communicates with the software serially and send commands to the slave chip through I2C making the Master cpu available for other tasks. Various sensor and wireless devices like TSOP-1738 etc can be interfaced with the master making the robot wirelessly controlled.

When installed, the USB servo controller appears as a serial port to the host computer. Programming is therefore as easy as sending commands to a serial port, and as an added benefit, the servo controller is compatible with many existing programs. The servo controller is compatible with USB 1.1 and USB 2.0 standards, and driver support will initially be available for Windows 98 through Windows 8.

With its Bluetooth, USB and UART interface, the servo controller can at first be used with a PC to quickly develop motion sequences with the advantage of graphical interfaces and quick program changes.

This Robokits Uno R3 based USB 18 Servo Controller provides one of the most simple, small, and economical alternatives to standard serial servo controllers.

Features

  • Bluetooth interface for wireless controll of robots
  • Voltage Rating : 5 to 7.5V DC
  • Controls 18 hobby servos from PC and Microcontroller
  • USB interface
  • Comes Pre-loaded Arduino Uno bootloader
  • Software exports servo sequences to Arduino Uno for running servo sequences
  • Independent range setting for each servo
  • Independent offset, Maximum, Minimum and Direction setting for each servo
  • 0.5-microsecond resolution
  • 50 Hz update rate
  • Small size of 80 X 47 mm
  • Plug and Play, Auto detection of hardware
  • Easy to use software
  • Servo sequencer with speed, delay, goto and many other features
  • Home and neutral position setting
  • Easy to install USB driver and Application software

Package Includes:

  • 1 x UNO R3 BASED BLUETOOTH & USB 18 SERVO CONTROLLER COMPATIBLE WITH ARDUINO

Downloads

USB 18 Servo Controller Software

USB Servo Controller Manual

Code:

#include

Servo myservo; int pos = 0; void setup() { myservo.attach(9); } void loop() { for(pos = 0; pos < 180; pos += 1){ myservo.write(pos); delay(15); } for(pos = 180; pos>=1; pos-=1) { myservo.write(pos); delay(15); } }

Gải thích code

#include

#include

Cho phép chương trình của bạn tải một thư viện đã được viết sẵn. Tức là bạn có thể truy xuất được những tài nguyên trong thư viện này từ chương trình của mình.

Servo myservo;

Khởi tạo đối tượng Servo và đặt tên là myservo.

Hàm attach()

Nếu các bạn theo dõi từ những bài trước và đã quen thuộc với hàm

pinMode()

thì hàm

attach()

ở đây cũng tương tự, dùng để khai báo chân kết nối.

Cú pháp

myservo.attach(pin);

Trong bài viết này mình dùng Pin D9 để điều khiển động cơ Servo. Các bạn có thể thay thế các chân Digital/Analog khác có trên Board mạch.

myservo.write(pos);

Dùng để ghi các dữ liệu ra và ở đây là xuất tọa độ ra cho servo.

Arduino tutorial |  How to control servo motor with joystick and arduino
Arduino tutorial | How to control servo motor with joystick and arduino

Programming the board

To program the board, you will need to have installed the offline editor, or use the online editor. There’s no need to install any external libraries.

Before we begin, let’s take a look at some of the core functions in the program:

  • – includes the Servo library.


    #include

  • – create a servo object.


    Servo myservo

  • – attach the servo to a pin.


    myservo.attach(9)

  • – write a value to the servo (0-180).


    myservo.write(pos)

The code can be found by navigating to File > Examples > Servo > Sweep, or can be copied directly from below. Upload the program to the board.

1#include

23Servo myservo; // create servo object to control a servo4// twelve servo objects can be created on most boards56int pos = 0; // variable to store the servo position78void setup() {9 myservo.attach(9); // attaches the servo on pin 9 to the servo object10}1112void loop() {13 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees14 // in steps of 1 degree15 myservo.write(pos); // tell servo to go to position in variable ‘pos’16 delay(15); // waits 15ms for the servo to reach the position17 }18 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees19 myservo.write(pos); // tell servo to go to position in variable ‘pos’20 delay(15); // waits 15ms for the servo to reach the position21 }22}

Keywords searched by users: arduino uno r3 servo control

See more here: kientrucannam.vn

Leave a Reply

Your email address will not be published. Required fields are marked *