Arduino Code and Video for Sharp IR Distance Module

Arduino Code and Video for Sharp IR Distance Module

In this tutorial, we will explore how to use the Sharp IR distance module to measure distances and trigger actions based on the detected distance. This project will demonstrate how to set up the hardware and write the Arduino code to read distance measurements and activate a buzzer when an object is detected within a certain range. You'll learn how to wire the components together and understand the key identifiers in the code.

Sharpt IR distance sensor

For this project, we will be utilizing a Sharp IR sensor that emits infrared light and measures the distance to an object based on the reflected light. When an object is detected within the range, the buzzer will be triggered, providing a simple yet effective way to demonstrate the capabilities of the Sharp IR module. Be sure to check the video for a visual guide on the setup and code (in video at 00:00).

Hardware Explained

The main components of this project include a Sharp IR distance sensor, an Arduino board, and a buzzer. The Sharp IR sensor consists of a transmitter and a receiver that work together to measure distances. When the infrared light from the transmitter hits an object, it reflects back to the receiver, allowing it to calculate the distance based on the time taken for the light to return.

The Arduino acts as the controller, reading the distance measurements from the IR sensor and controlling the buzzer based on the readings. The buzzer will sound when an object is detected within a specified range, making this setup useful for applications such as obstacle detection in robotics.

Datasheet Details

ManufacturerSharp
Part numberGP2Y0A21YK0F
Logic/IO voltage5 V
Supply voltage4.5 – 5.5 V
Output voltage0.4 – 2.5 V
Measuring range10 – 80 cm
Response time50 ms
Output current20 mA
PackageModule
Notes / variantsAvailable in multiple distance ranges

  • Ensure proper power supply (4.5 – 5.5 V) to avoid damage.
  • Use a resistor to limit current if necessary.
  • Keep the sensor clean for accurate readings.
  • Be mindful of ambient light interference.
  • Wiring must be secure to prevent intermittent connections.

Wiring Instructions

Arduino wiring for Sharf IR distance sensor
Arduino wiring for Sharf IR distance sensor

To wire the Sharp IR distance module, connect the ground pin of the module to the GND pin on the Arduino. Then, connect the VCC pin of the module to the 5V pin on the Arduino. The signal output pin from the Sharp IR module should be connected to pin 2 on the Arduino, which will read the distance data.

For the buzzer, connect the longer pin (positive) to pin 8 on the Arduino and the shorter pin (negative) to the GND. Make sure all connections are secure and that the components are powered correctly to ensure proper functionality.

Code Examples & Walkthrough

Below is a brief excerpt from the Arduino code where we define the pins and set up the sensor. The identifier IR is defined as the analog pin used for the sensor signal.

#define IR A0 // define signal pin
SharpIR SharpIR(IR, model); // Initialize SharpIR with the defined pin

This code initializes the Sharp IR sensor on the specified analog pin. The identifier model specifies the type of sensor being used, which in this case is set to 1080 for the GP2Y0A21YK0F model.

In the setup function, we start the serial communication to monitor the distance readings:

void setup() {
    Serial.begin(9600); // Initialize serial communication
    Serial.println("Robojax Sharp IR  "); // Print message to serial monitor
}

The setup function initializes the serial communication at 9600 baud, allowing the Arduino to send distance data to the Serial Monitor for observation.

Within the loop function, we read the distance from the sensor and print it:

void loop() {
    int dis = SharpIR.distance(); // Read the distance
    Serial.print("Mean distance: "); // Print distance to serial monitor
    Serial.println(dis); // Output the distance measurement
}

This excerpt shows how to continuously read the distance and print it to the Serial Monitor. The variable dis holds the distance value measured by the Sharp IR sensor.

Demonstration / What to Expect

When the setup is complete, you can expect the buzzer to sound whenever an object is detected within the specified range of the Sharp IR sensor. The Serial Monitor will display "Mean distance" along with the measured distance in centimeters. If there are no objects within range, the monitor will indicate that as well (in video at 02:30).

Common pitfalls include ensuring that the sensor is aligned properly and that there are no obstructions blocking the infrared signal. Also, check that the power supply is stable to avoid erratic readings.

Video Timestamps

  • 00:00 - Introduction to the project
  • 02:30 - Demonstration of the sensor functionality
  • 03:45 - Wiring explanation
  • 05:00 - Code walkthrough

Images

Sharpt IR distance sensor GP2Y0A21YK0F
Sharpt IR distance sensor GP2Y0A21YK0F
Arduino wiring for Sharf IR distance sensor
Arduino wiring for Sharf IR distance sensor
Sharpt IR distance sensor
Sharpt IR distance sensor GP2Y0A21YK0F
69-This is the Arduino code and video for a Sharp Infrared Sensor Module
Language: C++
Copied!

Things you might need

Resources & references

Files📁

Arduino Libraries (zip)

Fritzing File