Location:HOME > Technology > content
Technology
Arduino Uno Code for an 8-Array IR Sensor in Line Follower Robot
Arduino Uno Code for an 8-Array IR Sensor in Line Follower Robot
To cr
Arduino Uno Code for an 8-Array IR Sensor in Line Follower Robot
To create a line follower robot using an Arduino Uno with an 8-array IR sensor, you'll need to read the sensor values and control the motors based on those readings. This article provides a simple example of how to achieve this.
Components Needed
Arduino Uno 8-array IR sensor or multiple IR sensors Motor driver like L298N or L293D DC motors Chassis for the robot Power supply batteries WiringConnecting the Components
Make sure to connect the 8-array IR sensor outputs to the Arduino's digital pins. For example:
IR sensor outputs to pins 2 to 9 on the Arduino. Motor driver inputs to Arduino pins for example pins 10, 11 for the left motor, and 12, 13 for the right motor.Sample Code
Here's a basic code example for a line-following robot using an 8-array IR sensor:
// Define pin numbers const int sensorPins[8] {2, 3, 4, 5, 6, 7, 8, 9}; // IR sensor pins const int motorLeftForward 10; // Left motor forward const int motorLeftBackward 11; // Left motor backward const int motorRightForward 12; // Right motor forward const int motorRightBackward 13; // Right motor backward void setup() { // Initialize motor pins as outputs pinMode(motorLeftForward, OUTPUT); pinMode(motorLeftBackward, OUTPUT); pinMode(motorRightForward, OUTPUT); pinMode(motorRightBackward, OUTPUT); // Initialize sensor pins as inputs for (int i 0; i 8; i ) { pinMode(sensorPins[i], INPUT); } } void loop() { int sensorValues[8]; // Read sensor values for (int i 0; i 8; i ) { sensorValues[i] digitalRead(sensorPins[i]); } // Determine motor actions based on sensor readings if (sensorValues[0] LOW sensorValues[1] LOW sensorValues[2] LOW) { // Move forward moveForward(); } else if (sensorValues[1] LOW sensorValues[2] LOW) { // Slightly turn right turnRight(); } else if (sensorValues[3] LOW sensorValues[4] LOW) { // Slightly turn left turnLeft(); } else if (sensorValues[7] LOW) { // Hard turn left hardTurnLeft(); } else if (sensorValues[0] LOW) { // Hard turn right hardTurnRight(); } else { // Stop if no line is detected stop(); } } void moveForward() { digitalWrite(motorLeftForward, HIGH); digitalWrite(motorLeftBackward, LOW); digitalWrite(motorRightForward, HIGH); digitalWrite(motorRightBackward, LOW); } void turnRight() { digitalWrite(motorLeftForward, HIGH); digitalWrite(motorLeftBackward, LOW); digitalWrite(motorRightForward, LOW); digitalWrite(motorRightBackward, LOW); } void turnLeft() { digitalWrite(motorLeftForward, LOW); digitalWrite(motorLeftBackward, LOW); digitalWrite(motorRightForward, HIGH); digitalWrite(motorRightBackward, LOW); } void hardTurnLeft() { digitalWrite(motorLeftForward, LOW); digitalWrite(motorLeftBackward, LOW); digitalWrite(motorRightForward, HIGH); digitalWrite(motorRightBackward, LOW); } void hardTurnRight() { digitalWrite(motorLeftForward, HIGH); digitalWrite(motorLeftBackward, LOW); digitalWrite(motorRightForward, LOW); digitalWrite(motorRightBackward, LOW); } void stop() { digitalWrite(motorLeftForward, LOW); digitalWrite(motorLeftBackward, LOW); digitalWrite(motorRightForward, LOW); digitalWrite(motorRightBackward, LOW); }
Explanation
Sensor Reading: The code reads the values from the 8 IR sensors. The sensors output LOW when they detect a line (black surface) and HIGH when they detect the ground (white surface). Motor Control: Depending on the sensor readings, the robot will move forward, turn left, turn right, or stop. Movement Logic: The robot uses simple conditions to decide how to react based on which sensors detect the line.Tips
Tune the logic based on your specific sensor readings and the characteristics of your robot. You may want to implement additional functionality like PID control for smoother line following. Test and adjust the thresholds based on your environment and surface.This code should provide a good starting point for your line-following robot project!
-
Exploring the Limits and the Mean Value Theorem: A Critical Analysis
Exploring the Limits and the Mean Value Theorem: A Critical Analysis The Mean Va
-
Master of Science in Information Technology at KAIST BMSIT: Exploring the Fees and Curriculum
Master of Science in Information Technology at KAIST BMSIT: Exploring the Fees a