as the question title states, how will I go about turning the servo opposite of 90 degrees?

I have written my code as such;

  // Turn servo to the right
  if(x > 520 && y < 510 && y > 490){

  //Turn left
  }else if(x < 490 && y < 510 && y > 490){

 //Turn backwards
} else if(y < 490 && x < 520 && x > 490){

Obviously -90 isn't an actual degree on the servo, and it will only turn between 0 and 180. I was therefore wondering if there was any other way to code it?

The servo is connected to a joystick, and the plan is to built a 360 degrees vertical and 180 degrees horizontal turret of sorts. Every other command works, and when I replace -90 with 90 degrees, it works as well. So it is wired correctly.

Of course I would have to add another joystick and another servo later on, but at this stage I am simply testing my own coding skills.

  • 1
    There is no such thing as -90°. Just change your concept of what is "straight ahead"... – Majenko Mar 31 '20 at 17:51
  • It is unclear, how the joystick is connected to the servo and which movements you exactly need on the joystick. – chrisl Mar 31 '20 at 18:18
  • @chrisl In reality I wish for the servo to mimic the joystick. But since this is first time I am programming with servos and a joystick, I simply wish for a simple up, down, left, right, upper- left and right corner and then lower- right and left corner. I am basing this on a system, where the directions depending on whether or not x and/or y are above or below the middle value of 510-ish. As you move the joystick from vertical left to right it will increase from 0 to 1320, and the same for horizontal down to up. The x and y variables in my program are based on those values. – GeorgeWTrump Mar 31 '20 at 18:29
  • See my edited answer. You need two servos to control a 2 axis joystick. – Duncan C Apr 1 '20 at 0:03

Normal servos only have a range of 180°. You can give yourself an "offset", so you have -90° to 90°, but you only ever get 180° range. You can't go -90° to 180°.

Using mechanics (outside the scope of this site) you can multiply your movement (think gears) so that your range is bigger.

  • This seems too advanced for me in terms of the mechanical aspect. Would it possible to use a DC motor for the base and a function, that replicates the movement of the joystick? Or do you see any immediate problems with that? – GeorgeWTrump Mar 31 '20 at 18:14
  • You can get special servos with wider ranges, and you can get "continuous rotation" servos that go round and round, but you don't specify an angle with those, but a speed. If you want to use anything other than a "normal" servo you will need some form of feedback to know what angle you're facing. – Majenko Mar 31 '20 at 18:19
  • Thank you, are these "wide range" servos called anything special? When I google "360 degree servo" I simply get ads for regular servos. – GeorgeWTrump Mar 31 '20 at 18:33
  • @GeorgeWTrump, I'd recommend a stepper motor. Also look into motors with quadrature encoders and index pulses. They can be used to set position. Any of the above can rotate continuously in either direction while still knowing it's angle from 0 to 360 deg or -180 to +180 or however you want to think about it. With the proper control algorithm and PID loop, you can also command exact position. This is more advanced, however. – Gabriel Staples Aug 29 '20 at 2:43

You say "The servo is connected to a joystick, and the plan is to built a 360 degrees vertical and 180 degrees horizontal turret of sorts. "

That doesn't make sense. How could a joystick rotate 360° in one dimension and 180° in the other dimension?

A joystick does not need an angle greater than 180°. It's mounted to a flat surface, and can only move 180° in two different dimensions, X and Y. (And actually probably more like 120° in each dimension.)

Use two 180 degree servos, one for the X axis and one for the Y axis.

Set the "neutral" position to 0

You can map the range 0 - 1320 to -90 to 90 using a map statement. (or adjust as needed since the range of motion of the joystick is probably less than 180 degrees - more like 120°, like chrisl said in their comment.)

See this article for info on how a 2-axis joystick works. Specifically look at the section titled "Gimbal Mechanism" a little ways down in the article.

  • I am not sure that I understand? The joystick itself already moves 360 degrees? What I need to move 360 is the platform for the turret. Of course, I could always move it counter to what it should be pointing at, and then move the tilting backwards (i.e 180 degrees ) but that seems awkward to me personally. But mapping it -90 to 90, will still only result in 180 degrees. This means the target can simply just move behind the turret. – GeorgeWTrump Mar 31 '20 at 20:01
  • What type of joystick do you have? A normal joystick (like on the controllers of game consoles) rotate maybe 120 degrees on the x and the y axis. The axes are independent. So you would need 2 servos, each moving 120 degrees. Where is your joystick moving 360 degrees? – chrisl Mar 31 '20 at 20:59
  • You don't want one 360° rotation. You want to tilt it on separate X and Y axes by ±90° each (actually probably more like ±60 degrees, as chrisl said in their comment.) – Duncan C Mar 31 '20 at 21:02
  • Oh, but I would have 2 joysticks. One normal joystick with a normal code, that will output the normal range of 0-180 degrees on the tilt. Meanwhile, the other joystick will have a seperate "x" and "y" axis. Of course, I could just use the y axis for all backwards facing degrees by "y = y-output + 180". So that if y = 1, it will be 181, and therefore backwards. And for the forward facing degrees (i.e 0-180) I can simply output the x-value. – GeorgeWTrump Apr 2 '20 at 12:52
  • Now that is my train of thought, does this change your suggestion? As I understand it, you believe that I am limited to a total range of 180 degrees (in actuality 120 degrees), is that why you suggest using only 180 degrees of motion? Or am I mistaken? – GeorgeWTrump Apr 2 '20 at 12:55

If I understand correctly, you only need to move your servo through 180 degrees total, from one limit to the other, but you want -90 to represent one of the limits and +90 to represent the opposite limit? If so, just add 90 to the servo angle before you send it:


This turns -90 into 0, 0 into 90, and 90 into 180. You get to use the coordinates that you like (-90 - +90) and the servo sees the coordinates it needs (0 - 180).

This is a simple case of mapping; there is a map() function that can do this for you for all mapping all kinds of ranges, one into another. Mapping (-90 - 90) into (0 - 180) is so straightforward that the map function really doesn't add anything, but in other cases it's handy to know about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.