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)

}

Subscribe to our Newsletter “Electrical Insights Daily” 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.

Related Posts

Raspberry Pi vs Arduino: ESP32, Beagle Board and PI PICO Best Guide

Within the sphere of open-source hardware, the comparative analysis between Raspberry Pi…

Servo Motor Arduino Code: Best Guide

Working with servo motors through Servo Motor Arduino code is like giving instructions to…

TOP 60 Stepper Motor Interview Questions: Best Guide

Candidates preparing for these interviews face a diverse array of challenges encapsulated…

Arduino UNO R3 Pinout, Specifications and Best Guide

The Arduino Uno R3 is a fundamental microcontroller board renowned for its versatility and…

Ultrasonic Sensor Motor with Arduino: Best Overview

The combination of an ultrasonic Sensor Motor with Arduino microcontroller opens up a…

Ultrasonic Sensor Servo Motor Arduino: A Comprehensive Overview

Ultrasonic Sensor Servo Motor Arduino combination is a powerful setup that enables precise…
Sharing is Caring

Similar Posts

Leave a Reply

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

Advertisement