Let's Workshop: Keyes White/Black Line Sensor Module

Posted by Sebastian Karam on

Here is a quick introduction to using the Keyes White/Black line sensor module. Hopefully it will provide you with the confidence to object light reflection into your project. Providing an ideal launch point for a robot.

This example will demonstrate the use of an Arduino UNO in monitoring an analog pin over the range of light levels that a IR detector responds to as it moves from a white reflective surface to a black absorbent one.

Components

Wiring

Wire the one of the two boards to the Arduino as can be seen in the images below, taking care to match the pin numbers.

Coding

The code consists of a definition, setup and loop. First the A0 pin is assigned followed by a varaiable to store the value read. This allows easier use later in your program. A setup informs the system that the pin is an input is an input and then lauches the serial connection. Next we enter the loop, where the value on the pin is read and stored in the variable declared earlier. Following that we send it to the serial monitor so that the value can be read on the screen.

Load the code below into the Arduino IDE and upload it to your board.

/*
  A simple program designed to setup and demonstrate the Keyes White/Black
  Line Sensor Module.
 
  The program monitors a connected analog pin and outputs the value to the
  serial monitor.
  
  modified 14th June 2019
  by Sebastian Karam - Flux Workshop
*/
int analogApin = 0; // define OUT signal pin
int analogA; // define variable to store value read from pin
  
void setup() {
  pinMode(analogApin, INPUT); // set the OUT signal pin as an input
  Serial.begin(9600); // launch the serial monitor
  Serial.println("Flux Workshop Example");
}
 
void loop() {
  analogA = analogRead(analogApin);  // read the voltage level on the A0
  Serial.println((String)"Line Detection: " + analogA); // send the result to the serial monitor
  delay(200); // pause for a moment before repeating
}

Running

With the board loaded with the program and all the connections made the serial monitor will produce an output like the one seen below. In this case the sensor is moved from looking at white paper to a large black spot and back. The value gets higher as less of the IR light is reflected back onto the receiver.

What to try next?

  • Use the output to monitor the proximity to a wall or surface.
  • Using two, it is possible to begin a line following robot.

Share this post



← Older Post Newer Post →


Leave a comment