Hey everyone, I'm currently making a robotic arm using servos and rotational sensors. The Arm is composed of 4 micro SG90 servos and each is controlled by a B10K rotational sensor. The parts for the robotic arm are all 3D printed and they all are attached just fine.
My main concern is when i do run the code, the servo sometimes doesn't even respond when i try to turn the B10K. Usually it then turns after 2-3 seconds after. As well, the movement itself isn't that smooth and it sometimes just jitters back and forth. If any of you could please help me make it so that it is more smooth, that will be appreciated. Thanks! (PS, im only using two sensors and servos for now just to test. I will add the other two later)
The code:
#include <Servo.h>
Servo servo1;
Servo servo2;
int servoPin1 = 9;
int rotationPin1 = A0;
int servoPin2 = 10;
int rotationPin2 = A1;
void setup() {
servo1.attach(servoPin1);
servo2.attach(servoPin2);
}
void loop() {
int read1 = analogRead(rotationPin1);
int angle1 = map(read1, 0, 1023, 0, 180);
servo1.write(angle1);
int read2 = analogRead(rotationPin2);
int angle2 = map(read2, 0, 1023, 0, 180);
servo2.write(angle2);
}
The first thing you need to do is power the servos externally.
Your sketch works here as expected when using an external 5V servo power supply.
Print the angle values to the serial monitor to confirm they are not fluctuating.
void setup()
{
Serial.begin(115200); //make sure you set the serial monitor to the same rate <——<<<<
servo1.attach(servoPin1);
servo2.attach(servoPin2);
}
void loop()
{
int read1 = analogRead(rotationPin1);
int angle1 = map(read1, 0, 1023, 0, 180);
Serial.print(“angle1 = “);
Serial.println(angle1);
servo1.write(angle1);
int read2 = analogRead(rotationPin2);
int angle2 = map(read2, 0, 1023, 0, 180);
Serial.print(“angle2 = “);
Serial.println(angle2);
servo2.write(angle2);
//for testing, slow things down a bit
delay(500);
}
I have powered it with a 9V battery and both angles are showing in the serial monitor as I move the potentiometers. It still is a bit jittery in a certain spot and just moves back and forth rapidly.