Arduino Code and Video: 4-Channel Infrared Obstacle Sensor for Arduino Smart Car

Arduino Code and Video: 4-Channel Infrared Obstacle Sensor for Arduino Smart Car

In this tutorial, we will explore how to use a 4-channel infrared obstacle avoidance sensor module to create a smart car. This module consists of four infrared sensors that can detect obstacles in various directions, allowing your robotic vehicle to react accordingly. Each sensor provides feedback on proximity, enabling you to program responses based on the detected obstacles.

As we move through this project, we will cover the hardware components, wiring instructions, and the code necessary to operate the sensors effectively. You can find additional clarification in the video (in video at 00:00).

Hardware Explained

The main component of this project is the 4-channel infrared obstacle avoidance module, which utilizes the LM339 quad differential comparator. This IC is capable of operating between 2 to 36 volts, but in our case, we will use 5 volts for compatibility with the Arduino. Each sensor in the module has an inverting and non-inverting input, allowing it to compare the infrared light reflected from obstacles and output a signal accordingly.

The module is designed with pull-up resistors that keep the output high when no obstacle is detected. When an object is detected, the output goes low, providing a clear signal to the Arduino. Additionally, the module features LEDs that indicate when an obstacle is detected, enhancing user feedback during operation.

Datasheet Details

ManufacturerUnknown
Part number4-Channel Infrared Obstacle Avoidance
Logic/IO voltage5 V
Supply voltage2–36 V
Output current (per channel)10 mA (typ.)
Peak current (per channel)20 mA (max.)
PWM frequency guidanceN/A
Input logic thresholdsLOW < 0.8 V, HIGH > 2.0 V
Voltage drop / RDS(on) / saturation0.2 V (typ.)
Thermal limitsOperating temperature: -40 to 125 °C
PackageModule
Notes / variantsIncludes 4 sensors with adjustable sensitivity

  • Use a common ground for all components to avoid floating inputs.
  • Adjust the sensitivity potentiometer for optimal detection range.
  • Ensure proper power supply voltage to avoid damaging the sensors.
  • Check wiring connections to prevent misreads from the sensors.
  • Use decoupling capacitors to stabilize power supply for the sensors.
  • Test each sensor individually before integrating into the main system.

Wiring Instructions

To wire the 4-channel infrared obstacle avoidance module to the Arduino, start by connecting the power and ground. Connect the VCC pin of the sensor module to the 5V output on the Arduino. Then, connect the GND pin of the module to the ground (GND) on the Arduino. This will provide the necessary power for the sensors to operate.

Next, connect the output pins of the sensors to the digital input pins on the Arduino. For example, connect the output of the front-left sensor (labeled as N1) to digital pin 2, the front-right sensor (N2) to pin 3, the rear-left sensor (N3) to pin 4, and the rear-right sensor (N4) to pin 5. Additionally, you can connect the brake output to pin 8, the front obstacle indicator to pin 9, and the rear obstacle indicator to pin 10. Make sure all connections are secure for reliable operation.

Code Examples & Walkthrough

The Arduino code begins by defining constants for each sensor pin for easier reference throughout the program. For instance, FRONT_LEFT is defined as pin 2, making it clear which sensor corresponds to which pin.

#define FRONT_LEFT 2 // pin 2 for front-left sensor
#define FRONT_RIGHT 3 // pin 3 for front-right sensor
#define REAR_LEFT 4 // pin 4 for rear-left sensor
#define REAR_RIGHT 5 // pin 5 for rear-right sensor

In the setup() function, each sensor pin is configured as an input using pinMode(). This setup allows the Arduino to read the states of the sensors during operation.

void setup() {
  Serial.begin(9600);
  pinMode(FRONT_LEFT, INPUT);
  pinMode(FRONT_RIGHT, INPUT);
  pinMode(REAR_LEFT, INPUT);
  pinMode(REAR_RIGHT, INPUT);
}

In the main loop, the state of each sensor is read using digitalRead(). If an obstacle is detected (indicated by a LOW signal), the corresponding obstacle LED is activated, and a message is printed to the serial monitor.

void loop() {
  int FR = digitalRead(FRONT_RIGHT);
  int FL = digitalRead(FRONT_LEFT);
  
  if( FR == LOW || FL == LOW) {
    digitalWrite(FRONT_OB,HIGH);
    Serial.println("Front obstacle");
  } else {
    digitalWrite(FRONT_OB,LOW);
  }
}

This code structure allows for easy expansion and modification, enabling you to tailor the responses based on your specific application needs. For the complete code, please refer to the loading section below.

Demonstration / What to Expect

Once everything is wired and the code is uploaded, you should expect the LEDs on the module to light up whenever an obstacle is detected by any of the sensors. The serial monitor will also display messages indicating the presence of obstacles in the front or rear. If the sensors are positioned correctly, the smart car will react by stopping or changing direction as programmed (in video at 02:30).

Common pitfalls include incorrect wiring, which may lead to floating inputs or misreads. Ensure that the sensors are properly calibrated and that the potentiometer is adjusted for the desired sensitivity. Testing each sensor independently can help identify issues before full integration.

Video Timestamps

  • 00:00 – Introduction to the project
  • 01:30 – Hardware explanation
  • 02:30 – Code walkthrough
  • 03:45 – Wiring instructions
  • 04:50 – Live demonstration

Images

4 channel infrared sensor module-6
4 channel infrared sensor module-6
4 channel infrared sensor module-1
4 channel infrared sensor module-1
4 channel infrared sensor module-3
4 channel infrared sensor module-3
4 channel infrared sensor module-
4 channel infrared sensor module-4
4 channel infrared sensor module-
4 channel infrared sensor module-5
60-This is the Arduino code for a 4-channel infrared obstacle sensor.
Language: C++
Copied!

Things you might need

Resources & references

No resources yet.

Files📁

No files available.