controling air cylinder with a joystick.

Thank you so much for reading this.

I am having problems controlling an air cylinder moving a heavy weight with my joystick. With my program below I get the cylinder overshooting and undershooting the desired positions in a gradually decreasing amounts three or four times until it finally reaches the desired position. Part of it must be the compressibility of the air in the cylinder (the cylinder reacts kind of like a spring). I would love any other ways of looking at this problem.

// control a dual acting air cylinder (one way pushes/ one way pulls) with a joystick

void setup() { 
  int joystickpot =0;
  int aircylinderpot =0;
  pinMode(5,OUTPUT);  //retract cylinder valve
  pinMode(6,OUTPUT);  //extend cylinder valve

void loop() {

  int joystickpot=analogRead(A0);       //reads the joystick potentionmeter
  int aircylinderpot=analogRead(A1);   //reads positional pototentionmeter on the air cylinder
  int diff=joystickpot-aircylinderpot;  //is difference between joystick and the aircylinder positions
if (diff<-100){analogWrite(6,250);}   // open the air cylinder extend valve fully
if (diff<=-50 && diff>-100){analogWrite(6,100);}  //open the air cylinder extend valve halfway 
if (diff<=-10 && diff>-50){analogWrite(6,50);}    //open the air cylinder extend valve slightly
if (diff>-10 && diff<10){analogWrite(6,0);analogWrite(5,0);}  //if joystick and cylinder position are close to each other close air cylinder valves.
if (diff>=10 && diff<50){analogWrite(5,50);}      //open the air cylinder retract valve slightly
if (diff>= 50 && diff<100){analogWrite(5,100);}   //open the air cylinder retract valve halfway
if (diff>=100){analogWrite(5,250);}   //open the air cylinder retract valve fully  


You already have the answer. You need to develop a way to anticipate the movement to the location where you want the weight and reverse the air to slow and stop the weight. Can you do that with the joystick? I don't know. The problems are three: compressiblity of the gas, temperature of the gas and the inertia of the weight. I guess there is a fourth, the distance to move the weight. Perhaps a 5th, the direction to move the weight.

Quite a list.


thanks Paul. guess i am taking on a lot here. thought i might be able to make some equation with speed verses destination.....

It is worse than just speed vs destination - the mass is also a major factor. You may be able to get close by working with some sort of PID algorithm, but there is a reason that most things that move things like that use a non-compressible liquid (hydraulics) instead of air ... they seem to reserve the air cylinders for rides at the fair for exactly the reasons you are running into :confused: I guess you could use some sort of shock absorber which would limit the rate of motion and damp out the oscillations.

If you drop your speeds, does that help.
Say halve the 250 to 100
100 to 50
50 to 30, not sure what your minimum speed is.

Tom.... :slight_smile:

This sounds like a job for a PID controller. The "D" derivative - speed - term will reduce the drive as the device speeds up towards the target. So if it goes fast, it starts to slow down, before it reaches the goal. The idea is to arrive at the target position with zero speed.

This is not intended to disagree with what @MorganS has suggested in Reply #5 - but I am just wondering if there is a very simple solution.

Do you need the cylinder to react immediately to a change in position of the joystick - or would it be an option for the Arduino to read the new joystick position and then command the cylinder to move to that position at a slow rate which avoids the overshoot problems you mention? This idea might work better with a potentiometer knob rather than a joystick as the knob could have a scale showing the positions. Of course you could put a scale on a joystick.

You have not said if the cylinder has a position indicating device that can send the actual position back to the Arduino. That would be essential for a PID control system. It is in the code


This sounds impossible to me. Valves let air in and out of the cylinder, but the cylinder contains a volume of compressed air which will expand or contract depending on the load . You can't instantaneously change the compressed air pressure by any control system whatsoever.

try hydraulics...



I have seen some very impressive robots that do accurate positioning with compressed air only. Festo has a huge catalog of equipment which will do this.

If the load does not change or it does not change more rapidly than the control system can keep up, then there is no great advantage to hydraulics.

Wow guys great insightful help here! I am using air because it is so much cheaper and safer. I am basically making a circus ride (flight simulator). I am using proportional valves which are rather rare in pneumatic applications. The load is fixed as well as the psi, etc. Its really only a problem of writing good software code that can anticipate the motion and speed and then brake it before the overshoot. Will update as my programming and sketches improve!!!!

Maybe a link to this rare valve?

Do you have an exhaust port configured on each side of the cylinders, to let the air out on the non pressurised side as the ram extends or retracts?

Thanks.. Tom... :slight_smile:

You have a big deadzone where your input is between -10 and 10 and your output is zero. You are providing no control when your input is near zero. How about trying a continuous control.

if(diff > 100) diff = 100;  // clamp the input
if(diff < -100) diff = -100;
if (diff <0){analogWrite(5, -2.5*diff); analogWrite(6,0);}  // set the outputs
else {analogWrite(5,0); analogWrite(6, 2.5*diff);}

charliesixpack, looks good.

Can I suggest that when you reach your required position within the hysteresis region, you have BOTH extend and retract valves open at 50
So when you reach the region, the opposite force is applied, both sides now at equal pressure so a braking effect should occur., in fact a locking effect.
If you just keep decreasing the pushing force, your inertia will take over when you slow, having an opposite force should help retard that.

Tom… :slight_smile: