Programming simple servo sg90 rotation with Arduino Nano every

Hello,

This is my first project, I'm trying to make a simple easy surveillance camera project that will be mounted onto a servo sg90 to rotate the camera, using the servo, hooked up to a joystick. I'm learning coding at the same time using youtube. I came up with a code that worked ok but it continues to rotate the servo even though I have set limits for it's rotation angle. I'm trying to program a limited scope for the rotation from centre neutral position 90 degrees to maximum left 0 degrees and maximum right 180 degrees. I ran this by chatgpt too and the code should work according to chatgpt too.

Here is the code :


#include <Servo.h>

// Define pins
#define SERVO_PIN 9
#define JOYSTICK_X A0

Servo myServo;

// Define servo limit angles
const int LEFT_LIMIT = 0;    // Left limit (max left)
const int RIGHT_LIMIT = 180;  // Right limit (max right)
const int NEUTRAL = 90;       // Neutral position (center)

void setup() {
  myServo.attach(SERVO_PIN);  // Attach the servo to pin 9
  myServo.write(NEUTRAL);     // Set the initial servo position to neutral (90°)
  Serial.begin(9600);         // Start serial communication for debugging
}

void loop() {
  int joystickX = analogRead(JOYSTICK_X); // Read the X-axis value (joystick left-right)

  // Map joystick X-axis value (0 to 1023) to a servo angle between 0° and 180°
  int targetAngle = map(joystickX, 0, 1023, LEFT_LIMIT, RIGHT_LIMIT);

  // Ensure that the servo stays within the defined limits
  if (targetAngle < LEFT_LIMIT) {
    targetAngle = LEFT_LIMIT;  // Do not go below 0°
  }
  if (targetAngle > RIGHT_LIMIT) {
    targetAngle = RIGHT_LIMIT;  // Do not go above 180°
  }

  // Move the servo to the target angle if the position has changed
  static int lastAngle = NEUTRAL;  // Last known angle

  if (targetAngle != lastAngle) {
    myServo.write(targetAngle);  // Move the servo to the target angle
    lastAngle = targetAngle;     // Update the last angle
  }

  // Debugging information (optional)
  Serial.print("Joystick X: ");
  Serial.print(joystickX);
  Serial.print(" | Servo Angle: ");
  Serial.println(targetAngle);

  // Optional delay to control speed (smaller delay = faster movement)
  delay(15); // You can adjust this to control how fast the servo moves
}


I understand I could just simply use a different servo that has a limit of 180 degree rotation but thought I would try to use this servo as it was already in a bunch of stuff I was given.

Thanks for any help.

Welcome to the forum

It sounds like you have not got a servo but instead have a continuous rotation "servo"

If so you can only set its direction and speed of rotation and not its angle. Try the Servo Sweep example in the IDE and report how the servo behaves

A link to the actual servo that you have would also be helpful

Hello,

Thanks for the welcome and the quick reply! I will check the pre programmed sweep movement in a moment inside the IFDE. The servo is TowerPro, continuous 360 degrees , digital 9g, SG90-HV.

You do not have a servo

I see. So it's not a servo. I tried the sweep function and it worked but again it's automated I have no joystick control.

I have gone back through the box of stuff, there's a TowerPro SG92R micro servo. So that's definitely a servo.... I'll try this with my code....

The Sweep example will run with your "servo" but instead of sweeping the output back and forth between 0 and 180 degrees as it should the "servo" will run in one direction, speeding up as it goes, then reverse and do the same the other way and repeat this action. The rotation will not be limited to 0 and 180 degrees

Is that what happens ?

Yes, that's what happened, but I have since opened the servo that was in the box and hooked it up. Just to speed up the coding I went to chatgpt and got it coded' this works how I wanted it to originally :

#include <Servo.h>  // Include the Servo library

// Define the pin numbers
const int joyX = A0;  // Joystick X-axis connected to analog pin A0
const int joyY = A1;  // Joystick Y-axis connected to analog pin A1 (optional)

Servo myServo;  // Create a Servo object
int servoPin = 9;  // Define the pin for the servo

void setup() {
  myServo.attach(servoPin);  // Attach the servo control pin to digital pin 9
  Serial.begin(9600);  // Start serial communication for debugging
}

void loop() {
  int xValue = analogRead(joyX);  // Read the joystick X-axis value
  // int yValue = analogRead(joyY);  // Read the joystick Y-axis value (optional)

  // Map the X-axis joystick value (0-1023) to a servo angle (0-180 degrees)
  int mappedValue = map(xValue, 0, 1023, 0, 180);

  // Write the mapped value to the servo motor
  myServo.write(mappedValue);

  // Print values to the Serial Monitor for debugging (optional)
  Serial.print("Joystick X: ");
  Serial.print(xValue);
  Serial.print("\tMapped Servo Angle: ");
  Serial.println(mappedValue);

  delay(15);  // Short delay to smooth the servo movement
}

Thanks for your help and time by the way!

I am glad that I could help. It is quite a common problem when users do not understand that all servos are actually servos !

Good luck with your project

[EDIT]
That should have said
users do not understand that all servos are not actually servos !

Thanks!

Huh?

Whoops !

Missing a not

Edited

Maybe but a continuous rotating servo is still a servo.

To my way of thinking, and certainly in the context of this topic, describing something as a servo means that its position can be controlled

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.