Is the code and the circuit correct ?

Hello, I am new to this field and don't know it very well yet. I wanted to ask if the code I have written works or if there are even better solutions or alternatives.
My goal is that when the ultrasonic sensor detects an object less than 40 cm away, the dc motor should turn on for 5 seconds. Thank you very much in advance :grin:. I am using a HC-SR04 Ultrasonic sensor, Arduino Uno, 9V battery, L298n motor driver and a Hobby dc motor.

Code:

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 13;
int in1 = 2;
int in2 = 4;
int enA = 3;
                            //  Project by - Be innovative with Prasad
                                    //   title - ultrasonic sensor project with buzzer and Arduino 
// defines variables
long duration;
int distance;
int safetyDistance;
 
            
void setup() 
{

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode (enA, OUTPUT);
pinMode (in1, OUTPUT);
pinMode (in2, OUTPUT);
Serial.begin(9600); // Starts the serial communication

}
 
 
void loop() 
{

// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
 
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
 
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
 
// Calculating the distance
distance= duration*0.034/2;
 
safetyDistance = distance;
if (safetyDistance <= 40)   // You can change safe distance from here changing value Ex. 20 , 40 , 60 , 80 , 100, all in cm
{
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enA, 100);
  delay(5000);
}
else{
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  analogWrite(enA, 0);
}

}

How should we know? You are right there - does the code work?

What is it doing that it should not do, or not doing that it should?

a7

It should already be "clear"

Why bother?

You're pinging far too frequently.
You've got no way of distinguishing a late, distant echo from the most recent ping.

There is no good reason that these variables should have global scope.

If you really are using the battery depicted then expect problems with it providing enough current to run the motor

That battery is intended for use with low power devices such as smoke alarms. Consider using 6 AA batteries in series as an alternative

1 Like

Ok thanks for the advice

Have you written your code in stages, to test each piece of hardware BEFORE combining your code?

You should have code that tests each bit of hardware individually before you combine them one at a time, removing bugs as you go.

It sounds like you have not even tried your code.

Can I suggest you write some code that JUST uses the ultrasonic, so you can prove the hardware and the code.
THEN.
Code for the motor driver, a simple code to drive the motor back and forth, do some different speeds, so you can prove your hardware.

I think you will find a 9V smokedetector battery will not be able to provide enough current and be the main cause of frustrating problems.

Tom.. :smiley: :+1: :coffee: :australia:

Hi first of all thanks for the help. yes i tested the code step by step and it worked but when i put it together i was not sure if it was put together correctly. thanks for the advice i will now use 6 AA batteries instead of a 9V. :grin:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.