Wheel-encoder square driver on an Arduino Uno
A two-wheel robot that counts pulses from a slot sensor on each wheel. A feedback loop holds a steady speed on any battery, and the pulse counts drive a square: four sides and four corners. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Advanced About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $40. On Mokxi they're free.
What you'll learn
- Sensors
- Motors
- Using the Motor driver
- Using the Robot car chassis
- Difficulty: Advanced
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
A two-wheel robot that counts pulses from a slot sensor on each wheel. A feedback loop holds a steady speed on any battery, and the pulse counts drive a square: four sides and four corners.
Arduino Uno R3 runs the sketch below. The circuit around it: Motor driver, L9110S, Robot car chassis, 2WD. pinMode sets A_IA as an output. analogWrite sends a PWM signal out on ib. delay waits 150 milliseconds. Serial.begin opens the serial port at 115200 baud. millis reads the time since power-up, so the sketch can time things without stopping.
Parts list
4 parts, plus the jumper wires. They retail for about $40; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Motor driver, L9110S
- 1 × Robot car chassis, 2WD
- 1 × Power, 6 V
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Arduino Uno R3 pin GND | Ground |
| Motor driver, L9110S pin BIB | Arduino Uno R3 pin 10 |
| Motor driver, L9110S pin BIA | Arduino Uno R3 pin 9 |
| Motor driver, L9110S pin AIB | Arduino Uno R3 pin 6 |
| Motor driver, L9110S pin AIA | Arduino Uno R3 pin 5 |
| Robot car chassis, 2WD pin ENCL | Arduino Uno R3 pin 3 |
| Robot car chassis, 2WD pin ENCR | Arduino Uno R3 pin 2 |
| Robot car chassis, 2WD pin VCC | Arduino Uno R3 pin 5V |
| Motor driver, L9110S pin GND | Ground |
| Motor driver, L9110S pin VCC | 6 V |
| Robot car chassis, 2WD pin R+ | Motor driver, L9110S pin OB1 |
| Robot car chassis, 2WD pin R- | Motor driver, L9110S pin OB2 |
| Robot car chassis, 2WD pin L+ | Motor driver, L9110S pin OA1 |
| Robot car chassis, 2WD pin L- | Motor driver, L9110S pin OA2 |
| Robot car chassis, 2WD pin GND | Ground |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Wheel-encoder square driver: closed-loop speed and distance by counting.
//
// Slot sensors on the chassis: left ENC on pin 3, right ENC on pin 2
// (VCC to 5V, GND to ground), 20 pulses for every turn of a wheel
// L9110 driver: IA pin 5, IB pin 6 (motor A, left) B-IA pin 9, B-IB pin 10 (right motor)
//
// # Counting
//
// Each slot that passes the sensor is one rising edge, and an interrupt adds
// one to a counter. With 65 mm wheels one turn is 204 mm, so one pulse is a bit
// more than 10 mm of floor. That is the whole of odometry: distance is pulses
// times 10.2 mm, and the two wheels' counts tell the robot how far it has gone
// and, from the difference, how far it has turned.
//
// # Feedback
//
// With no feedback, "PWM 150" gives a different speed on a fresh battery than
// on a flat one. This robot asks the wheel instead. Every 250 ms it counts the
// pulses each wheel made and compares them with the target. If a wheel is too
// slow its PWM goes up by a little; too fast, down. That is an integral
// controller, and each wheel has its own, so two motors that are not quite the
// same still run at the same speed.
//
// # The square
//
// Four sides of about 580 mm, each a count of pulses, and four corners. A
// corner is a spin on the spot: the left wheel back and the right wheel
// forward, until each has counted the pulses that a quarter turn of a 130 mm
// track takes, 10 of travel in all.
// The wheels coast a little after the PWM stops, so the sketch brakes by
// holding both inputs low, which this driver treats as a brake.
const int ENC_LEFT = 3;
const int ENC_RIGHT = 2;
const int A_IA = 5;
const int A_IB = 6;
const int B_IA = 9;
const int B_IB = 10;
const unsigned long WINDOW_MS = 250;
// Pulses per window the speed loop aims for: 32 a second, a bit over 300 mm/s.
const int TARGET = 8;
const int GAIN = 6;
// The robot is still rolling when the brake goes on, about 5 pulses' worth at
// this speed, so 52 counted is about 57 on the floor: 580 mm a side. A corner
// is 10 pulses of wheel travel, less the 2 or so it spins on after the brake.
const int SIDE_PULSES = 52;
const int CORNER_PULSES = 8;
const int SPIN_PWM = 150;
const int FIRST_GUESS = 130;
volatile unsigned int leftCount = 0;
volatile unsigned int rightCount = 0;
void onLeft() { leftCount++; }
void onRight() { rightCount++; }
int leftPwm = FIRST_GUESS;
int rightPwm = FIRST_GUESS;
unsigned int sideStartLeft = 0;
unsigned int sideStartRight = 0;
unsigned int lastLeft = 0;
unsigned int lastRight = 0;
unsigned long lastWindow = 0;
int sides = 0;
bool done = false;
void motor(int ia, int ib, int speed) {
if (speed >= 0) {
analogWrite(ib, 0);
analogWrite(ia, speed);
} else {
analogWrite(ia, 0);
analogWrite(ib, -speed);
}
}
void drive(int left, int right) {
motor(A_IA, A_IB, left);
motor(B_IA, B_IB, right);
}
int clampPwm(int v) {
if (v < 40) return 40;
if (v > 255) return 255;
return v;
}
void readCounts(unsigned int &l, unsigned int &r) {
noInterrupts();
l = leftCount;
r = rightCount;
interrupts();
}
// Spin on the spot until each wheel has counted `pulses`, then brake.
void corner(int pulses) {
unsigned int l0, r0, l, r;
readCounts(l0, r0);
drive(0, 0);
delay(150);
drive(-SPIN_PWM, SPIN_PWM);
unsigned long started = millis();
do {
readCounts(l, r);
} while (((int)(l - l0) < pulses || (int)(r - r0) < pulses) && millis() - started < 3000);
drive(0, 0);
delay(300);
}
void setup() {
pinMode(A_IA, OUTPUT);
pinMode(A_IB, OUTPUT);
pinMode(B_IA, OUTPUT);
pinMode(B_IB, OUTPUT);
pinMode(ENC_LEFT, INPUT);
pinMode(ENC_RIGHT, INPUT);
attachInterrupt(digitalPinToInterrupt(ENC_LEFT), onLeft, RISING);
attachInterrupt(digitalPinToInterrupt(ENC_RIGHT), onRight, RISING);
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: wheel-encoder square driver");
Serial.println("ready: driving a square, 4 sides of about 580 mm");
readCounts(sideStartLeft, sideStartRight);
lastLeft = sideStartLeft;
lastRight = sideStartRight;
lastWindow = millis();
drive(leftPwm, rightPwm);
}
void loop() {
if (done) return;
if (millis() - lastWindow >= WINDOW_MS) {
lastWindow += WINDOW_MS;
unsigned int l, r;
readCounts(l, r);
int leftSpeed = (int)(l - lastLeft);
int rightSpeed = (int)(r - lastRight);
lastLeft = l;
lastRight = r;
leftPwm = clampPwm(leftPwm + GAIN * (TARGET - leftSpeed));
rightPwm = clampPwm(rightPwm + GAIN * (TARGET - rightSpeed));
drive(leftPwm, rightPwm);
Serial.print("pulses ");
Serial.print(leftSpeed);
Serial.print(" ");
Serial.print(rightSpeed);
Serial.print(" pwm ");
Serial.print(leftPwm);
Serial.print(" ");
Serial.println(rightPwm);
}
unsigned int l, r;
readCounts(l, r);
if (((int)(l - sideStartLeft) + (int)(r - sideStartRight)) / 2 >= SIDE_PULSES) {
drive(0, 0);
sides++;
Serial.print("side ");
Serial.print(sides);
Serial.println(" done");
corner(CORNER_PULSES);
if (sides >= 4) {
Serial.println("square complete");
done = true;
return;
}
readCounts(sideStartLeft, sideStartRight);
lastLeft = sideStartLeft;
lastRight = sideStartRight;
lastWindow = millis();
leftPwm = FIRST_GUESS;
rightPwm = FIRST_GUESS;
drive(leftPwm, rightPwm);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup() {
pinMode(A_IA, OUTPUT);
pinMode(A_IB, OUTPUT);
pinMode(B_IA, OUTPUT);
pinMode(B_IB, OUTPUT);
pinMode(ENC_LEFT, INPUT);
pinMode(ENC_RIGHT, INPUT);
attachInterrupt(digitalPinToInterrupt(ENC_LEFT), onLeft, RISING);
attachInterrupt(digitalPinToInterrupt(ENC_RIGHT), onRight, RISING);
Serial.begin(115200);
Serial.println("Mokxi Arduino Uno: wheel-encoder square driver");
Serial.println("ready: driving a square, 4 sides of about 580 mm");
readCounts(sideStartLeft, sideStartRight);
lastLeft = sideStartLeft;
lastRight = sideStartRight;
lastWindow = millis();
drive(leftPwm, rightPwm);
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop() {
if (done) return;
if (millis() - lastWindow >= WINDOW_MS) {
lastWindow += WINDOW_MS;
unsigned int l, r;
readCounts(l, r);
int leftSpeed = (int)(l - lastLeft);
int rightSpeed = (int)(r - lastRight);
lastLeft = l;
lastRight = r;
leftPwm = clampPwm(leftPwm + GAIN * (TARGET - leftSpeed));
rightPwm = clampPwm(rightPwm + GAIN * (TARGET - rightSpeed));
drive(leftPwm, rightPwm);
Serial.print("pulses ");
Serial.print(leftSpeed);
Serial.print(" ");
Serial.print(rightSpeed);
Serial.print(" pwm ");
Serial.print(leftPwm);
Serial.print(" ");
Serial.println(rightPwm);
}
unsigned int l, r;
readCounts(l, r);
if (((int)(l - sideStartLeft) + (int)(r - sideStartRight)) / 2 >= SIDE_PULSES) {
drive(0, 0);
// ... 19 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- The motor does not turn or stops quickly when both inputs are the same. Motor driver
Each output follows its input. Set one input high and the other low to turn, swap them to reverse. Equal inputs brake the motor rather than coast. Use PWM on one input for speed. - The driver gets hot or the board resets. Motor driver
Power the motors from a separate supply of about 2.5 to 12 V, within the chip's rating, and share ground with the controller. Small motors only: stalled or large motors exceed its roughly 800 mA limit. - The car drives in circles or goes backwards. Robot car chassis
Motor polarity decides the direction. Swap the two wires on a motor that spins the wrong way. For a straight run, the motors are never identical, so trim the speed on one side. - The motors do not run, or the board resets when they start. Robot car chassis
Motors draw far more current than a pin can give. Use a motor driver such as an L298N or L293D. Power the motors from a separate battery pack and join its ground with the Arduino ground. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.