For a teacher with a class? The classroom edition has the full plan, slides and standards.

Homeschooling PDF

Family edition

Robotics: Follow a Line with One Sensor

Ages 10-14 ยท about 45 minutes, in one sitting

Your child runs a simulated robot that follows a black line using one light sensor, watches the sensor's number change between the line and the white mat, and finds the simple rule in the program that steers it. Then they predict what a gentler turn will do, test it, and test more values to find the one that works best.

What you need

Before you start: try it once yourself

Run the robot and make the lesson's change yourself once before you start, about ten minutes.

  1. Open the lesson. Go to mytekdev.com/try/robotics?lesson=follow-a-line-with-one-sensor&ref=fam-robotics-1. The app opens on the Simulator. Press Run, the play button at the top left. The robot follows the black line to the far end of the mat and stops.
  2. Watch the sensor. Press reset, the circle arrow next to Run. Open the sensor readings with the sensors button at the top right (it looks like a page of lines, just left of the camera button), and press Run. Under in1: Color Sensor, Intensity (%) drops to the teens over the line and rises to about 100 over the mat.
  3. Read the program. Open Blocks: set turn to 40, then repeat while gps y is less than 90, with an if-else inside it that compares color_sensor reflected light intensity to 58.
  4. Make it fail. Change 40 to 10. Go back to Simulator, press reset, then Run. The robot runs off the mat at the first curve. Set it back to 40.
  5. Try one class value. Try 80 the same way, so you have seen one of the values your child will test.
Two views of the same curve. On the left, under the block set turn to 40, the robot sits on the black line partway through the curve. On the right, under set turn to 10, the robot has run off the left edge of the mat at the first curve.

Turn 40 reaches the end, circling back to the line at some curves. Turn 10 is too gentle and runs off the mat at the first one.

Run and reset are only on the Simulator tab, so after every change in Blocks, you have to switch back. Run starts from wherever the robot is now, so a robot that was not reset starts at the far end or off the mat. Typing in the Python tab brings up a warning that Python changes cannot be converted back into blocks; after that, Run uses the Python and ignores changes to the blocks.

The ideas, in plain words

The robot has one light sensor under its front, pointing down. It shines on the mat and measures how much light comes back: black reflects little, so the reading is low (about 16), and white reflects a lot (about 100). The program picks a cut-off between them, 58. Below it counts as the line, and above it counts as the mat. That cut-off is called a threshold.

One sensor cannot tell which side of the line the robot is on, only whether it is over dark or light. So the program follows the edge of the line: over dark it steers one way, over light it steers the other way, and it checks again every few milliseconds. The robot never drives straight. It zigzags along the edge, correcting each time it crosses. The turn value is how hard each correction is. Too gentle, and the robot cannot keep up with a bend. Too hard, and the robot spends its time turning instead of driving: at 80 it takes about two minutes to reach the end, and at 100 the wheels turn opposite ways and it only spins in place.

How often the robot checks matters too. In this simulator, the robot reads its sensor once each time the screen is drawn, so a slower computer checks less often, and the robot drives further between corrections. On this line the difference is small: in our tests, each turn value in this plan gave the same result on fast and slow computers, and only the times changed. Real robots have the same problem, which is why engineers test a program on more than one machine before they trust a number.

Your child might askA good answer
Why does it wiggle instead of driving straight?It only knows dark or light. Each time it checks, it turns toward the other side, so it keeps crossing the edge of the line.
Why 58?It sits between the line's reading, about 16, and the mat's, about 100. Readings in between happen when the sensor is half over the edge.
How does it know when to stop?The loop repeats while the GPS y is less than 90. 90 is just short of the far end of the mat, so the loop ends there and the robot stops.
Why did my result not match the one in this plan?First check that you both pressed reset before Run. Then check that neither of you typed in the Python tab, which makes Run ignore the blocks. In our tests, every value in the table gave the same result on fast and slow computers.

