Materials:-
Arduino uno = https://bit.ly/30v5XcY
Motor Drive L298N = http://bit.ly/3bVldY7
IR Sensor = http://bit.ly/38QOmll
Gear motor = http://bit.ly/3cH2nmO
Diagram:-
Connections:-
IR Sensor to Arduino
- IR sensor VCC = Arduino 5V
- IR sensor GND = Arduino GND
- IR sensor out (left) = Arduino D9
- IR sensor out (Right) = Arduino D8
Motor Drive(L298N) to Arduino
- Motor drive IN1 = Arduino D5
- Motor drive IN2 = Arduino D4
- Motor drive IN3 = Arduino D3
- Motor drive IN4 = Arduino D2
- Motor drive 5V out = Arduino Vin
- Motor drive GND = Battery pack GND & Arduino GND
- Motor drive 12V IN = Battery pack VCC
Code
#define motor1 2
#define motor12 3
#define motor22 4
#define motor2 5
void setup() {
pinMode(motor1 ,OUTPUT);
pinMode(motor12 ,OUTPUT);
pinMode(motor2 ,OUTPUT);
pinMode(motor22 ,OUTPUT);
pinMode(8,INPUT);
pinMode(9, INPUT);
}
void loop() {
int leftsen=digitalRead(8);
int rightsen=digitalRead(9);
if(leftsen== 0 && rightsen== 0)
{
digitalWrite(motor1,HIGH);
digitalWrite(motor12,LOW);
digitalWrite(motor2,HIGH);
digitalWrite(motor22,LOW);
}
else if(leftsen== 1 && rightsen== 0)
{
digitalWrite(motor1,LOW);
digitalWrite(motor12,HIGH);
digitalWrite(motor2,HIGH);
digitalWrite(motor22,LOW);
}
else if(leftsen== 0 && rightsen== 1 )
{
digitalWrite(motor1,HIGH);
digitalWrite(motor12,LOW);
digitalWrite(motor2,LOW);
digitalWrite(motor22,HIGH);
}
else if(leftsen== 1 && rightsen== 1)
{
digitalWrite(motor1,LOW);
digitalWrite(motor12,LOW);
digitalWrite(motor2,LOW);
digitalWrite(motor22,LOW);
}
}