Arduino Nano Line Follower Robot with Obstacle Avoidance and PID Control
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This document provides a detailed overview of a line follower robot with obstacle avoidance capabilities. The robot uses an 8-array IR sensor to follow a line and an ultrasonic sensor to detect and avoid obstacles. The system is controlled by an Arduino Nano microcontroller, which interfaces with various components including motors, sensors, LEDs, and buttons.
Component List
Arduino Nano
- Description: Microcontroller board based on the ATmega328P.
- Pins: D1/TX, D0/RX, RESET, GND, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11/MOSI, D12/MISO, VIN, 5V, A7, A6, A5, A4, A3, A2, A1, A0, AREF, 3V3, D13/SCK
Ponte H
- Description: H-bridge motor driver.
- Pins: OUT4-Motor2, OUT3-Motor2, OUT2-Motor1, OUT1-Motor1, 5V, GND, 12V, IN3, IN1, IN2, IN4
HC-SR04 Ultrasonic Sensor
- Description: Ultrasonic distance sensor.
- Pins: VCC, TRIG, ECHO, GND
DC Mini Metal Gear Motor
- Description: Small DC motor with metal gears.
- Pins: IN1, IN2
8 Array IR Sensor
- Description: Infrared sensor array for line following.
- Pins: GND, IR, D1, D2, D3, D4, D5, D6, D7, D8, VCC
Pushbutton
- Description: Simple pushbutton switch.
- Pins: Pin 2, Pin 1, Pin 3, Pin 4
Battery 12V
- Description: 12V battery for power supply.
- Pins: +, -
Rocker Switch
- Description: On/Off switch.
- Pins: 1, 2
Alligator Clip Cable (Red)
- Description: Alligator clip cable for temporary connections.
- Pins: Pin, Alligator
LED: Two Pin (Blue)
- Description: Blue LED.
- Pins: cathode, anode
LED: Two Pin (Orange)
- Description: Orange LED.
- Pins: cathode, anode
Wiring Details
Arduino Nano
- GND: Connected to the GND of the battery, HC-SR04 Ultrasonic Sensor, and Ponte H.
- D2: Connected to Pin 3 of the Pushbutton.
- D3: Connected to IN1 of the Ponte H.
- D4: Connected to IN2 of the Ponte H.
- D5: Connected to Pin of the Alligator Clip Cable (Red).
- D6: Connected to Pin 3 of the Pushbutton.
- D7: Connected to ECHO of the HC-SR04 Ultrasonic Sensor.
- D8: Connected to TRIG of the HC-SR04 Ultrasonic Sensor.
- D9: Connected to IN3 of the Ponte H.
- D10: Connected to IN4 of the Ponte H.
- D11/MOSI: Connected to Pin of the Alligator Clip Cable (Red).
- D12/MISO: Connected to anode of the Orange LED.
- VIN: Connected to Pin 1 of the Rocker Switch and 12V of the Ponte H.
- 5V: Connected to VCC of the HC-SR04 Ultrasonic Sensor.
- A7: Connected to D8 of the 8 Array IR Sensor.
- A6: Connected to D7 of the 8 Array IR Sensor.
- A5: Connected to D6 of the 8 Array IR Sensor.
- A4: Connected to D5 of the 8 Array IR Sensor.
- A3: Connected to D4 of the 8 Array IR Sensor.
- A2: Connected to D3 of the 8 Array IR Sensor.
- A1: Connected to D2 of the 8 Array IR Sensor.
- A0: Connected to D1 of the 8 Array IR Sensor.
- 3V3: Connected to VCC of the 8 Array IR Sensor.
- D13/SCK: Connected to anode of the Blue LED.
Ponte H
- GND: Connected to the GND of the battery.
- IN1: Connected to D3 of the Arduino Nano.
- IN2: Connected to D4 of the Arduino Nano.
- IN3: Connected to D9 of the Arduino Nano.
- IN4: Connected to D10 of the Arduino Nano.
- 12V: Connected to VIN of the Arduino Nano and Pin 1 of the Rocker Switch.
- OUT4-Motor2: Connected to IN1 of the DC Mini Metal Gear Motor.
- OUT3-Motor2: Connected to IN2 of the DC Mini Metal Gear Motor.
- OUT2-Motor1: Connected to IN2 of the DC Mini Metal Gear Motor.
- OUT1-Motor1: Connected to IN1 of the DC Mini Metal Gear Motor.
HC-SR04 Ultrasonic Sensor
- GND: Connected to the GND of the battery.
- TRIG: Connected to D8 of the Arduino Nano.
- ECHO: Connected to D7 of the Arduino Nano.
- VCC: Connected to 5V of the Arduino Nano.
DC Mini Metal Gear Motor
- IN1: Connected to OUT4-Motor2 of the Ponte H.
- IN2: Connected to OUT3-Motor2 of the Ponte H.
- IN2: Connected to OUT2-Motor1 of the Ponte H.
- IN1: Connected to OUT1-Motor1 of the Ponte H.
8 Array IR Sensor
- GND: Connected to the GND of the Arduino Nano.
- D1: Connected to A0 of the Arduino Nano.
- D2: Connected to A1 of the Arduino Nano.
- D3: Connected to A2 of the Arduino Nano.
- D4: Connected to A3 of the Arduino Nano.
- D5: Connected to A4 of the Arduino Nano.
- D6: Connected to A5 of the Arduino Nano.
- D7: Connected to A6 of the Arduino Nano.
- D8: Connected to A7 of the Arduino Nano.
- VCC: Connected to 3V3 of the Arduino Nano.
Pushbutton
- Pin 1: Connected to the GND of the Arduino Nano.
- Pin 3: Connected to D2 of the Arduino Nano.
- Pin 1: Connected to the GND of the Arduino Nano.
- Pin 3: Connected to D6 of the Arduino Nano.
Battery 12V
- -: Connected to the GND of the Arduino Nano, HC-SR04 Ultrasonic Sensor, and Ponte H.
- +: Connected to Pin 2 of the Rocker Switch.
Rocker Switch
- 1: Connected to VIN of the Arduino Nano and 12V of the Ponte H.
- 2: Connected to + of the battery.
Alligator Clip Cable (Red)
- Pin: Connected to D5 of the Arduino Nano.
- Pin: Connected to D11/MOSI of the Arduino Nano.
LED: Two Pin (Blue)
- cathode: Connected to the GND of the Arduino Nano.
- anode: Connected to D13/SCK of the Arduino Nano.
LED: Two Pin (Orange)
- cathode: Connected to the GND of the Arduino Nano.
- anode: Connected to D12/MISO of the Arduino Nano.
Code Documentation
/*
* Line Follower Robot with Obstacle Avoidance
* This Arduino sketch uses a PID algorithm to follow a line using an 8-array IR sensor.
* It also uses an ultrasonic sensor to detect and avoid obstacles.
* Two buttons are used: one for calibration and one to start the robot.
* During calibration, a blue LED is lit.
*/
#include <PID_v1.h>
// Pin definitions
#define IR_PIN A0
#define TRIG_PIN A7
#define ECHO_PIN A6
#define LED_PIN 4
#define CALIBRATE_BUTTON_PIN 2
#define RUN_BUTTON_PIN A3
#define MOTOR1_IN1 9
#define MOTOR1_IN2 6
#define MOTOR2_IN3 5
#define MOTOR2_IN4 3
// PID parameters
double setpoint = 0, input = 0, output = 0;
PID myPID(&input, &output, &setpoint, 2, 5, 1, DIRECT);
// Function prototypes
void calibrateSensors();
void follow