[Solved] Ultrasonic readings on seven segment module?

Hi,

Any Idea how to display the reading taken by an HC-SR04 ultrasonic module on a 4 digit seven segment module(TM1637)

I'm using the "TM1637Display.h" library from GitHub and so far successfully tried the example that came along with it.

I use a Arduino nano clone with CH340G.

I'm a total noob. I just want the distance data retrieved by the ultrasonic module to show on the TM1637 module just like it shows up in the serial monitor.

Thanks in advance.

I don't have the library or the example code but if you post the example code for the display we can work out whats being displayed. Once you know whats being displayed its just a simple case of moving the distance from the untrasonic into that spot.

@gpop1

here is the link for the library

here is the example code attached.

also there is this PDF called "User guide for TM1637 4 digits display - Arduino Forum"
i don't know the link, but you get it as the first result if you search google using exact words.

thanks for the reply

TM1637Test.ino (2.05 KB)

with out getting the library it looks like numbers are displayed using

display.showNumberDec(153, false, 3, 1);

you should play with this section of code or read up to find out what it does

if 153 is the number displayed then just move your reading into this spot

Any Idea how to display the reading taken by an HC-SR04 ultrasonic module on a 4 digit seven segment module(TM1637)

Sure - you hook the module and the display up to an arduino and write a sketch to read your device and output to your display.

What's the problem?

Can you not get the numbers off the module? Do you know how the module talks? Using i2c/wire? Hook it to a analog in? Talk to it over serial?

Do you not know know how to convert the reading it gives you into four decimal digits?

Do you not know how to write a four-digit number of your choice to your display?

Is it the overall structure that's bugging you? How about this?

variables to store the reading go here;

void loop() {
  take_reading();
  display_reading();
  sleep(1000);
}

void take_reading() {
  fill in the blank;
}

void display_reading() {
  fill in the blank;
}

Basically, "I haz hardware, how do I write codes?" isn't really specific enough. As to how you go about writing the code, break the job into tasks - reading the sensor and writing to the display, and get those working.

@gpop1

Thanks for the info, in fact as per your suggestion, 153 is the number displayed.

gpop1:
with out getting the library it looks like numbers are displayed using

display.showNumberDec(153, false, 3, 1);

you should play with this section of code or read up to find out what it does

if 153 is the number displayed then just move your reading into this spot

I somehow managed to move my reading into that spot like this.

int x = (sonar.ping_cm());
display.showNumberDec(x,false,3,1);

and guess what, it works...yay.
But now a new problem has arisen, the number shown in the display module is different from what is shown in the serial monitor.

example observation

ser-monitor 7seg-module


88 82
61 32
88 77
87 83
73 74
88 78
57 23
86 74
88 72

I'm attaching my sketch, I dunno what's wrong?

@PaulMurrayCbr

my problem was that i did not know how to get the readings shown in the serial monitor to display on my seven segment module.
now that its somehow solved, a new problem haz arizen. incorrect numbers on display. :confused: please help me out.

Thanks

working4bitssonar.ino (514 Bytes)

It's pointless attaching such tiny amounts of code - just post it and save others the hassle of downloading it.

#include <Arduino.h>
#include <TM1637Display.h>
#include <NewPing.h>

#define TRIGGER_PIN  11  
#define ECHO_PIN     12  
#define MAX_DISTANCE 200

#define CLK 2
#define DIO 3

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
TM1637Display display(CLK, DIO);

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  delay(5000);                   
  Serial.print(sonar.ping_cm());
  Serial.println("cm");

  int x = (sonar.ping_cm());

  display.setBrightness(0x0f);
  display.showNumberDec(x,false,3,1);

}

number shown in the display module is different from what is shown in the serial monitor.

Probably because they're different readings.

AWOL:
Probably because they're different readings.

But how? it takes just one reading every 5 seconds.
I just don't understand why would it have different reading on serial monitor and display module?.

You take two readings every five seconds.
It's right there in your code.
Take one reading right after the delay, assign the value to x, and only print the value of x

Try this (uncompiled, untested)

#include <Arduino.h>
#include <TM1637Display.h>
#include <NewPing.h>

const byte TRIGGER_PIN = 11; 
const byte ECHO_PIN    = 12; 
const int MAX_DISTANCE = 200;

const byte CLK = 2;
const byte DIO = 3;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
TM1637Display display(CLK, DIO);

void setup()
{
  Serial.begin(115200);
  display.setBrightness(0x0f);
}

void loop()
{
  delay(5000);                   
  int x = sonar.ping_cm();
  display.showNumberDec(x,false,3,1);
  Serial.print(x);
  Serial.println(" cm");
}

AWOL:
Try this (uncompiled, untested)

That's it, works like a charm.
You are a life saver.

May I ask why change '"#define" to "const byte"?

once again, Thank you very much for all the help and support.

May I ask why change '"#define" to "const byte"?

Because I prefer it that way.

The other advantage of doing only one reading is that you don't do two back-to back pings and run the risk that the second gets a distant echo from the first ping, and possibly misinterpreting it as being an object closer than is actually the case.

(This library may prevent such an occurrence - I don't know, I've never used it)