im trying to make a obstacle detection robot car using :
arduino uno r3 + 3x hc sr04 ultrasonic distance sensors + L298n motor driver + 6xAA battery external power supply + 2x dc motors/wheels (connection below)
my code (attached below) works fine when it is connected to my pc (all 3 sensors can sense distance, wheels rotate forward & reverse according to my code), but once i disconnect it the wheels stop functioning according to my code (it just keeps turning forward forever even with different input from the sensors).
i have tried some solutions suggested here before such as connecting the RX pin to ground and i have already commented out the "serial" communication functions in my code.
however, nothing solves my issue of the wheels not working as it should.
problem: code (wheels) work fine when connected to my pc, but doesnt work properly when connected to external power supply (via the l298n)
would appreciate any and all help & suggestions. thanks
connection:
code:
int t0 = 12;
int t1 = 10;
int t2 = 8;
//declare int var t0 at pin 8 to activate/trigger ultrasonic wave
int e0 = 13;
int e1 = 11;
int e2 = 9;
//declare int var e0 at pin 9 to receive the ultrasonic echo wave
//t = trigger pin, e = echo pin, 0 = 1st ldr position, a = analog, b = boolean value, c = calibrated value
int ObstacleDist = 5;
//change this value to change obstacle detection distance
int time0;
int time1;
int time2;
//store time taken to register echo for 1st uds
int dist0;
int dist1;
int dist2;
//store distance value for 1st uds
bool obs0;
bool obs1;
bool obs2;
//obstacle detection for 1st uds, obstacle = 1, no obstacle = 0
int obst;
//store total value/readings of all uds to set if condition
int stp;
//declare int var stp to store value 0, stops the while loop when 1
//used to stop the while loop that holds the if else logic to determine wheel movement
//while loop is used to make the if else statement be repeatedly verified until stp = 1
//when robot moves to the end of the course obst = 000, make stp = 1 or any number since it is int data type
//initialise L298N driver that controls DC motors/wheels
//l298n driver draws in high voltage required to power the dc motors as the arduino cannot supply enough power that the dc motors require
//l298n driver reduces the high voltage drawn in from the batteries & decreases it to supply the arduino with power when it is disconnected from the monitor
//pin 2 > ena, pin 3 > in1, pin 4 > in2
//pin 5 > enb, pin 6 > in3, pin 7 > in4
int enA = 2;
//declare int var enA to store value of 2, it outputs values from pin 2 of the controller to enA pin of l298n driver
//enA sets the speed for the left wheel
int in1 = 3;
//declare int var in1 to store value of 3, it outputs values from pin 3 of the controller to in1 pin of l298n driver
//in1 drives the left wheel forward when it is high & reverses the wheel when low
int in2 = 4;
//declare int var in2 to store value of 4, it outputs values from pin 3 of the controller to in2 pin of l298n driver
//in2 drives the left wheel forward when it is low & reverses the wheel when high
int enB = 5;
//declare int var enB to store value of 5, it outputs values from pin 5 of the controller to enB pin of l298n driver
//enB sets the speed for the right wheel
int in3 = 6;
//declare int var in1 to store value of 6, it outputs values from pin 6 of the controller to in3 pin of l298n driver
//in3 drives the right wheel forward when it is high & reverses the wheel when low
int in4 = 7;
//declare int var in2 to store value of 7, it outputs values from pin 7 of the controller to in4 pin of l298n driver
//in4 drives the right wheel forward when it is low & reverses the wheel when high
void setup() {
// put your setup code here, to run once:
//Serial.begin(9600);
//begin serial communication with controller at 9600baud
pinMode(t0, OUTPUT);
pinMode(t1, OUTPUT);
pinMode(t2, OUTPUT);
//set t0 trig pin 8 to output mode, activates/triggers the ultrasonic wave to be sent
pinMode(e0, INPUT);
pinMode(e1, INPUT);
pinMode(e2, INPUT);
//set e0 echo pin 9 to input mode, receives the ultrasonic echo wave
stp = 0;
//initialise var stop program to store value 0 so while loop doesnt stop
pinMode(enA, OUTPUT);
//set pin 2 to output data, controls speed of left wheel via enA
pinMode(enB, OUTPUT);
//set pin 5 to output data, controls speed of right wheel via enB
//reset all motors to off it
//turn off dc motors/set initial state/clear all previous memory
digitalWrite(in1, LOW);
//sends digital output signal to controller to set in1 to low
digitalWrite(in2, LOW);
//sends digital output signal to controller to set in2 to low
digitalWrite(in3, LOW);
//sends digital output signal to controller to set in3 to low
digitalWrite(in4, LOW);
//sends digital output signal to controller to set in4 to low
}
void loop() {
// put your main code here, to run repeatedly:
//set speed of dc motor
analogWrite(enA, 230);
//send analog output signal from controller to set left motor speed in enA to be 100
analogWrite(enB, 250);
//send analog output signal from controller to set right motor speed in enA to be 100
while (stp == 0) {
//while stp = 0, continue running the while loop, if stp = any other number, stops the while loop
//clear trigger pin for 2 microseconds (2x10^6)
digitalWrite(t0, LOW);
//store analog detected value from 1st ldr at pin a0 in var ldr0a
delayMicroseconds(2);
//pause for 2 microseconds, higher freq than delay()??
//send ultrasonic pulse to detect obstacles within 4cm
digitalWrite(t0, HIGH);
//trigger/send the ultrasonic pulse
delayMicroseconds(10);
//continue sending pulse for 10 sec
digitalWrite(t0, LOW);
//turn off the ultrasonic pulse
//read ultrasonic wave echo
time0 = pulseIn(e0, HIGH);
//turn e0 echo pin on to listen for the ultrasonic wave reflection
digitalWrite(t1, LOW);
delayMicroseconds(2);
digitalWrite(t1, HIGH);
delayMicroseconds(10);
//continue sending pulse for 10 sec
digitalWrite(t1, LOW);
time1 = pulseIn(e1, HIGH);
digitalWrite(t2, LOW);
delayMicroseconds(2);
digitalWrite(t2, HIGH);
//trigger/send the ultrasonic pulse
delayMicroseconds(10);
//continue sending pulse for 10 sec
digitalWrite(t2, LOW);
//turn off the ultrasonic pulse
time2 = pulseIn(e2, HIGH);
//turn e0 echo pin on to listen for the ultrasonic wave reflection
//calculate distance
dist0 = time0 * (0.034/2);
dist1 = time1 * (0.034/2);
dist2 = time2 * (0.034/2);
//speed of sound = 0.034 cm/microsecond
//MIGHT NEED TO BE RECLAIBRATED to adjust distance
//change obstacledist if changing object detection alarm distance
//obstacle detected = 1, no obstacle = 0
if (dist0 <= ObstacleDist) { //if dist0 is less than or equal to 4, obstacle detected at 1st uds
obs0 = 1; //set obs0 to 1/high/yes
}
else { //if dist0 is not more than 4cm = no obstacle detected
obs0 = 0; //set obs0 to 0/low/no
}
if (dist1 <= ObstacleDist) { //if dist0 is less than or equal to 4, obstacle detected at 1st uds
obs1 = 1; //set obs0 to 1/high/yes
}
else { //if dist0 is not more than 4cm = no obstacle detected
obs1 = 0; //set obs0 to 0/low/no
}
if (dist2 <= ObstacleDist) { //if dist0 is less than or equal to 4, obstacle detected at 1st uds
obs2 = 1; //set obs0 to 1/high/yes
}
else { //if dist0 is not more than 4cm = no obstacle detected
obs2 = 0; //set obs0 to 0/low/no
}
obst = obs0*4 + obs1*2 + obs2;
//display results
/*
Serial.print("\ndist0: ");
Serial.print(dist0);
Serial.print(" cm, dist1: ");
Serial.print(dist1);
Serial.print(" cm, dist2: ");
Serial.print(dist2);
Serial.print(" cm, \n obs0: ");
Serial.print(obs0);
Serial.print(" , obs1: ");
Serial.print(obs1);
Serial.print(" , obs2: ");
Serial.print(obs2);
Serial.print("\n");
Serial.print(obst);
*/
switch (obst){ //stop
case B000:
//stop left wheel
digitalWrite(in1, LOW);
//send digital output signal to in1 to high to stop left wheel
digitalWrite(in2, LOW);
//send digital output signal to in2 to low to stop left wheel
//stop right wheel
digitalWrite(in3, LOW);
//send digital output signal to in3 to high to stop right wheel
digitalWrite(in4, LOW);
//send digital output signal to in4 to low to stop right wheel
Serial.print("end of line: stop both wheels");
stp = 6;
break;
case B001: //turn L
//stop left wheel, or can reverse it too
digitalWrite(in1, LOW);
//send digital output signal to in1 to high to stop left wheel
digitalWrite(in2, LOW);
//send digital output signal to in2 to low to stop left wheel
//move right wheel
digitalWrite(in3, HIGH);
//send digital output signal to in3 to high to make right wheel move forward
digitalWrite(in4, LOW);
//send digital output signal to in4 to low to make right wheel move forward
//Serial.print("obstacle on right: move left, forward left wheel & stop/reverse right wheel");
continue;
case B010: //turn left for 0.5 sec
//stop left wheel, or can reverse it too
digitalWrite(in1, LOW);
//send digital output signal to in1 to high to stop left wheel
digitalWrite(in2, LOW);
//send digital output signal to in2 to low to stop left wheel
//move right wheel
digitalWrite(in3, HIGH);
//send digital output signal to in3 to high to make right wheel move forward
digitalWrite(in4, LOW);
//send digital output signal to in4 to low to make right wheel move forward
//Serial.print("obstacle in front: turn left for 2 sec until no more obstacles");
delay(500);
continue;
case B011: //turn L
//stop left wheel, or can reverse it too
digitalWrite(in1, LOW);
//send digital output signal to in1 to high to stop left wheel
digitalWrite(in2, LOW);
//send digital output signal to in2 to low to stop left wheel
//move right wheel
digitalWrite(in3, HIGH);
//send digital output signal to in3 to high to make right wheel move forward
digitalWrite(in4, LOW);
//send digital output signal to in4 to low to make right wheel move forward
//Serial.print("obstacle on right: move left, forward left wheel & stop/reverse right wheel");
continue;
case B100: //turn right
//move left wheel
digitalWrite(in1, HIGH);
//send digital output signal to in1 to high to make left wheel move forward
digitalWrite(in2, LOW);
//send digital output signal to in2 to low to make left wheel move forward
//stop right wheel, or can reverse it too
digitalWrite(in3, LOW);
//send digital output signal to in3 to low to stop right wheel
digitalWrite(in4, HIGH);
//send digital output signal to in4 to low to stop right wheel
//Serial.print("obstacle on left: move right, forward right wheel & stop/reverse left wheel");
continue;
case B101: //move forward
//move left wheel forward
digitalWrite(in1, HIGH);
//send digital output signal to in1 to high to make left wheel move forward
digitalWrite(in2, LOW);
//send digital output signal to in2 to low to make left wheel move forward
//move right wheel forward
digitalWrite(in3, HIGH);
//send digital output signal to in3 to high to make right wheel move forward
digitalWrite(in4, LOW);
//send digital output signal to in4 to low to make right wheel move forward
//Serial.print("no obstacles in front: move forward");
continue;
case B110: //turn right
//move left wheel
digitalWrite(in1, HIGH);
//send digital output signal to in1 to high to make left wheel move forward
digitalWrite(in2, LOW);
//send digital output signal to in2 to low to make left wheel move forward
//stop right wheel, or can reverse it too
digitalWrite(in3, LOW);
//send digital output signal to in3 to low to stop right wheel
digitalWrite(in4, HIGH);
//send digital output signal to in4 to low to stop right wheel
//Serial.print("obstacle on left: move right, forward right wheel & stop/reverse left wheel");
continue;
case B111: //turn left for 0.5 sec
//stop left wheel, or can reverse it too
digitalWrite(in1, LOW);
//send digital output signal to in1 to high to stop left wheel
digitalWrite(in2, LOW);
//send digital output signal to in2 to low to stop left wheel
//move right wheel
digitalWrite(in3, HIGH);
//send digital output signal to in3 to high to make right wheel move forward
digitalWrite(in4, LOW);
//send digital output signal to in4 to low to make right wheel move forward
//Serial.print("obstacle in front: turn left for 2 sec until no more obstacles");
delay(500);
continue;
default: //error stop
//stop left wheel
digitalWrite(in1, LOW);
//send digital output signal to in1 to high to stop left wheel
digitalWrite(in2, LOW);
//send digital output signal to in2 to low to stop left wheel
//stop right wheel
digitalWrite(in3, LOW);
//send digital output signal to in3 to high to stop right wheel
digitalWrite(in4, LOW);
//send digital output signal to in4 to low to stop right wheel
//Serial.print("error: stop both wheels");
stp = 5;
break;
}
}
}




