Im making a small, controllable toy car. Im using the L239D motor controller to drive 2 6V DC motors, which I intend to control with a joystick. Using the L239D state table (shown below), I can make the car move forward, backward, left and right.

Thats all good, but I want the car to do all the bits in between those movements (drifting slightly left/right etc depending on joystick location). So far I have two separate functions that look at the x-axis and y-axis value from the joystick, scale them and apply a pwm signal. This works fine when only one function is on but ofcourse, they overwrite each other.

My idea then was to create a variable that takes the displacement of the joystick in both axis and divides them, this will be the duty cycle. I then have a delay at the end of each loop which is sized by this duty cycle. The only problem is that the motor stutters a lot and it dosent work very well at all.

I was wondering if anyone has a better idea for getting full motor control with the joy stick rather than the simple block movements (full right, full left, full forward, full backward). Thanks!

enter image description here enter image description here

  • Please look at the chip on the driver board and verify the number on it. The usual L239 is a diode, while the usual L293 is a driver (with 4 half-H-bridges, making 2 H-bridges). If the chip is indeed L293 instead of L239, please edit your question and fix the number all thru. – James Waldby - jwpat7 Aug 18 '15 at 13:10
  • Ah sorry, Im pretty sure out of the two people could guess what I'm on about anyway just through common sense – Chris Collins Aug 18 '15 at 15:47
  • Did you look at the chip and figure out the number? Please fix the question if the question is wrong. – James Waldby - jwpat7 Aug 18 '15 at 15:55

The problem here is that you lack the concept of speed with your motors.

It's not enough to turn the motors on or off in the right direction, you have to control the speed of them.

Your left/right control is fine as it is (without the delay in your loop), which is controlled by the inputs being either on or off to define the flow of current through the motor. Now all you need to do is control the speed of the motors while they are in those states.

Fortunately there is another pair of inputs to the driver chip that you have completely overlooked: ENABLE1 and ENABLE2. These can accept a PWM signal and define the ratio of on-to-off for the motor on that channel.

So you can connect a PWM signal to each of those inputs (all the other inputs are just straight digital ON/OFF signals), and setting the duty cycle of those PWM signals defines the speed of the motor (0 = stop, 255 = full speed).

Now all you need to do is interpret the joystick properly, for instance:

  • Between centre and half-left: Left = Stop, Right = Forward, Enable Right = percentage of half the joystick range
  • Between half-left and full-left: Left = Reverse, Right = Forward, Enable Right = 100%, Enable Left = percentage of half the remaining joystick range

And of course reversed for right turns.

You could incorporate into that the forward/backward position of the joystick to add an overall forward or backward speed to the motors so you have full 2D motion.

It may be better to visualise the joystick in Polar space instead of simple Cartesian space. Convert the X/Y coordinates of the joystick's position into an angle and distance from the centre point. The distance defines the overall speed, and the angle defines how that speed is split between the left and right motors (enable pins), and the direction of those motors (input pins).

| improve this answer | |
  • Brilliant. You knew exactly where I went wrong. I decided to create a series of if statements with flags that dictated which pins to set digitally. I then created a value that was the maximum displacement of both axis (0 to 255, whichever axis is bigger) and applied that value as a PWM signal to the enable lines. Works perfectly! – Chris Collins Aug 18 '15 at 17:11

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.