Arduino Archives - SANSKAR'S CODE https://sanskarsontakke.com/category/arduino/ Elevating Ideas through Code Mon, 20 May 2024 09:35:44 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 Servo With Arduino https://sanskarsontakke.com/2024/05/19/servo-with-arduino/ https://sanskarsontakke.com/2024/05/19/servo-with-arduino/#respond Sun, 19 May 2024 10:13:34 +0000 https://sanskarsontakke.com/?p=131 In this tutorial I will tell you how to control a servo motor with Arduino Mega/Uno/Nano. First we will create the circuit. Then we will include the header file ‘<servo.h>‘ We have to create the servo object like this : Servo myservo;’ Then we will write ‘ myservo.attach(9)‘ to specify to which pin we have ... Read more

The post Servo With Arduino appeared first on SANSKAR'S CODE.

]]>
In this tutorial I will tell you how to control a servo motor with Arduino Mega/Uno/Nano.

First we will create the circuit.

Then we will include the header file ‘<servo.h>

We have to create the servo object like this : Servo myservo;’

Then we will write ‘ myservo.attach(9)‘ to specify to which pin we have connected our servo.

So the code will be :

#include <Servo.h>

Servo myservo;
int pos = 0; 

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

void loop() {
    myservo.write(0);    
    delay(1000);
    myservo.write(180);  
}

In this code the servo motor will go to 0 degree and then to 180 degree after 1 second.

Servo Sweep

To make a servo sweep the code can be modified like this using for loops :

#include <Servo.h>

Servo myservo; 

int position = 0;   

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

void loop() {
  for (position = 0; position <= 180; position += 1) {
    myservo.write(position);         
    delay(15);               
  }
  for (position = 180; position >= 0; position -= 1) {
    myservo.write(position); 
    delay(15); 
  }
}

This code will make the servo sweep like this :

The post Servo With Arduino appeared first on SANSKAR'S CODE.

]]>
https://sanskarsontakke.com/2024/05/19/servo-with-arduino/feed/ 0