Servo Motor Arduino Code: Best Guide

| | |
Servo Motor Arduino Code: Programming Guide
Servo Motor Arduino Code

Servo Motor Arduino Code

Raspberry Pi vs Arduino

#include <Servo.h>

Servo myServo;  // Create a servo object

void setup() {

  myServo.attach(SERVO_PIN);  // Replace SERVO_PIN with the pin connected to your servo

}

void loop() {

  myServo.write(90);  // Rotate the servo to 90 degrees

  delay(1000);  // Wait for 1 second

}
#include <Servo.h>

Servo myServo;  // Create a servo object

void setup() {

  myServo.attach(9);  // Replace 9 with the pin number connected to your servo

}

void loop() {

  myServo.write(45);  // Rotate the servo to 45 degrees

  delay(1000);  // Wait for 1 second

}
#include <Servo.h>

Servo myServo;  // Create a servo object

const int triggerPin = 7;  // Trigger pin of the ultrasonic sensor

const int echoPin = 6;     // Echo pin of the ultrasonic sensor

void setup() {

  Serial.begin(9600);

  myServo.attach(9);  // Pin 9 is used for the servo motor

  pinMode(triggerPin, OUTPUT);

  pinMode(echoPin, INPUT);

}

void loop() {

  long duration, distance;

  digitalWrite(triggerPin, LOW);

  delayMicroseconds(2);

  digitalWrite(triggerPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(triggerPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");

  Serial.print(distance);

  Serial.println(" cm");

  int angle = map(distance, 0, 200, 0, 180); // Map distance to servo angle range (adjust as needed)

  myServo.write(angle); // Move the servo based on distance

  delay(500);  // Add a small delay before next measurement

}
#include <Servo.h>

Servo myServo;  // Create a servo object

int irSensorPin = 2;  // IR sensor connected to digital pin 2

void setup() {

  myServo.attach(9);  // Pin 9 is used for the servo motor

  pinMode(irSensorPin, INPUT);

  Serial.begin(9600);

}

void loop() {

  int irSensorValue = digitalRead(irSensorPin);

  if (irSensorValue == HIGH) {

    // Object detected by IR sensor

    Serial.println("Object Detected!");

    myServo.write(90);  // Rotate the servo to a specific angle (90 degrees in this case)

    delay(1000);  // Wait for 1 second

  } else {

    // No object detected

    Serial.println("No Object Detected");

    myServo.write(0);  // Rotate the servo to another angle (0 degrees in this case)

    delay(1000);  // Wait for 1 second

  }

}
#include <Servo.h>

Servo myServo;  // Create a servo object

void setup() {

  myServo.attach(SERVO_PIN);  // Replace SERVO_PIN with the pin connected to your servo

}

void loop() {

  myServo.write(180);  // Rotate the servo to 180 degrees

  delay(1000);  // Wait for 1 second

}
  • Go to Tinkercad and create a new circuit.
  • Search and add an Arduino board and a servo motor from the components panel.
  • Connect the servo’s signal pin to a PWM pin on the Arduino (usually marked with ~).
  • Connect the servo’s power pin to the 5V pin on the Arduino.
  • Connect the servo’s ground pin to the GND pin on the Arduino.
  • Ensure your circuit is properly wired.
  • Open the Code Editor for the Arduino component.
  • Use the following example code to control the servo motor:
#include <Servo.h>

Servo myServo;  // Create a servo object

void setup() {

  myServo.attach(9);  // Use the pin number where your servo is connected

}

void loop() {

  myServo.write(90);  // Rotate the servo to 90 degrees

  delay(1000);  // Wait for 1 second

  myServo.write(180);  // Rotate the servo to 180 degrees

  delay(1000);  // Wait for 1 second

}
#include <Servo.h>

Servo myServo;  // Create a servo object

int potPin = A0;  // Connect the potentiometer to analog pin A0

void setup() {

  myServo.attach(9);  // Use the pin number where your servo is connected

}

void loop() {

  int potValue = analogRead(potPin);  // Read the potentiometer value (0-1023)

  int servoAngle = map(potValue, 0, 1023, 0, 180);  // Map potentiometer value to servo angle range

  myServo.write(servoAngle);  // Set the servo position based on the potentiometer value

  delay(15);  // Add a small delay for stability (adjust as needed)

}

Frequently Asked Questions

How do I control a servo motor with Arduino?

To control a servo motor with Arduino, you need to use the Servo library. Include the library at the beginning of your code, create a Servo object, and use the attach() function to connect the servo to a specific pin. Then, use the write() function to set the desired angle.
#include <Servo.h>
 
Servo myServo;
 
void setup() {
  myServo.attach(9); // Connect the servo to pin 9
}
 
void loop() {
  myServo.write(90); // Move the servo to 90 degrees
  delay(1000);
  myServo.write(0);  // Move the servo to 0 degrees
  delay(1000);
}

How can I sweep a servo back and forth continuously?

You can use a for loop to sweep the servo back and forth. Adjust the angles and delay times based on your requirements.
#include <Servo.h>
 
Servo myServo;
 
void setup() {
  myServo.attach(9); // Connect the servo to pin 9
}
 
void loop() {
  for (int angle = 0; angle <= 180; angle += 5) {
    myServo.write(angle);
    delay(50);
  }
 
  delay(1000); // Pause at the end position
 
  for (int angle = 180; angle >= 0; angle -= 5) {
    myServo.write(angle);
    delay(50);
  }
 
  delay(1000); // Pause at the start position
}

How can I control multiple servo motors with Arduino?

You can control multiple servo motors by creating Servo objects for each motor and attaching them to different pins. Then, use the write() function for each servo to set their respective angles.
#include <Servo.h>
 
Servo servo1;
Servo servo2;
 
void setup() {
  servo1.attach(9); // Connect servo1 to pin 9
  servo2.attach(10); // Connect servo2 to pin 10
}
 
void loop() {
  servo1.write(90); // Move servo1 to 90 degrees
  delay(1000);
 
  servo2.write(180); // Move servo2 to 180 degrees
  delay(1000);
}

Can I use a potentiometer to control a servo?

Yes, you can use a potentiometer to control a servo motor. Connect the potentiometer to an analog pin on Arduino, read the analog input, map the values to servo angles, and use the write() function to control the servo.
#include <Servo.h>
 
Servo myServo;
int potPin = A0;
 
void setup() {
  myServo.attach(9); // Connect the servo to pin 9
}
 
void loop() {
  int potValue = analogRead(potPin);
  int angle = map(potValue, 0, 1023, 0, 180);
  myServo.write(angle);
  delay(15);
}

Worth Read Posts

  1. Touch Sensors
  2. ESP8266 Pinout
  3. Hysteresis Loss Formula
  4. ESP32 Pinout
  5. Work Function Formula
  6. Power Triangle
  7. BC547 Pinout

Follow us on LinkedIn”Electrical Insights” to get the latest updates in Electrical Engineering. You can also Follow us on LinkedIn and Facebook to see our latest posts on Electrical Engineering Topics.

Sharing is Caring

Similar Posts

2 Comments

Leave a Reply