Servo angle

Thread Starter

Dabu WhiteHacker

Joined Sep 5, 2017
68
hey i was trying to control the servo using joystick(potentiometer).
i am but stuck at a problem
when joystick is at center servo is at 90° when i move the joystick to left, servo sets at 0° and when i release the joystick it came back to its 90° but when i move the joystick to right, the server does sets to 180° but does not come back to 90° on release the joystick, it sets to a little more than 90° like 100°
what could be the problem ?

Code:
#include <Servo.h>
Servo servo;
int sp ;
int js_x;
void setup()
{
servo.attach(3);
}
void loop()
{
  sp= map(js_x ,0 ,1023, 0, 180);
   servo.write(sp);
}
Mod edit: code tags
 
Last edited by a moderator:

Sensacell

Joined Jun 19, 2012
3,447
Might not be a code problem, could be joystick hysteresis.

Move the joystick and read the voltage with a voltmeter, see if it returns to the same value each way.
 

shteii01

Joined Feb 19, 2010
4,644
And a third thing.
Uno ADC is unipolar(?). Meaning it is from 0 to +5V, which digitally is 0 to 255 for 8 bit, 0 to 1023 for 10 bit.

You joystick might be bipolar. -X volts for left, 0V for center, +X volts for right. Digitally, for 8 bit ADC, it would be -128 for left, 0 for center, 127 for right.
So.
If your joystick is bipolar, then Uno ADC never sees the negative voltages because it can only go down to 0 volts, anything lower than 0 volts will simply be seen as 0 volts.
 

philba

Joined Aug 17, 2017
959
The arduino uno returns a 10bit value on analogRead().

I don't see how the original code works as there is no analogRead() to get the adc value. Actual code that shows the problem wold help.
 

strantor

Joined Oct 3, 2010
6,798
Might not be a code problem, could be joystick hysteresis.

Move the joystick and read the voltage with a voltmeter, see if it returns to the same value each way.
+1. the pot shaft in your joystick may be slipping, or may just just be a crappy pot.
 
Top