Obstacle Avoidance Using IR Sensor and Ultrasonic

I am doing a small project about Obstacle Avoidance using both IR Sensor and Ultrasonic . I want to use both buzzer and vibrator motor to alarm and my problems are:

  1. I don’t know how to code for the vibrator motor, the speed increases when the sensors come nearly to the obstacle.

  2. I dont know how to code for both IR and Ultrasonic work together with the same time, the same distance and then alarm.

  3. I saw on google that we use capacitor to reduce noise for IR sensor. Is it really worked?

Here is components:

IR sensor: Analog SHARP GP2Y0A21YK0F
Ultrasonic US-015
buzzer
Vibrating Motor 1027

Here is my code for the buzzer alarms:

#define trigPin 7
#define echoPin 6
#define buzzer 4
int val= 0;
int ir =0;
void setup() 
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzer, OUTPUT); 
  pinMode(ir, INPUT);
}
void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  int ir = analogRead(A0);  //read the sensor value
  int val= pow(3027.4/ir, 1.2134); //convert readings to distance(cm)
 if (val < 50 )
 { 
 tone(4,100);
  for (int i=val; i>10; i--)
    delay(10);

  noTone(4);
  for (int i=val; i>10; i--)
    delay(10);
    }
    Serial.print(distance);
    Serial.println(" cm"); 
    Serial.print(val);
    Serial.println(" cm ir"); 
  delay(200);
}
int ir =0;
void setup() 
{
  Serial.begin (9600);

  pinMode(ir, INPUT);
}

You should NOT set Pin 0 (Serial RX) as an INPUT when you are using Serial. Same for Pin 1 (Serial TX).

You do not need to set Pin A0 to INPUT when you are using it for analog input.

Note: Unless you are using analogRead(), ‘0’ and ‘A0’ mean different things.

You need to decide which digital pin will have the vibrator motor attached.

A good way to assign names to pin numbers is:

const byte NameOfPin = 2;

For example, in your sketch it would be:

const byte TriggerPin = 7;
const byte EchoPin = 6;
const byte BuzzerPin = 4;
const byte IRPin = A0;

Thank you so much for correcting me ! And I still wonder about question 2: how to code for both IR and Ultrasonic work together with the same time, the same distance and then alarm"

ktodaone:
Thank you so much for correcting me ! And I still wonder about question 2: how to code for both IR and Ultrasonic work together with the same time, the same distance and then alarm"

Measure the distance with both.
If the distances are similar enough and close enough to cause alarm, sound the alarm.
Have you got the distance measurements working? Have you written a sketch that shows the two distances on Serial Monitor (or Serial Plotter)? The Ultrasonic reading is slower than the IR measurement so I would do that one first. With the two measuring systems showing values you can adjust your constants until they give the same distance.

johnwasser:
Measure the distance with both.
If the distances are similar enough and close enough to cause alarm, sound the alarm.
Have you got the distance measurements working? Have you written a sketch that shows the two distances on Serial Monitor (or Serial Plotter)? The Ultrasonic reading is slower than the IR measurement so I would do that one first. With the two measuring systems showing values you can adjust your constants until they give the same distance.

Hi sir,
I see , thank you so much! This is my update code for Ultrasonic , and for IR i just replace “distance” by “val” on “if” part . Each sensor still work well with their value “val” and “distance” . So i just can measure for each sensor not both, not in the same time they work together. I dont know how to combine sensors code working with the same code.

#define trigPin 7
#define echoPin 6
#define buzzer 4
int val= 0;
int ir =0;
void setup() 
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzer, OUTPUT); 
  pinMode(ir, INPUT);
}
void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  int ir = analogRead(A0);  //read the sensor value
  int val= pow(3027.4/ir, 1.2134); //convert readings to distance(cm)
 if (distance < 50 )
 { 
 tone(3,500);
  for (int i=distance; i>0; i--)
    delay(10);

  noTone(3);
  for (int i=distance; i>0; i--)
    delay(10);
    }
    Serial.print(distance);
    Serial.println(" cm"); 
    Serial.print(val);
    Serial.println(" cm ir"); 
  delay(100);
}

You could use the average of the two:

#define trigPin 7
#define echoPin 6
#define buzzer 4
int val = 0;
int ir = 0;
void setup()
{
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(ir, INPUT);
}
void loop()
{
  unsigned long duration;
  unsigned ultrasonic_distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  ultrasonic_distance = (duration / 2) / 29.1;


  unsigned ir_distance = pow(3027.4 / analogRead(A0), 1.2134); //convert readings to distance(cm)


  unsigned average_distance = (ultrasonic_distance + ir_distance) / 2;
  
  if (average_distance < 50)
  {
    tone(3, 500);
    delay(10 * average_distance);
    noTone(3);
    delay(10 * average_distance);
  }


  Serial.print("ultrasonic_distance == ");
  Serial.print(ultrasonic_distance);
  Serial.println(" cm");


  Serial.print("ir_distance == ");
  Serial.print(ir_distance);
  Serial.println(" cm");


  Serial.print("average_distance == ");
  Serial.print(average_distance);
  Serial.println(" cm");


  delay(100);
}