DC motor direction/speed control with joystick

Relatively new to arduino, following paul mcwhorter's tutorials on yt currently on lesson 39 where the goal is to use a joystick to control the direction/speed of the dc motor. Having trouble with the joystick values in the positive x, its resting state is around 370 and its highest is around 770, but whenever i get to that point, it glitches out and is randomly around 500 - 770 which makes it pretty inconsistent. The dc motor also constantly beeps when not in negative. The negative side works pretty well, however have been times when i have to give it a nudge. the wiring diagram below is from paul's website, and the only differences are the joystick pins where i attached 5v and ground to their rails, and the x pin to a0. Any help would be greatly appreciated.

int speedP = 5;
int dir1 = 4;
int dir2 = 3;
int mSpeed;
int rSpeed;
int xP = A0;
int xVal;
void setup() {
  // put your setup code here, to run once:
  pinMode(speedP, OUTPUT);
  pinMode(dir1, OUTPUT);
  pinMode(dir2, OUTPUT);
  pinMode(xP, INPUT);
  Serial.begin(9600);
}
void loop()  {
  // put your main code here, to run repeatedly:
  xVal = analogRead(xP);
  mSpeed = map(xVal, 0, 1023, -255, 255);
  if (mSpeed<-255){
    mSpeed = -255;
  }
  if (mSpeed>255){
    mSpeed = 255;
  }
  rSpeed = abs(mSpeed);
  if (mSpeed<0){
  digitalWrite(dir1, LOW);
  digitalWrite(dir2, HIGH);
  }
  if (mSpeed>=0) {
  digitalWrite(dir1, HIGH);
  digitalWrite(dir2, LOW);    
  }
  analogWrite(speedP, rSpeed);
  Serial.print("x: ");
  Serial.print(xVal);
  Serial.print(", in range: ");
  Serial.print(mSpeed);
  Serial.print(", Abs val: ");
  Serial.println(rSpeed);
}

The code as-is works for me in simulation:

If your joystick is limited to the 370-770 range, you might want to look at this line in your code:

and consider what map does:

If 770 is your top value, try replacing the 1023 with it so when you get to 770 on the joystick, it is putting full power (analogWrite(speedP, 255);) to the motor instead of half with analogWrite(speedP, 128);

Paul McWhorters video:

Tried 770 as the upper value and still glitches. So i guess its a hardware problem?

Yes. I do not think it is the code. It could be the glitchy joystick or bad connections.

When you atry with the 770 as the upper value, does it print lines like:

x: 770, in range: 255, Abs val: 255

to show it is trying to deliver 100% duty cycle power?

Also, the L293D is ancient and inefficient. A more modern MOSFET based driver like a TB6612FNG module would leave more power for the motor.

Nope the x val fluctuates the same as before

OK, but the Abs value -- if it is showing '255' then it should be giving the motor full power and might need less of a kick than at 128.

If by changing the 1023 to 770 you doubled the duty cycle from 128/255 to 255/255 and the motor doesn't react any better, it could be a power problem.

Do you have a common ground between the arduino and the breadboard? If that power supply is putting out 5V, the voltage drop through the L293D will reduce it by 1.4V to 3.6V and might run the motor much slower/weaker than if it were directly connected to 5V

1 Like

Hi, @vertily
Welcome to the forum.
Thanks for using code tags. :+1:

Can you please post a copy of your complete circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Can you post some images of your project?
So we can see your component layout.

Have you got the gnd of the UNO connected to the gnd of the protoboard?
image

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

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