Building and testing micromouse sensors

To build a micromouse I need sensors that see the maze and motors that move the robot through the maze, connected to a brain that I can teach. All of that is powered by a battery. In this post I’m going to connect the micromouse sensors and graph their output.

Micromouse sensors, in theory

The robot will use the sensors to understand the maze, and then use the understanding to steer through the maze.  I’ve chosen to use four distance sensors: Two will point sideways, just ahead of the wheels, to help steer straight down hallways; Two will point forward-left and forward-right to help drive diagonally and to watch for walls ahead of the robot.  I want sensors that are easy to connect with the Arduino brain I like to teach.  Given that the rooms in a micromouse track are 16x16cm, I guess sensors with a 10cm range will do the job.

Attempt 1

In my first attempt I bought some Sharp GP2Y0D810Z0F Digital Distance Sensor.  This sensor shines infrared light and measures the reflected light to detect things nearby.  The sensors have three wires: power in, ground out, and signal out.  If I hook signal out to Arduino analog in, the Arduino should tell me what the sensor is doing.

I didn’t read carefully that the name of the sensor said digital. That means the sensors will only say “something is near!” when what I need is a sensor that says “something is this far away!”  I doubt I can steer without knowing how far away are the obstacles.  So… close but no cigar.

Attempt 2

In my second attempt I bought some Sharp GP2Y0A51SK0F Analog Distance Sensors.  It also measures the reflected light off things nearby, but it also tells me how much is reflected.  Things that are close should reflect more light, giving a bigger sensor value.

https://www.instagram.com/p/BFW7ZUGofJ

Micromouse sensor testing

I want to be sure the micromouse sensors are working. The quickest way is an Arduino sketch – a lesson for the robot brain – that says “tell me your sensor information non-stop forever.”
It looks a bit like this*:

// read four analog distance sensors and send the values to serial.
// Dan Royer ([email protected])
// 2016-05-14

void setup() {
  Serial.begin(57600);
}


void loop() {
  Serial.print(analogRead(2));  Serial.print("\t");
  Serial.print(analogRead(3));  Serial.print("\t");
  Serial.print(analogRead(4));  Serial.print("\t");
  Serial.print(analogRead(5));  Serial.print("\n");
  delay(5);
}

Now when I open the serial window in Arduino the connected robot starts dumping a long list of numbers. When I wave my hand in front of the sensors, the numbers seem to move up and down. I’m feeling pretty good about this, but I want 100% certainty.

Gaining insight

I think in pictures, which means I need to draw the sensor data so it makes the most sense to me. I found https://www.arduino.cc/en/Tutorial/Graph, which uses Processing to make a single graph of one Arduino sensor. I modified it to handle all four sensors streams coming from the robot. This code automatically connects to the last port found by Processing.*

// read four analog values from serial (range 0-1023) and graph those values over time.
// Dan Royer ([email protected])
// 2016-05-14
// based on https://www.arduino.cc/en/Tutorial/Graph by David A. Mellis and Tom Igoe.
import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph
float inByte0 = 0;
float inByte1 = 0;
float inByte2 = 0;
float inByte3 = 0;

float inByte0old = 0;
float inByte1old = 0;
float inByte2old = 0;
float inByte3old = 0;

void setup () {
  // set the window size:
  size(800, 600);

  // List all the available serial ports
  String [] ports = Serial.list();
  // if using Processing 2.1 or later, use Serial.printArray()
  println(ports);

  // I know that the last port in the serial list on my mac is usually the robot.
  // Open whatever port is the one you're using.
  myPort = new Serial(this, ports[ports.length-1], 57600);

  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');

  // set inital background:
  background(0);
}

void draw () {
  // draw the line:
  stroke(127, 34, 255);
  stroke(255,   0,   0);  line(xPos-1, height-inByte0old, xPos, height - inByte0);
  stroke(  0, 255,   0);  line(xPos-1, height-inByte1old, xPos, height - inByte1);
  stroke(  0,   0, 255);  line(xPos-1, height-inByte2old, xPos, height - inByte2);
  stroke(255, 255, 255);  line(xPos-1, height-inByte3old, xPos, height - inByte3);

  // at the edge of the screen, go back to the beginning:
  if (xPos >= width) {
    xPos = 0;
    background(0);
  } else {
    // increment the horizontal position:
    xPos++;
  }
  inByte0old = inByte0;
  inByte1old = inByte1;
  inByte2old = inByte2;
  inByte3old = inByte3;
}


void serialEvent (Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    String[] tok = splitTokens(inString);
    if (tok.length==4) {
      inByte0 = float(tok[0]);
      inByte1 = float(tok[1]);
      inByte2 = float(tok[2]);
      inByte3 = float(tok[3]);
    } else {
      inByte0=0;
      inByte1=0;
      inByte2=0;
      inByte3=0;
    }
    inByte0 = map(inByte0, 0, 1023, 0, height);
    inByte1 = map(inByte1, 0, 1023, 0, height);
    inByte2 = map(inByte2, 0, 1023, 0, height);
    inByte3 = map(inByte3, 0, 1023, 0, height);
  }
}

Now when I move my hand in front of the sensors I get a graph like this.

graph of micromouse sensors' readings

Discoveries

  • When I put my hand at 1cm or less from the micromouse sensors the graph did not climb more than halfway.  That means the analogRead() in Arduino is never returning a value more than ~50% of it’s possible max.   Surprise!
  • When my hand is outside of the sensor’s rated 10cm range (but still in view), the Arduino analogRead() is still returning a number about 30% of max.  How unexpected!
  • The red channel (sensor 1) is picking up a regular “tick”. More testing reveals that light from the other sensors is causing interference. Pointing them all straight ahead like I have on this test is not a good idea on the final robot.
  • The values when the sensor sees something more than 10cm away is > 30%.  The values when the sensor sees nothing are < 30%.  This might be good to know later.

Next

Next post I’ll add the motors.

*

The code samples in this post might have been updated since this post was written. Please visit https://github.com/MarginallyClever/micromouseSketches for the latest version.

Only registered users can comment.

  1. It is better to use digital compass? Or IMU? I need your response fr my fyp project. Tq so much for the tips, i really appreciate this : D