Arduino nano project with servo motor | How does work servo motor.

Hello guys, Welcome back to my blog. We have already described 10 posts on this Arduino. These posts are created to be very easy to learn. Today we are going to talk about the servo motor that many people like to learn.OK, let’s go to today’s post.

What is the servo motor?

  A servo motor usually includes a simple DC motor but it has a gear wheel system. Also, the servo motor includes a sensor system for measuring gear wheel rotation. So we can rotate it to the desired digress. Usually, the servo motor can rotate from 0 to 180 degrees. You can also buy 360-degree servo motors in the market if you want. These servo motors are mainly used to make robotic arms. We can buy servo motors of different sizes in the market. It depends on the project we are doing. These servo motors can be rotated by the PWM signal on the Arduino board. You can use PWM pins on the Arduino board for that. The servo motor consists of three wires. The wire colors are orange, red, and brown. The red and brown wires are used to power the servo motor. The orange wire is used to rotate the servo motor to the required number of degrees. We can use a PWM signal for this purpose. These points are common to all servo motors.

Okay, let’s do a simple project using a servo motor. We use a very cheap servo motor for this project. The required components and how to do it step by step are as follows.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Connect the servo motor to your Arduino board using the circuit diagram below.

Step 3

Look at the code below.

#include <Servo.h>
Servo srituhobby;

void setup() {
  srituhobby.attach(3);
}

void loop() {
  srituhobby.write(0);
  delay(400);
  srituhobby.write(180);
  delay(400);
}

Let us learn something new from this code. It’s about libraries.

What is the Arduino library file?

A library is a file containing code issued by a company to make it easier for us to work with a device. The servo motor library file is included in the Arduino IDE. Include it as follows.

Create an object using any name you like.

Servo srituhobby;

This code informs us about the PWM pin we are using.

void setup() {
  srituhobby.attach(3);
}

This code will be inserted into the Arduino board in the number of degrees required for the servo motor to rotate.

void loop() {
  srituhobby.write(0);
  delay(400);
  srituhobby.write(180);
  delay(400);
}

Step 4

Now, select the board and port. After, click the upload button.

OK, let’s do this project using the Arduino Nano board. Follow the steps below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, connect the servo motor to the Arduino Nano board.

Step 2

Use the code above for this project. The D9 PIN was used for this project.

  • The complete program of this project – Download
#include <Servo.h>
Servo srituhobby;

void setup() {
  srituhobby.attach(9);
}

void loop() {
  srituhobby.write(0);
  delay(400);
  srituhobby.write(180);
  delay(400);
}

Step 3

Now, select the board and port. After, click the upload button.

Now, you can test this project. The full video guide is below.

Arduino nano project with servo motor | How does work servo motor

Also, you can test the code below. We will meet in the next post. Have a good day.

#include <Servo.h>
Servo srituhobby;

void setup() {
  srituhobby.attach(9);//set arduino PWM pin D9
}
void loop() {
  for (int a = 0 ; a <= 180 ; a++) {
      srituhobby.write(a);
    delay(10);
  }
  for (int k = 179; k > 0 ; k--) {
   srituhobby.write(k);
   delay(10);
 }
}

Similar Posts

Leave a Reply

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