Back to Course

Arduino Programming using Arduino IDE

0% Complete
0/0 Steps
  1. Chapter-1->How to download Arduino IDE?
    3 Topics
    |
    1 Quiz
  2. Chapter-2-> Introduction to Arduino IDE
    3 Topics
    |
    1 Quiz
  3. Chapter-3–> How to connect any board with Arduino IDE?
    4 Topics
    |
    1 Quiz
  4. Chapter-4–>Steps to write a code in Arduino Programming
    5 Topics
    |
    1 Quiz
  5. Chapter-5–>Introduction to Arduino Uno
    5 Topics
    |
    1 Quiz
  6. Chapter-6–>Explanation of Variables and operators in programming
    5 Topics
    |
    1 Quiz
  7. Chapter-7–>How to use conditional statement and loops?
    6 Topics
    |
    1 Quiz
  8. Chapter-8–>How to use analog input in Arduino IDE ?
    6 Topics
    |
    1 Quiz
  9. Chapter-9–>How to add libraries in Arduino IDE ?
    6 Topics
    |
    1 Quiz
  10. Chapter-10–>How the Arduino Serial library performs serial communication?
    4 Topics
    |
    1 Quiz
  11. Chapter-11–>Interfacing of Dht11 sensor with Arduino
    4 Topics
    |
    1 Quiz
  12. Chapter-12–>Troubleshooting and Errors in Programming
    3 Topics
    |
    1 Quiz
  13. Assessment
    6 Topics
Lesson 9, Topic 5
In Progress

9.5 Coding using Arduino IDE

23/09/2021
Lesson Progress
0% Complete

Coding :

To control the servo motor using Arduino Uno,first we have to code the circuit using Arduino IDE. As to code the servo motor, add the library of servo motor in Arduino IDE.Arduino IDE have in built library of servo, so add that library using the steps, as shown in the previous topic.Go to Sketch< Include library < Servo as shown in the below image:

Steps to do coding in Arduino IDE

1. Open Arduino IDE from the desktop.You will be see the following page when you will open Arduino IDE:
2. Now, clear the page open in Arduino IDE and then include the servo library.Next, we will define a variable named “servoPin” with integer data type to assign which pin we are using of Arduino UNO.

Expression :

#include <Servo.h>
int servoPin = 9;

3. Now, Create a LIBRARY OBJECT FILE (.o) that is linked with programs which use servo library.

Expression :

Servo Servo1;

4. In void setup ( ) function, we need to attach the servo object with the servo pin which er have used. 

Expression :

void setup()

{
// We need to attach the servo to the used pin number
Servo1.attach(servoPin);
}

4. In void loop() function, we write the main code and it run over and over again.first we will make the servo to move to 0 degree, then to 90 degree and then 180 degree.
Note: The servo goes to a specific position depending on the input signal, a continuous rotation servo either rotates clockwise or counter-clockwise at a speed proportional to the signal. For example, the Servo1.write(0) function will make the servomotor spin counter-clockwise at full speed. The Servo1.write(90) function will stop the motor and Servo1.write(180) will turn the motor clockwise at full speed.

Expression: 

void loop(){
// Make servo go to 0 degrees
Servo1.write(0);
delay(1000);
// Make servo go to 90 degrees
Servo1.write(90);
delay(1000);
// Make servo go to 180 degrees
Servo1.write(180);
delay(1000);
}

Note : Lines mentioned in the “// ” are the comments.These comments are written for the explanation of program to programmer.

Complete Code:

#include <Servo.h>

int servoPin = 9;

Servo Servo1;
void setup() {

Servo1.attach(servoPin);
}
void loop(){

Servo1.write(0);
delay(1000);

Servo1.write(90);
delay(1000);

Servo1.write(180);
delay(1000);
}