Arduino code and video for Sharp IR distance module with TM1637 display
In this tutorial, we will explore how to use the Sharp infrared distance module in conjunction with the TM1637 display to measure and visually represent distances. The setup allows you to measure distances accurately and display them in real-time on the TM1637 module. By the end of this guide, you will have a working project that can measure distances and show them on the display.

For this project, you will need to understand the wiring and the code that drives the interaction between the components. The Sharp IR distance sensor communicates the measured distance to the Arduino, which in turn updates the TM1637 display. This tutorial will detail the wiring instructions and provide insights into the code that makes everything work smoothly (in video at 00:00).
Hardware Explained
The main components of this project are the Sharp IR distance module and the TM1637 display. The Sharp IR module uses infrared light to measure the distance to an object by calculating the time it takes for the emitted light to reflect back. It provides an analog output that varies based on the distance measured.
The TM1637 display is a four-digit 7-segment display that is controlled via two pins: a clock pin and a data input/output pin. This display allows for easy visualization of the distance readings from the Sharp IR module, making it user-friendly and practical for various applications.
Datasheet Details
| Manufacturer | Sharp |
|---|---|
| Part number | GP2Y0A41SK0F |
| Logic/IO voltage | 5 V |
| Supply voltage | 4.5–15 V |
| Output current (per channel) | 10 mA |
| Peak current (per channel) | 60 mA |
| PWM frequency guidance | N/A |
| Input logic thresholds | 0.8 V (low), 2.0 V (high) |
| Voltage drop / RDS(on) / saturation | N/A |
| Thermal limits | –10 to 60 °C |
| Package | Plastic case |
| Notes / variants | Distance measurement range: 10–80 cm |
- Ensure proper power supply (4.5–15 V) to avoid damaging the sensor.
- Use decoupling capacitors near the power pins for stability.
- Keep the sensor away from direct sunlight to prevent interference.
- Calibrate the sensor for accurate distance readings.
- Wire the TM1637 display correctly to avoid miscommunication.
- Consider heat-sinking if operating at higher currents.
Wiring Instructions
To wire the Sharp IR distance module and TM1637 display to the Arduino, follow these steps:
Connect the red wire of the Sharp IR module to the 5V pin on the Arduino. Then, connect the black wire to the ground (GND) pin on the Arduino. The yellow wire, which is the output, should be connected to the analog pin A0 on the Arduino. For the TM1637 display, connect the CLK pin to digital pin 2 and the DIO pin to digital pin 3 of the Arduino. This setup allows the Arduino to read the distance from the Sharp IR module and send the data to the TM1637 display.
Make sure all connections are secure to avoid any intermittent issues. If you’re using jumper wires, ensure they are appropriately connected to avoid loose connections.
Code Examples & Walkthrough
The following excerpt shows how the display and sensor are set up:
#define CLK 2 // connect CLK to pin 2
#define DIO 3 // connect DIO to pin 3
SharpIR SharpIR(A0, model); // Initialize Sharp IR sensor
In this segment, we define the pins for the TM1637 display and initialize the Sharp IR sensor by specifying the analog pin A0. This is crucial for the Arduino to know where to read the data from the IR sensor.
The loop function retrieves the distance measurement and displays it:
int dis = SharpIR.distance(); // get distance
display.showNumberDec(dis, false, 3, 1); // display the distance
Here, the distance is stored in the variable dis, and the TM1637 display updates to show this value. The parameters in showNumberDec control how the number is displayed, ensuring it's formatted correctly.
Demonstration / What to Expect
Once you have everything wired and the code uploaded, you should see the distance displayed on the TM1637. As you move an object closer or farther away from the sensor, the display will update in real-time. However, be aware of potential inaccuracies at greater distances, as noted in the video (in video at 12:00).
Common pitfalls include reversed polarity on the power connections and floating inputs on the sensor, which can lead to erratic readings. Make sure to test your setup thoroughly before relying on it for critical measurements.
Video Timestamps
- 00:00 - Introduction to the project
- 01:30 - Wiring instructions
- 03:00 - Code walkthrough
- 05:30 - Demonstration of distance measurement
- 08:00 - Common pitfalls and troubleshooting
الأشياء التي قد تحتاجها
-
أمازون
الموارد والمراجع
-
خارجيSharp IR GP2Y0A51SK0F datasheet (PDF)global.sharp
ملفات📁
مكتبات أردوينو (ملف مضغوط)
-
TM1637 Arduino Library
TM1637_library.zip1.36 MB -
Arduino library for sharp IR distance sensor
application/zip
Fritzing File
-
TM1637 Seven Segment module
application/zip0.01 MB -
sharp ir sensor GP2Y0A02YK0F
application/zip
دليل المستخدم
-
دليل عرض TM1637
robojax-TM1637_display_manual.pdf0.31 MB