搜索代码

Arduino code and video for Sharp IR distance module with TM1637 display

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.

Sharpt IR distance sensor

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

ManufacturerSharp
Part numberGP2Y0A41SK0F
Logic/IO voltage5 V
Supply voltage4.5–15 V
Output current (per channel)10 mA
Peak current (per channel)60 mA
PWM frequency guidanceN/A
Input logic thresholds0.8 V (low), 2.0 V (high)
Voltage drop / RDS(on) / saturationN/A
Thermal limits–10 to 60 °C
PackagePlastic case
Notes / variantsDistance 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

图像

Sharpt IR distance sensor GP2Y0A21YK0F
Sharpt IR distance sensor GP2Y0A21YK0F
Sharpt IR distance sensor
Sharpt IR distance sensor GP2Y0A21YK0F
68-Sharp IR (infrared) module with TM1637 Display for Arduino
语言: C++
/*
 * Sharp IR (infrared) distance measurement module with TM1637 Display for Arduino
 * Measures the distance in cm.

 * Original library: https://github.com/guillaume-rico/SharpIR
  * Original code for TM1637 from https://github.com/avishorp/TM1637
  
 * Watch video instructions for this code: https://youtu.be/dnjY9n927HI

 * Written by Ahmad Shamshiri on Feb 03, 2018 at 12:32
 * in Ajax, Ontario, Canada. www.robojax.com
 * 

 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
all in one place. 

If you found this tutorial helpful, please support me so I can continue creating 
content like this. 

or make a donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

*/
#include <Arduino.h>
#include <TM1637Display.h>

// Display Module connection pins (Digital Pins)
#define CLK 2 // connect CLK to pin 2
#define DIO 3 // connect DIO to pin 3

// The amount of time (in milliseconds) between display
#define TEST_DELAY   100  
  uint8_t blank[] = { 0x0, 0x0, 0x0, 0x0 };// blank screen

TM1637Display display(CLK, DIO);

///////////////////***** start of Sharp IR
#include <SharpIR.h>

#define IR A0 // define Sharp IR signal pin
#define model 430 // the model of the IR module
// Sharp IR code for Robojax.com
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:
/*
 * GP2Y0A02YK0F --> "20150"
 GP2Y0A21YK --> "1080"
 GP2Y0A710K0F --> "100500"
  GP2YA41SK0F --> "430"
 */

SharpIR SharpIR(IR, model);
/////////////////////**** end of Sharp IR


void setup()
{
  // Sharp IR with TM1637 Display code for Robojax.com
   display.setBrightness(0x0f);// set the brightness at max
}

void loop()
{
  // Sharp IR with TM1637 Display code for Robojax.com
  display.setSegments(blank);// clear the screen
  
  int dis=SharpIR.distance();  // this returns the distance to the object you're measuring

  display.showNumberDec(dis, false, 3, 1);// display the distance
  delay(TEST_DELAY);//delay between display
 // Sharp IR with TM1637 Display code for Robojax.com


}

|||您可能需要的东西

资源与参考

文件📁

Arduino 库(zip 格式)

Fritzing 文件

用户手册