Skip to content

Class 2 · Feel the walls

The car drives, but it's blind: it goes wherever you point it until it hits something. Today it gets three distance sensors, an HC-SR04 in front and two Sharp IR sensors on the sides, and learns to steer itself down the middle of a corridor. This is the core of every micromouse robot.

By the end of class

The car drives down a foam-board corridor without touching either wall and stops before the end wall.

New parts today

Part What it's for
HC-SR04 (from your kit) Distance to the wall in front
Sharp GP2Y0A41SK0F IR sensors, ×2 Distance to the left and right walls, 4–30 cm
10 µF capacitors, ×2 Smooth each IR sensor's power
Foam-board corridor, about 1 m long The test track

Build it

  1. Mount the HC-SR04 at the front, facing forward. Wire it exactly as in the dispenser: trig to pin 5, echo to pin 6.
  2. Mount the two Sharp sensors facing left and right, near the middle of the car.

    The 4 cm blind spot

    The Sharp sensors read 4–30 cm. Closer than 4 cm, the reading goes the wrong way: the sensor reports "far" when the wall is almost touching. Mount each sensor far enough inward that, even with the car's side touching the wall, the sensor is at least 5 cm from it.

  3. Find each sensor's three wires. The cable colors vary between sellers, so check the pin labels in your sensor's datasheet or on the board, and test with the multimeter: with power on, the output wire reads somewhere between about 0.3 V and 3 V, and changes as you move your hand.

  4. Wire them:
From To
HC-SR04 VCC / GND Uno 5V / GND
HC-SR04 Trig Uno pin 5
HC-SR04 Echo Uno pin 6
Left Sharp Vcc / GND Uno 5V / GND
Left Sharp output (Vo) Uno A0
Right Sharp Vcc / GND Uno 5V / GND
Right Sharp output (Vo) Uno A1
10 µF capacitor (×2) Across Vcc and GND, as close to each Sharp sensor as you can

The capacitor's stripe (−) goes to GND. The Sharp sensors pull their current in short bursts, and the capacitor supplies those bursts so the 5 V line doesn't dip and add noise to every other reading. Sharp's datasheet recommends 10 µF or more.

The corridor. Build two foam-board walls about 1 m long, a car-width plus 10 cm apart, and a third wall across the far end.

Mission 1 · A calibration table

The Sharp sensor's output is not a straight line: from 4 to 10 cm the reading changes a lot, and from 20 to 30 cm it barely changes. Rather than trusting a formula from the internet, you'll measure your own sensors.

  1. Print analogRead(A0) and analogRead(A1) to the Serial Monitor several times a second. Average 5 readings each time: single readings jump around.
  2. Tape a ruler to the table. Hold a piece of foam board (the same material as your walls) at 4, 5, 6, 8, 10, 12, 15, 20, 25 and 30 cm from the front of each sensor and write down the reading.
  3. Put both tables in your build log and make a quick graph of reading vs. distance.

Done when

You have a table for each side sensor, 10 distances each, and a graph that shows the curve.

Tool: a lookup table with arrays

An array is a numbered list of values with one name. raw[0] is the first value, raw[1] the second, and so on. A for loop counts through them.

Two arrays side by side make a table, and you can interpolate: if a reading falls between two rows, draw a straight line between them. Example: a thermistor whose readings you calibrated against a thermometer.

const int N = 5;
const int raw[N]  = {300, 420, 520, 610, 700};  // readings, smallest first
const int degC[N] = { 10,  18,  25,  32,  40};  // what each one means

float rawToDegC(int r) {
  if (r <= raw[0])     return degC[0];      // off the bottom of the table
  if (r >= raw[N - 1]) return degC[N - 1];  // off the top
  for (int i = 0; i < N - 1; i++) {
    if (r <= raw[i + 1]) {
      // r is between row i and row i+1: draw a straight line between them
      float fraction = (float)(r - raw[i]) / (raw[i + 1] - raw[i]);
      return degC[i] + fraction * (degC[i + 1] - degC[i]);
    }
  }
  return degC[N - 1];
}

For a reading of 470, the loop stops at i = 1 (between 420 and 520), fraction is 0.5, and the answer is halfway between 18 and 25: 21.5 °C.

The Sharp sensor's reading goes down as distance goes up. Keep the raw array smallest-first, so your distances will be largest-first.

Mission 2 · Distances in centimeters

Write float leftCm() and float rightCm() from your two tables, and use the dispenser's readDistanceCm() for the front. Print all three on one line.

Done when

With the car in the corridor, moving it sideways by hand changes left and right in opposite directions, and each reads within about 1 cm of a ruler between 5 and 20 cm.

Tool: proportional control

To steer toward a target, measure the error (how far you are from where you want to be) and correct by an amount proportional to it: a small error gets a small correction, a big one a big correction. The constant that sets "how much" is called the gain, KP.

Example: two photoresistors on the table, one left and one right, and a servo pointer between them that leans toward the brighter side. When both see the same light, the error is 0 and the pointer stands straight up at 90°. The bigger the difference, the further it leans.

#include <Arduino.h>
#include <Servo.h>       // fine here; this is not the car

Servo pointer;
const float KP = 0.2;

void setup() {
  pointer.attach(9);
}

void loop() {
  int left  = analogRead(A2);    // photoresistor dividers
  int right = analogRead(A3);
  int error = left - right;      // + means brighter on the left

  int angle = 90 + KP * error;   // straight up, plus a correction
  pointer.write(constrain(angle, 0, 180));
  delay(20);
}

Too small a KP and the pointer barely moves. On a car, which turns and then sees a new error, too big a KP makes it overshoot and weave from wall to wall. Tuning means finding the middle.

Mission 3 · Down the middle

Add a corridor mode. Each time through loop():

  • The error is leftCm() - rightCm(). Zero means centered.
  • Drive both wheels at a base speed, plus or minus a correction of KP * error. Work out which wheel gets the plus: if the car is closer to the right wall, which way should it turn?
  • If the front distance is under 15 cm, stop.

Start a new command for it, C 150 (corridor mode at speed 150), so the Pi can use it later. S still stops everything.

Start with a small KP and raise it until the car wobbles, then back off. Write down the values you try.

Done when

Started at an angle near one wall, the car straightens out, drives the whole corridor without touching either wall, and stops before the end wall. Three runs in a row. Take a video.

If you finish early

  • If one wall is missing (an opening on the left), the IR reading goes off the end of your table. Make the car hold the distance to the wall it still has.
  • Add a ? command that replies with all three distances on one line, like D 42 9.5 10.2. The Pi will use it in Class 3.
  • Try higher speeds. Does the best KP change?

Sign-off with Dr. Alvear

  • Calibration tables and graph in the build log
  • Left, right and front distances match a ruler
  • C command drives the corridor without touching, three runs in a row
  • KP values tried, and the one you chose
  • Build log updated (photo + two sentences)