Do it together

  1. Open mytekdev.com/try/robotics?lesson=follow-a-line-with-one-sensor&ref=fam-robotics-1 together. The app opens on the Simulator with a robot at the bottom of a white mat with a curvy black line. A short guide runs along the bottom; follow it together. It has your child press Run, open the sensor readings, and then look in Blocks. Then ask: the robot cannot see the line ahead of it, so how does it stay on the line?

    You'll know it worked when: The robot follows the line to the far end of the mat and stops.

  2. Go back to Simulator, press reset (the circle arrow next to Run), make sure the sensor readings are open (the sensors button at the top right, which looks like a page of lines), and press Run. Watch Intensity (%) under in1: Color Sensor.

    You'll know it worked when: The number drops to the teens over the black line and rises to about 100 over the white mat.

  3. Open Blocks and read the program together from the top: set turn to 40; repeat while gps y is less than 90, which is near the far end of the mat; if color_sensor reflected light intensity is less than 58, move steering with direction turn, else the same block with minus turn; then sleep for 10 milliseconds. The robot only ever knows dark or light, so it zigzags along the edge of the line.

    You'll know it worked when: Your child can say what the robot does when the reading is below 58 and when it is above.

  4. Predict, then test. Change set turn to 40 to 10. Before Run, your child writes or says what will happen. Then go to Simulator, press reset, then Run. Set it back to 40 afterward.

    If you get stuck: Run and reset are only on the Simulator tab, so after every change in Blocks, switch back. Run starts from wherever the robot is now, so always press reset first.

    You'll know it worked when: The robot runs off the mat at the first curve: its turns are too gentle to follow the bend.

  5. Test more values: 20, 30, 50, 60, 70 and 80, one at a time. For each, predict first, then reset and Run, and let it go until the robot stops at the end or leaves the mat (up to about two minutes). Write down one of three results: reached the end, left the line and came back, or ran off the mat.

    If you get stuck: Typing in the Python tab brings up a warning, and after that Run ignores the blocks. Keep changes in Blocks.

    You'll know it worked when: A filled-in table, with a prediction written before every run.

  6. Finish with one question: which turn value would you choose, and what is your evidence? Then: on a slower computer, the robot checks its sensor less often. How might that change your answer?

    You'll know it worked when: Your child picks a value and points to the results that support it, and says that a robot that checks less often drives further between corrections.

Talk about it

Ask these while you work together. A child who has understood the lesson can answer them without help:

The last one is the real test. A child who checks how a test was run (was the robot reset, were the blocks changed) before blaming the program is thinking like a robotics engineer.

For younger and older kids

Younger: For ages 8 and 9, do the turn 10 test side by side, test two more values together, and save the rest for another day.

Older: For teens, work in the Python tab, test the cut-off (try 90 and 30) as well as the turn value, and explain in a few sentences why a robot that checks less often needs gentler turns.

Keep going

Bring it into other subjects

For your records

Ready to copy into an hour log, a portfolio or a transcript.

LessonRobotics: Follow a Line with One Sensor
SubjectsComputer science, Engineering
Time45 minutes
What we didRan a simulated line-following robot, read its light sensor's values over the line and the mat, and explained the if-else rule inside a loop that steers it. Predicted and tested how changing the turn value changes the robot's path, recorded each result, and chose a value from the evidence.
Skills practicedsensors and thresholds; conditionals inside a loop; predicting before running; fair testing and recording results

Robotics: Follow a Line with One Sensor

Activity sheet (optional)

Name: ______________________________ Date: ______________

Open the app: scan the QR code, or go to mytekdev.com/try/robotics?lesson=follow-a-line-with-one-sensor

Essential question: How can a robot with one sensor stay on a line it cannot see ahead of?

  1. Run it. Press Run, the play button. Watch the robot follow the line.
  2. Read the sensor. Open the sensor readings. Intensity over the black line: ______ over the white mat: ______
  3. Reset first. Before every Run, press reset, the circle arrow, so the robot starts over.
  4. Predict, then test. For each turn value, write your prediction first. Then reset, Run, and write what happened: reached the end, left the line and came back, or ran off the mat.
Turn valueI predictWhat happened
10
My first value: ____
My second value: ____

1. Which turn value would you choose? What results from your table support it?

2. Why does the robot zigzag instead of driving straight?

Quick check

Name: ______________________________

1. The light sensor reads 20. Where is it most likely to be? (A) over the black line, (B) over the white mat, (C) you cannot tell.

2. The program says: if intensity is less than 58, steer one way, else steer the other way. Why does the robot zigzag instead of driving straight? (A) the line is crooked, (B) it only knows dark or light, so it keeps turning back across the edge, (C) the motors are uneven.

3. With turn set to 10, the robot ran off the mat at the first curve. What would you try next, and why?

Answers

Measured in the app: the color sensor's intensity reads about 16 over the black line and about 100 over the white mat. With turn 10 the robot runs off the mat at the first curve; with turn 40 it reaches the end of the line.

QuestionAnswerWhy
1A20 is close to the line's reading, about 16, and far below the cut-off of 58.
2BOne sensor only tells dark from light. Each check turns the robot back across the edge, so it zigzags along it.
3A bigger turn value, such as 40, because 10 turns too gently to follow the bendA good answer names a larger value and a reason about how hard the robot turns.

If your state or umbrella school asks, this lesson covers CSTA 2017 1B-AP-15, CSTA 2017 2-AP-17, NGSS 3-5-ETS1-3.

The Robotics simulator is built on GEARS (GearsBot) by Cort Wee, open source under the GNU General Public License 3.0. Its blocks are Blockly, its 3D view is Babylon.js and its Python runs in Skulpt.