Components and supplies
1
Motor Shield
1
Arduino UNO
1
Ultrasonic Sensor - HC-SR04 (Generic)
Project description
Code
Line Follower Robot Code
c_cpp
You can copy-paste this, or modify it after you understand how it works
1#include <Ultrasonic.h> //This calls upon the libray for the Ultrasonic sensor 2 3Ultrasonic ultrasonic (9, 10);/*Says that the Trig pin is pin 9 and the Echo pin is pin 10. It also gives the sensor a name. In this case, the name is ultrasonic.*/ 4 5const int right_irsensor = 8;/*Says that the right IR sensor is connected to pin 8. const is for saying that it will always be connected to pin 8. int is for integer. 8 is an integer, so we use int. After int, a variable or a constant is declared. In this case, it is a constant. A variable can change values while a constant cannot.*/ 6const int left_irsensor = 7; 7const int motor_rightFront = 3;//the wire of the right motor which controls its forward motion is connected to pin 3 8const int motor_rightBack = 2;//the wire of the motor which controls its backward motion is connected to pin 3 9const int motor_leftFront = 5;//The varables can be named however you want. I decided to go along with simple names. 10const int motor_leftBack = 4; 11int x; 12int y;/*x and y are two variables that I will be using in order to declare inputs and outputs. This method is not necessary, but it is easier than the conventional method*/ 13 14void setup() { 15// put your setup code here, to run once: 16Serial.begin(9600); 17for(x = 2; x<6; x++){//As long as x starts at 2 and increases by 1 each time and remains less than 6. In other words, pins 1 to 5. 18pinMode(x, OUTPUT);/*Declaring x as an output. The pinMode() function is used to do this. In short, pins 2 to 5 are outputs. This makes sense since motors are control devices, to which commands are given.*/ 19} 20for(y=7; y<9 ; y++){ 21pinMode(y, INPUT);//The two IR sensors are declared as inputs. The sensors give values, so this also makes sense. 22} 23} 24 25void loop() { 26// put your main code here, to run repeatedly: 27int right_sensor = digitalRead(right_irsensor);/*declaring a variable called right_sensor to take the reading from the right IR sensor. We are using digitalRead() since it is only a white and black surface. It will read LOW for black and HIGH for white.*/ 28int left_sensor = digitalRead(left_irsensor); 29int reading = ultrasonic.read();//Declaring a variable for the Ultrasonic sensor to read. Here, the name you gave to the sensor has to be used for the command. So, <your name>.read 30if(reading>29){ //If the reading is more than 29 cm, then the robot can run. 31if(right_sensor == HIGH && left_sensor == LOW){/*The right sensor detects white and the left sensor detects black.*/ 32left();//turn left. 33} 34if(right_sensor == LOW && left_sensor == HIGH){ 35right(); 36} 37if(right_sensor == LOW && left_sensor == LOW){ 38yay(); 39} 40if(right_sensor == HIGH && left_sensor == HIGH){ 41forward(); 42} 43} 44else{ 45yay();//if it is not more than 29 cm, then it will stop. yay() is defined as a function below. 46} 47} 48 49void right(){//void is used to define functions. This function will make the robot turn right. 50/*digitalWrite() is used to make things work or stop based on digital signals given to it in the form of HIGH and LOW.HIGH is to make something work while LOW is to stop it.*/ 51digitalWrite(motor_rightFront, LOW);//make the right motor stop moving forward 52digitalWrite(motor_rightBack,LOW );//make the right motor stop moving backward 53digitalWrite(motor_leftFront, LOW);//make the left motor stop moving forward 54digitalWrite(motor_leftBack, HIGH);//make the left motor stop moving backward 55} 56 57void left(){ 58digitalWrite(motor_rightFront, LOW); 59digitalWrite(motor_rightBack, HIGH); 60digitalWrite(motor_leftFront, LOW); 61digitalWrite(motor_leftBack, LOW); 62 63} 64 65void forward(){ 66digitalWrite(motor_rightFront, LOW); 67digitalWrite(motor_rightBack, HIGH); 68digitalWrite(motor_leftFront, LOW); 69digitalWrite(motor_leftBack, HIGH); 70 71} 72 73void yay(){ //This is to make it stop. stop is a function on its own, so I could not name this function stop. 74digitalWrite(motor_rightFront, LOW); 75digitalWrite(motor_rightBack, LOW); 76digitalWrite(motor_leftFront, LOW); 77digitalWrite(motor_leftBack, LOW); 78} 79 80void backward(){ 81digitalWrite(motor_rightFront, HIGH); 82digitalWrite(motor_rightBack, LOW); 83digitalWrite(motor_leftFront, HIGH); 84digitalWrite(motor_leftBack, LOW); 85}
Line Follower Robot Code
c_cpp
You can copy-paste this, or modify it after you understand how it works
1#include <Ultrasonic.h> //This calls upon the libray for the Ultrasonic sensor 2 3Ultrasonic ultrasonic (9, 10);/*Says that the Trig pin is pin 9 and the Echo pin is pin 10. It also gives the sensor a name. In this case, the name is ultrasonic.*/ 4 5const int right_irsensor = 8;/*Says that the right IR sensor is connected to pin 8. const is for saying that it will always be connected to pin 8. int is for integer. 8 is an integer, so we use int. After int, a variable or a constant is declared. In this case, it is a constant. A variable can change values while a constant cannot.*/ 6const int left_irsensor = 7; 7const int motor_rightFront = 3;//the wire of the right motor which controls its forward motion is connected to pin 3 8const int motor_rightBack = 2;//the wire of the motor which controls its backward motion is connected to pin 3 9const int motor_leftFront = 5;//The varables can be named however you want. I decided to go along with simple names. 10const int motor_leftBack = 4; 11int x; 12int y;/*x and y are two variables that I will be using in order to declare inputs and outputs. This method is not necessary, but it is easier than the conventional method*/ 13 14void setup() { 15// put your setup code here, to run once: 16Serial.begin(9600); 17for(x = 2; x<6; x++){//As long as x starts at 2 and increases by 1 each time and remains less than 6. In other words, pins 1 to 5. 18pinMode(x, OUTPUT);/*Declaring x as an output. The pinMode() function is used to do this. In short, pins 2 to 5 are outputs. This makes sense since motors are control devices, to which commands are given.*/ 19} 20for(y=7; y<9 ; y++){ 21pinMode(y, INPUT);//The two IR sensors are declared as inputs. The sensors give values, so this also makes sense. 22} 23} 24 25void loop() { 26// put your main code here, to run repeatedly: 27int right_sensor = digitalRead(right_irsensor);/*declaring a variable called right_sensor to take the reading from the right IR sensor. We are using digitalRead() since it is only a white and black surface. It will read LOW for black and HIGH for white.*/ 28int left_sensor = digitalRead(left_irsensor); 29int reading = ultrasonic.read();//Declaring a variable for the Ultrasonic sensor to read. Here, the name you gave to the sensor has to be used for the command. So, <your name>.read 30if(reading>29){ //If the reading is more than 29 cm, then the robot can run. 31if(right_sensor == HIGH && left_sensor == LOW){/*The right sensor detects white and the left sensor detects black.*/ 32left();//turn left. 33} 34if(right_sensor == LOW && left_sensor == HIGH){ 35right(); 36} 37if(right_sensor == LOW && left_sensor == LOW){ 38yay(); 39} 40if(right_sensor == HIGH && left_sensor == HIGH){ 41forward(); 42} 43} 44else{ 45yay();//if it is not more than 29 cm, then it will stop. yay() is defined as a function below. 46} 47} 48 49void right(){//void is used to define functions. This function will make the robot turn right. 50/*digitalWrite() is used to make things work or stop based on digital signals given to it in the form of HIGH and LOW.HIGH is to make something work while LOW is to stop it.*/ 51digitalWrite(motor_rightFront, LOW);//make the right motor stop moving forward 52digitalWrite(motor_rightBack,LOW );//make the right motor stop moving backward 53digitalWrite(motor_leftFront, LOW);//make the left motor stop moving forward 54digitalWrite(motor_leftBack, HIGH);//make the left motor stop moving backward 55} 56 57void left(){ 58digitalWrite(motor_rightFront, LOW); 59digitalWrite(motor_rightBack, HIGH); 60digitalWrite(motor_leftFront, LOW); 61digitalWrite(motor_leftBack, LOW); 62 63} 64 65void forward(){ 66digitalWrite(motor_rightFront, LOW); 67digitalWrite(motor_rightBack, HIGH); 68digitalWrite(motor_leftFront, LOW); 69digitalWrite(motor_leftBack, HIGH); 70 71} 72 73void yay(){ //This is to make it stop. stop is a function on its own, so I could not name this function stop. 74digitalWrite(motor_rightFront, LOW); 75digitalWrite(motor_rightBack, LOW); 76digitalWrite(motor_leftFront, LOW); 77digitalWrite(motor_leftBack, LOW); 78} 79 80void backward(){ 81digitalWrite(motor_rightFront, HIGH); 82digitalWrite(motor_rightBack, LOW); 83digitalWrite(motor_leftFront, HIGH); 84digitalWrite(motor_leftBack, LOW); 85}
Downloadable files
Schematic for just the Motor Driver
If you don't have a motor shield, or would just prefer to use a motor driver, then this is the schematic for you. A disadvantage is that you also need a breadboard.
Schematic for just the Motor Driver

Comments
Only logged in users can leave comments