Session 4: Navigating with Light – Advanced Line Following
Duration: 50-60 minutes
Equipment Required:
- M3D Robotics kit with line sensor attached
- Laptops with Arduino IDE
- USB/OTG cables for connecting the robot to the laptop
- Projector for live coding demonstrations
- Black tape and white surface for creating paths
Learning Objectives:
- Understand how to use IR sensor readings to follow a line with varying brightness levels.
- Implement conditional logic to make the robot adapt based on sensor data.
- Apply comparison logic to adjust the robot’s direction when it deviates from the path.
1. Introduction to Brightness Detection and Line Following (10 minutes)
Activity:
Begin with an interactive discussion on brightness detection and introduce the line sensor in detail. Explain how our robot uses its sensors to differentiate between light and dark areas and follow a path based on contrast. Use real-life examples and hands-on demonstrations to make this concept engaging and relatable.
Interactive Demonstration:
- Start with a Visual Comparison:
- Show two contrasting surfaces, like a white paper with a black strip. Ask students to describe the differences they see.
“How does your eye see the difference between these two areas?”
- Guide them to the idea of contrast — the black line against a white background stands out because it absorbs more light than it reflects, making it easy for our eyes (and the robot’s sensors) to distinguish.
- Introducing the Line Sensor:
- Explain that the robot has two infrared (IR) sensors, which are small devices that measure brightness by sensing light reflections. The two IR sensors — left and right — allow the robot to detect contrasts, such as dark and light areas, as it navigates.
“Think of these sensors like your eyes looking down at your feet on a path. If you step to the right of the line, you notice and adjust. The robot does the same by checking the readings from its left and right IR sensors.”
- Demonstrate Sensor Connection and Use:
- Show students how to connect the line sensor to the robot step-by-step. Make sure each group connects their sensors properly.
- Once connected, display how the sensor reads values on a sample black-and-white track using the commands
line.onTheLine()andline.notOnLine()to see whether the robot is on or off the line.
Example Activity:
After connecting the line sensors, have a marked black line on a white surface and let students observe the robot’s readings as it’s positioned over different sections of the line.
- You may say:
“Watch what happens to the readings as we move the robot on and off the line. If both sensors detect the line, it means the robot is right over the line. But if one sensor detects more white than black, what might that mean for the robot?”
Encourage students to describe what they observe. Ask them to predict what would happen if the robot only detected the line on one side. This will help them understand how the robot adjusts its movement to stay aligned with the line.
Teacher’s Role:
Guide students through connecting the line sensor, understanding sensor readings, and observing how the robot distinguishes between light and dark surfaces. Emphasize the idea that the robot uses brightness detection to keep itself aligned, with both sensors playing a crucial role in maintaining direction along the path.
2. Introducing Basic Line Detection Commands (10 minutes)
Activity:
Guide students in using basic line detection commands to check if the robot is on or off the line.
Commands to Introduce:
line.onTheLine();– checks if both IR sensors detect the line.line.notOnLine();– checks if neither sensor detects the line.
Example Code:
#include <M3DGo.h>
void setup() {
go.begin();
}
void loop() {
if (line.onTheLine()) {
go.forward(50); // Move forward if on the line
display.text("On the Line"); // Display message
} else {
go.stop(); // Stop if not on the line
display.text("Off the Line");
}
go.delay(500);
}
Explanation:
You may say:
“Here, we’re telling the robot to move forward if both sensors detect the line, but stop when it senses it’s off the line. This keeps the robot from going too far off track.”
Teacher’s Role:
Guide students through the basic line-following code. Explain how each condition helps the robot determine its position relative to the line.
3. Adjusting Direction Based on Sensor Readings (10 minutes)
Activity:
Introduce how the robot can compare the readings from the left and right sensors to adjust its path if it starts to veer off.
Instructions:
Explain that by comparing the two sensor values, the robot can decide if it needs to turn to the left or right to stay on the line.
Example Code for Directional Adjustment:
#include <M3DGo.h>
void setup() {
go.begin();
}
void loop() {
if (line.onTheLine()) {
go.forward(50); // Move forward if on the line
display.text("Following Line");
} else if (line.getLeft() > line.getRight()) { // Left sensor detects more white
go.spinClockwise(30); // Turn right
display.text("Adjusting Right");
} else { // Right sensor detects more white
go.spinCounterclockwise(30); // Turn left
display.text("Adjusting Left");
}
go.delay(500);
}
Explanation:
You may say:
“In this code, the robot checks if it’s starting to slip off the line by comparing the readings from its left and right sensors. If the left sensor sees more white, it turns right to get back on track, and if the right sensor does, it turns left.”
Teacher’s Role:
Demonstrate the code and help students understand how the sensor readings guide the robot’s adjustments.
4. Hands-On Challenges (25 minutes)
Encourage students to apply the line-following logic with sensor readings in various challenges.
Challenge 1: Basic Line Following
- Task: Program the robot to follow a straight line, stopping if it no longer detects the line.
Example Code:
void setup() {
go.begin();
}
void loop() {
if (line.onTheLine()) {
go.forward(50);
display.text("Following Line");
} else {
go.stop();
display.text("Line Lost");
}
}
Challenge 2: Path Recovery with Turns
- Task: Adjust the robot to make gradual turns based on sensor readings, helping it return to the line if it veers off.
Example Code:
void setup() {
go.begin();
}
void loop() {
if (line.onTheLine()) {
go.forward(50);
display.text("On Track");
} else if (line.getLeft() < line.getRight()) {
go.spinCounterClockwise(20); // Gradual left turn
display.text("Turning Left");
} else {
go.spinClockwise(20); // Gradual right turn
display.text("Turning Right");
}
}
Challenge 3: Navigate a Curved Path
- Task: Program the robot to navigate a curved path using the sensor readings, adjusting continuously to follow the curve.
Example Code:
void setup() {
go.begin();
}
void loop() {
if (line.onTheLine()) {
go.forward(50);
display.text("Following Curve");
} else if (line.getLeft() < line.getRight()) {
go.spinCounterClockwise(15); // Smooth left adjustment
display.text("Adjusting Left");
} else {
go.spinClockwise(15); // Smooth right adjustment
display.text("Adjusting Right");
}
}
Teacher’s Role:
Assist students in coding and testing each challenge. Encourage them to experiment by adjusting speeds and sensor thresholds for smoother performance.
5. Reflection and Wrap-Up (5 minutes)
Reflection Questions:
- You may ask:
“How did the robot know when to turn left or right?”
“What happens when you adjust the speed or turn angles in your code?”
“How could we make the robot follow the line even better?”
Teacher’s Role:
Guide a wrap-up discussion, reinforcing how sensor data and conditionals can be used to enhance navigation along a line, even on a curved or irregular path.
Learning Outcome:
By the end of this session, students will understand how to use IR sensor data to follow a line with varying brightness levels. They will learn how conditionals and sensor comparisons help in adjusting the robot’s path, developing a foundation for adaptive navigation.