Skip to content

Robotics

How to make a two-wheeled robot drive straight

A two-wheeled robot curves even when both motors get the same PWM, because no two motors, gearboxes and wheels are exactly alike. To drive straight, measure each wheel with an encoder, take the difference in ticks, and trim the speeds: slow the wheel that's ahead and speed up the one that's behind, in proportion to the difference.

Equal PWM isn't equal speed

analogWrite(9, 200) and analogWrite(10, 200) give each motor the same average voltage. But one motor might be 2% stronger, one gearbox stiffer, one tyre a little bigger. The faster wheel swings the nose toward the slower side, and over a couple of metres the robot ends up in the wall.

A fixed fudge, sending 195 to the strong side, works for a minute. Then the motors warm up, the battery sags or one wheel hits carpet, and the mismatch changes.

Measure, then correct

Give each wheel an encoder and count both. The difference says which wheel is ahead and by how much:

e = ticksleft − ticksright

On a rover with 12.7 cm between its wheels and a tick every 2 cm of travel, each tick of difference means the rover has turned about 9°, toward the side that's behind. So correct on every pass:

int e = ticksLeft - ticksRight;            // + means left is ahead
analogWrite(9, constrain(200 - e * 20, 0, 255));
analogWrite(10, constrain(200 + e * 20, 0, 255));

The wheel that's ahead slows down, the one behind speeds up, and the bigger the gap, the harder the correction. That's proportional control on the difference.

Live experiment

Down a 50 cm corridor

The left motor is 2% stronger than the right. The program trims each wheel by the tick difference, e = left − right, times a gain. With no trim the rover curves into the wall; gain 2 settles a tick off and drifts 22 cm; gain 20 stays within 3.4 cm of the middle.

Try it yourself: free lesson

“Drive dead straight” is lesson 1 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 14 before it first.

Choosing the gain

Swap the signs, adding to the leader and taking from the follower, and the correction makes things worse: the rover spins into the wall. If it does, check the signs first.

Better still

The tick difference measures the heading only as well as the wheels grip. A gyro measures the turn rate of the robot itself, so it isn't fooled by a slipping wheel; add its rate up over time and steer to keep the heading at zero. Many robots use both: encoders for distance, a gyro for heading. Add an integral term if a small steady offset remains, and the robot holds a line across a whole room.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Drive dead straight” is lesson 1 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 14 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3You find the left motor is a little stronger. Why not just send it 195 for ever, and 200 to the right?
    Show the answer

    Answer: The mismatch changes with load, battery and floor, so a fixed fudge drifts again. Motors change as they warm up, the battery sags, one wheel hits carpet. Measuring the difference on every pass corrects whatever the cause is today.

    • Why not “It would work for ever”? The mismatch isn't fixed: it shifts with warmth, battery voltage and floor, so a fixed fudge stops working.
    • Why not “The encoders would stop counting”? The encoders keep counting whatever speeds you send; that's what makes measuring the difference possible.
    • Why not “195 isn't a value analogWrite allows”? 195 is a perfectly good analogWrite value, anywhere from 0 to 255 is allowed.
  2. Question 2 of 3The wall is on the right and the rover is too far from it. To lean toward it…
    Show the answer

    Answer: Left wheel faster, right wheel slower. A faster left wheel swings the nose right, toward the wall. e is positive, so left gets + e ÷ 8 and right gets − e ÷ 8.

    • Why not “Stop”? Stopping doesn't bring the rover closer to the wall; it has to steer toward it.
    • Why not “Both faster”? Speeding both up equally keeps the same heading, so the rover doesn't lean toward the wall at all.
    • Why not “Right wheel faster”? A faster right wheel swings the nose left, away from a wall on the right.
  3. Question 3 of 3Which pair of speeds turns the rover right most tightly, without stopping a wheel?
    Show the answer

    Answer: Left 255, right 64. The bigger the difference between the sides, the tighter the turn. 255 and 64 drives round a circle about 20 cm across; 255 and 192 one about 85 cm across. Equal speeds don't turn at all, and a faster right wheel turns left.

    • Why not “Left 64, right 255”? With the right wheel faster, the rover turns left, not right.
    • Why not “Left 255, right 192”? 255 and 192 differ by less than 255 and 64, so they turn more gently, a circle about 85 cm across.
    • Why not “Left 128, right 128”? Equal speeds don't turn at all: the rover drives straight.
Keep practising: free lesson

“Drive dead straight” is lesson 1 of 6 in Waypoint Wharf, Robotics World 3. Free with an account. Lessons unlock in order, so you’ll do the 14 before it first.

Questions people ask

Why does my robot not drive straight?

No two motors are identical, and gearboxes, wheels and the weight on each side differ too. At the same PWM one wheel turns slightly faster, so the robot curves toward the slower side. The fix is to measure each wheel and correct the difference.

How do I make a robot drive straight without encoders?

Use a gyro instead: measure the turn rate, add it up into a heading, and steer to keep the heading at zero. Without any sensor you can only trim the speeds by hand, and that trim drifts as the battery and floor change.

How big should the correction gain be?

Big enough to correct a difference of one tick firmly, so the robot never holds a heading error, but not so big that one tick makes it swerve. Start small, raise it until the drift stops, and back off if the robot starts to wiggle.

Every topic