Reflectance Sensor Array

Information
Step 1
Reflectance Sensor Array

We'll be using the QTR-3RC from pololu to create a line following robot.

In this lesson we will be looking at the reference document to figure out how it this sensor works, and then you will be building a rudimentary line following robot

Step 2
Setting up the sensor

Take a look at the specifications on the product page

These are the most useful bits of information to pay attention to when mounting your sensor

Information
Step 3
How does the sensor array work?

There is a nice in depth description of how each individual sensor works here on the product page for a single sensor.

The gist of it is that it works like this:

1. A capacitor is attached to ground through a phototransistor- a device which lets current flow when it receives light.

Information
Step 4
2. The sensor is powered by the Arduino's 5V pin.

Putting 5V into Vcc charges the capacitor, the the phototransistor blocks the current flow to ground so the charge stays inside the capacitor

Information
Step 5
3. The Arduino sets the corresponding digital pin HIGH, which discharges the capacitor.
Information
Step 6
4. The IR receiver measures ambient IR light reflected off the surface below the sensor.

The phototransistor lets some current through. The more light it detects, the more current it lets through.

Information
Step 7
5. The HIGH voltage attracts electrons from GND, which can now flow through the phototransistor- charging the capacitor
Information
Step 8
6. The Arduino records how long it took the capacitor to fully charge and the circuit is back to where we started

The longer it took to charge the capcitor, the less light the phototransistor received.

Since darker surfaces reflect less light than lighter surfaces, we can use this value to decide if the sensor saw the tape or not

Planning
Step 9
Sensor instructions

The reference manual outlines the steps which need to happen for an accurate sensor reading.

To complete this lesson you will need to create a function which performs these steps in order:

Note: digital I/O lines refer to digital pins (Input/Output)
μs stands for microseconds

Step 10
Setting up variables

Your code will need some way to record the data your sensor is reading.

For this you should use an array, otherwise you'll need to give each sensor it's own variable.

(using arrays will make your code much simpler in the coming lessons)

For help using arrays, check out these exercises Introduction to Arrays .

Step 11
It's also a good idea to define in your program which pins you will be using for your sensor

Again you can either use an array or you can make three more variables

Challenge
Step 12
Create a function which reads the sensor values

Now is a good time to reread the information step images which explain how the reflectance sensor physically works.

Next you should look at the sensor instructions. There are four steps to take a sensor reading, and your function should perform them for each sensor

Additional hints are available in the following steps

Information
Step 13
Printing your sensor values to the Serial Monitor

Something like this where the three sensor values are all on the same line.

   (left value)   |   (mid value)   |   (right value)
   (left value)   |   (mid value)   |   (right value)
   (left value)   |   (mid value)   |   (right value)
   ...

println (print line) is the same as print but it makes a new line afterwards

Planning
Step 14
Using a while loop as a timer

Part of reading a sensor value is timing how long it takes for the capacitor to discharge.

Imagine using a stop watch to time this.

  • You would start when the capacitor was fully discharged
  • You would stop as soon as the capacitor fully charged
  • Then the stop watch would tell you the elapsed time

What we are looking for then is a way to measure elapsed time of the capacitor discharging.

Planning
Step 15
There's no way for us to use a stopwatch in our program. We'll be using something more like a clock

Imagine looking at a clock on your wall.

How would you use this to record the length of an event, say, getting ready for school?

If you woke up at 7am and were ready at 7:45am, what formula would you use to calculate how long it took you to get ready?

Using this formula: currentTime - startTime we can calculate elapsedTime.

Step 16
Recording the time

Create two variables:
startTime
elapsedTime
These should be of type unsigned long

Step 17
Getting currentTime

The current time can be found using the built-in function micros() which tells us how long the program has been running since the Arduino began.

Think of the value of micros() as the clock on the wall.

Step 18
Set the startTime right when the capacitor is fully discharged
Step 19
Now for the while loop

You should have a while loop that continuously checks the value of each sensorPin.

The while loop should repeat until either:

  • sensorPin voltage tells us the capacitor has charged

OR

  • the elapsed time is greater than 2.5 milliseconds

HINT: What value will you get with digitalRead() from the sensorPin once the capacitor has fully discharged?

Try one of these lessons next:

Reflectance Sensor Array Info

Account

MVCode

Created By

jamie

Course:

Feedback Control Systems: Line follower

Access Level

public

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach