Arduino Code and Video: HC-SR04 Ultrasonic Distance Sensor with TP1637 Display

Arduino Code and Video: HC-SR04 Ultrasonic Distance Sensor with TP1637 Display

In this tutorial, we will explore how to use the HC-SR04 ultrasonic distance sensor in conjunction with the TP1637 display to visually show distance measurements. This project is great for applications like robotic cars and other distance-sensing tasks. By the end of this guide, you will have a working setup that measures distance and displays it on the screen, allowing for easy monitoring of the sensor's readings.

How HC-SR04 ulrasonic sensor work

We will be utilizing two main components: the HC-SR04 ultrasonic sensor and the TP1637 display module. The HC-SR04 sensor emits ultrasonic waves and measures the time it takes for them to bounce back, computing the distance based on that time. The TP1637 display uses I2C communication to show the measured distance in a user-friendly format. For additional clarity, you can refer to the video at 1:30 for a visual explanation of the components.

Hardware Explained

The HC-SR04 ultrasonic sensor has four pins: VCC, Trig, Echo, and GND. The VCC pin connects to the power supply, while the GND pin connects to ground. The Trig pin is used to trigger the ultrasonic pulse, and the Echo pin receives the reflected pulse. This sensor can measure distances ranging from 2 cm to 400 cm.

The TP1637 display module consists of four pins as well, which are used for I2C communication. These pins include VCC, GND, Clock (CLK), and Data (DIO). In this project, the CLK pin is connected to digital pin 2 on the Arduino, and the DIO pin is connected to digital pin 3. This display makes it easy to visualize the distance readings from the ultrasonic sensor.

Datasheet Details

ManufacturerTM1637
Part numberTM1637
Logic/IO voltage3.3 V to 5.5 V
Supply voltage5 V
Output current (per channel)~25 mA
Peak current (per channel)~50 mA
PWM frequency guidanceN/A
Input logic thresholds0.3VCC max
Voltage drop / RDS(on) / saturationN/A
Thermal limits85 °C
PackageDIP-16
Notes / variantsCommonly used in 7-segment displays

  • Ensure proper connections to avoid damage.
  • Maintain a maximum supply voltage of 5 V for safe operation.
  • Use appropriate pull-ups for I2C communication if needed.
  • Keep the distance measurement range within the specified limits (2 cm - 400 cm).
  • Avoid shorting the power and ground pins.
  • Test the display brightness settings to ensure visibility.
  • Check for floating inputs that may lead to inaccurate readings.

CLK = clock, DIO = data output

Wiring Instructions

Arduino wiring for HC-SR04 Ultrasonic Sensor with TM1637 display
Arduino wiring for HC-SR04 Ultrasonic Sensor with TM1637 display

To wire the HC-SR04 ultrasonic sensor, connect the VCC pin (red wire) to the 5V pin on the Arduino, and the GND pin (yellow wire) to the ground. The Trigger pin (blue wire) should be connected to digital pin 12, while the Echo pin (green wire) should be connected to digital pin 11. Make sure these connections are secure to ensure proper operation.

For the TP1637 display, connect the VCC pin (red wire) to the 5V pin on the Arduino and the GND pin (yellow wire) to the ground. The Clock pin (blue wire) should go to digital pin 2, and the Data pin (white wire) should connect to digital pin 3. This setup allows the Arduino to communicate with the display and send distance data for visualization.

Code Examples & Walkthrough

The following code excerpt initializes the ultrasonic sensor and display:

#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
TM1637Display display(CLK, DIO);

Here, we define the pins for the trigger and echo of the ultrasonic sensor and set the maximum distance to 200 cm. The NewPing library is used to handle the sensor, while the TM1637Display object manages the display.

In the main loop, we continuously read the distance and display it:

int distance = sonar.ping_cm(); // get distance
display.showNumberDec(distance, false, 3, 1);

This part of the code retrieves the distance measurement from the sensor and shows it on the TP1637 display. The showNumberDec function formats the distance to be displayed as a decimal number.

For the full code, please refer to the section below the article.

Demonstration / What to Expect

Once everything is set up and the code is uploaded, you should see the distance readings displayed on the TP1637 display. The readings will update approximately every 50 milliseconds, providing near real-time feedback on the distance measured by the ultrasonic sensor. If you notice any unexpected values, ensure the sensor is pointed correctly and that there are no obstructions in its path (in video at 6:15).

Video Timestamps

  • 00:00 - Introduction
  • 01:30 - Component overview
  • 02:45 - Wiring instructions
  • 04:00 - Code explanation
  • 06:15 - Demonstration

الصور

HC-SR04 ulrasonic sensor
HC-SR04 ulrasonic sensor
How HC-SR04 ulrasonic sensor work
How HC-SR04 ulrasonic sensor work
Arduino wiring for HC-SR04 Ultrasonic Sensor with TM1637 display
Arduino wiring for HC-SR04 Ultrasonic Sensor with TM1637 display
43-This is the Arduino code for the HC-SR04 ultrasonic distance sensor with a TP1637 display.
اللغة: C++
تم النسخ!

الأشياء التي قد تحتاجها

ملفات📁

ملفات أخرى