Short range measurement and display

Hi everybody,

First time in this forum and mostly first time with Arduino. :slight_smile:

I've done a project, which I would like to improve significantly, but I'm a little stuck. Maybe someone can give advice.

Project task:
The main task of the project, is to have a sensor, that measures distance (0cm ~15cm) in dim-lit area (Maybe even dark/dark (Up to black) surface, if possible). Then display a 2 digit number based on that. Codewise, I could define what numbers are shows, based on how far is the item. Compact and longevity (Battery and weather) is key.

How I have approached, was a very rough unit, because I wanted to check how all works.

I used:
2x Arduino Micros (I'll get there, don't kill me yet)
2x 7-seg display(Each LED seperate PIN)
1x IR sensor (If I remember correctly, it was SHARP 0A41SK)
1x 9V batery.

How mine worked:
One Arduino (A1) measured distance and displayed the second digit and gave some input to the other Arduino (A2), because I was lacking the PIN amount. The "prototype" worked fine, but the measurement wasn't that great the closer it got (Though datasheet warned of this, and dark surfaces).

I got the general idea, how it should work. But is the IR sensor best way of approach? As I'd want it to work in dim light and display accurate readings (Also, compact). In the end, I don't have to use 7-seg display. I thought on maybe O-LED screen even.
And how could I take a long maximum from battery life, as it should work constantly (Or should I make ON/OFF switch)?

Would someone have advice, that I could use? Any and all are welcome :slight_smile:

You could possibly use an LCD display rather than LED, to reduce current consumption.

If I get any of these details wrong don't shoot me but running two separate LEDs using a separate pin for each 'segment' of the LED is not the way to go here (did I understand this correctly?)

The best way to interface with LED 7-segment displays is via SPI or I2C both very easy (the latter only uses TWO wires). That way you'll need just the one Arduino!

That said, using LED 7-segment displays with a 9v battery is also an interesting choice. Are you permanently displaying the number(s)? That will run the battery flat very quickly. An LCD display (with the backlight turned off) could be an option. Or only displaying the number(s) on demand (eg when you - or someone else - pushes a button, for example).

On the IR detector topic, you are right that some detectors have problems with dark surfaces - but I wouldn't have expected an issue at close range (eg a few centimetres). But IR detectors have different ranges, some are designed to work from say, 3cm to 50cm, some from 30cm to 80cm and so on. Using the wrong detector will give sub-optimal results. The detector you mention has a range of 4cm to 30cm so using it below 4cm will probably not be ideal, but you can certainly give it a go.

A few more details of what you're trying to accomplish would be good but in the meantime these comments might get to searching for the right comments (if nothing else my YouTube article on interfacing 7-segment LEDs using I2C may make you rethink your approach!).

Post back if you want more help (and do post more information of what you trying to accomplish).

running two separate LEDs using a separate pin for each 'segment' of the LED is not the way to go here

I'm not currently holding back on any options. Right now, it's using 2 separate LED displays with each pin separate. But using so, I'm losing a lot of PINs there, and 1 Arduino is not sufficient.
Unless I think of a "smart way" to manipulate, that quickly change between the led strips in milliseconds (faster), so human eye cannot understand the difference and then make integer, that it's either first or second digit.

The best way to interface with LED 7-segment displays is via SPI or I2C both very easy

I tried using a larger screen, but my knowledge was limited. But it is something I can consider. If the display is not too wide, guess 1x2 is sufficient. As I only need to show 2 digits.

Are you permanently displaying the number(s)?

I did think on this. As I have thought, I'm thinking to switch to just an ON/OFF switch. As current solution lasted only about 18 hours.

Using the wrong detector will give sub-optimal results.

I did use the 4-30 distance. But I had an anomaly in my use-case. As in raw data, I got too close, the unit16_t value started to "wave" more. It wasn't a straight forward increasing, it was increasing and then decrasing whenever it felt like. As in, I couldn't take it directly, if it's close or actually far. In cm, we're talking approximately 5cm. I didn't go further than 10cm.

Main goal in short, is to have a sensor detect something very close (0-20cm perhaps) in a dim/dark area and dark surface. Have that be worked and accurate, that I can display on LED-s a specific value on each distance.
To create a simple use test (For argument sake), as a reference. I want to display millimeters on the LED display (If we're not talking about 3 digit mm right now). Hope that clears and narrows down a bit :slight_smile:
I have chosen a digital/analog device, as mechanical has wear/tear in time.
Also, right now I focus on "compact is key". As in, the full project should me as flat as possible. And best if it's as fat as the screen.

Longevity in weather? This thing is outdoors?

I would consider switching to an ultrasonic sensor. They seem to work quite well as parking sensors on cars and you can be sure the car designers are very good at outdoor longevity. I don't know anything about the power consumption and 5cm distance capabilities. This suggestion may turn out to useless to you.

An LCD screen will give maximum battery life. They work for years in watches with tiny batteries. E-ink was looking good a few years ago but I haven't seen one for sale in a long time. OLED actually draws a lot of power but it looks good.

MorganS:
...and you can be sure the car designers are very good at outdoor longevity

Yes, car manufacturers do it well but use waterproof ultrasonic devices - also available to the hobbyist but at a price premium of course! Just have a look at what Sparkfun or similar company offers and you will get an idea - I tried this some while ago and was totally put off by the price.

That all said, a standard $2 £1.50 ultrasonic device will probably last a year even in the UK's wet weather so you could argue that you could just replace it annually!!! (They slowly corrode from the inside).

Coincidentally I'm still deciding whether to use ultrasonic or IR for my next project which is a water-softener salt-level monitor. The ultrasonic device will undoubtedly corrode in the salty confines of a water softener but I would follow my own advice above in that case. But IR may prove to be better - I just need to test out both devices.

GanDuumar:
Right now, it's using 2 separate LED displays with each pin separate. But using so, I'm losing a lot of PINs there, and 1 Arduino is not sufficient.

Quite so, that's why using I2C to communicate with an LED device using a MAX2719 is the way to go - trust me, it really is simple. Look at my Youtube videos #9, #10 (and even #11 if you're interested in improving your C++ code)

GanDuumar:
Unless I think of a "smart way" to manipulate, that quickly change between the led strips in milliseconds (faster), so human eye cannot understand the difference and then make integer, that it's either first or second digit.

Sorry, not quite with you on this one. Can you just rephrase this so I get a better idea of what you mean by this, especially the led "strips" (digits?) and changing quickly?

As always, happy to help.

I've used SPI with MAX7219, default clock speed of 4MHz works great.
Are you sure about I2C? Datasheet doesn't say anything about supporting that.

CrossRoads:
I've used SPI with MAX7219, default clock speed of 4MHz works great.
Are you sure about I2C? Datasheet doesn't say anything about supporting that.

Hah! When I wrote that I was looking at a old project lying on my workbench which had the LED MAX7219 unit connected but (doh!) it was connected via SPI and the OLED display was connected via I2C all to the same UNO - well, it was dark(ish) and I just didn't look hard enough (or stop to think).

So MAX7219 obviously is only for SPI (still just three pins though) and OLEDs via I2C (just two wires).

I just don't quite understand the OP using all his pins connecting each segment of an LED to the UNO, that must be horrendous to work with. 14/16 wires plus a common anode or cathode! No wonder he ran out of pins, if that's what he has really done.

Longevity in weather? This thing is outdoors?

This thing is withing a box, that is exposed to outdoors. It does not measure outside from the box, but inside. It has a few cracks, where water might drip in. Also cold and warmth can touch it, as the box is metal (Black coloured).

I just don't quite understand the OP using all his pins connecting each segment of an LED to the UNO, that must be horrendous to work with.

It was something I wanted to test, on how to display 2 digit numbers. I didn't have any displays around, so I went with something I had. Horrendous yes, but it "worked". But something I do not want to do

...to the UNO, ...

I'm aiming to use Arduino Micro, but I guess they are quite same. Just wanted to clear it.

I've used SPI with MAX7219, default clock speed of 4MHz works great.

I actually got my hands on MAX7219. But couldn't grasp the whole subject on code part. Also seemed to bit a overkill? I only need 2 digits and don't extend further then say number 50.
I want it to be most compact as possible.

I totally understand your desire to just get things working (in principle) and then refine a working solution.

On the subject of LEDs, however, a MAX7219 with 8 digits on it would be overkill if all you need to display is two digits. But you could connect your own two-digit Led displays to a MAX7219 (see the datasheet it's quite straightforward) and this would make your wiring (and coding) so MUCH easier! People here can help with the coding (or see my YouTube Arduino article on this very subject).

An alternative would be a 16x2 OLED (or, to reduce power even further) just a 16 x 2 LCD which are very cheap these days because everyone wants the OLED version! Not as compact as two LED digits but much easier to program (via I2C anyway).

Just suggestions, you must make your own judgement on what suits you best. Keep asking those questions and you will get the solution you want and need.

An alternative would be a 16x2 OLED

I thought on OLED version. But what's the smalled OLED (That's compatible and simple with Arduino Micro)? Also, which endures more elements? I will cover them, to avoid water and such. More of a issue is heat and cold (Say, winter and summer).

But you could connect your own two-digit Led displays to a MAX7219

It is a thought that went thought my mind. But thought maybe there are more reasonable solutions.

One thing I checked out, was lazer rangefinder. But those seemed like 3x the Arduino Micro size, if next to each other. Any experience with those?

I'm afraid I have no experience of lazer rangefinders - but that doesn't mean that others have not already experimented.

You say you need a small (compact), weather-resistant solution - I would definitely go with either a home-built dual LED display with a MAX7219 which you could easily build on stripboard, or a small 16x2 LCD (not OLED, they are more power hungry). Any putting all of it into a transparent weather-proof box would be a good idea.

I don't know what you're trying to do with your project so I'm working a bit blind, do you want to explain the whole idea so I can have a bit more to work with?

You say you need a small (compact), weather-resistant solution - I would definitely go with either a home-built dual LED display with a MAX7219 which you could easily build on stripboard, or a small 16x2 LCD (not OLED, they are more power hungry). Any putting all of it into a transparent weather-proof box would be a good idea.

I think this is actually the best result I can take.

I though I'll go with either taking a small IR unit (0-15 cm) and test it (Just bought it). If this does not give correct results, I also have bought a simple IR LED (Can't remember specs, hope it's good) and an "Integrated IR receiver 38khz", which I hope is enough. I'll put them on board and a small wall between them. Using 2x 7-seg display and the MAX7219, I hope I'll get my results. I got some specs, and I'll be usin an C type battery. I'll seal it tight with glue I guess and leave compartment for battery (Seal it good).

But is there any specific way the IR must be protected from water, or is just glass or something good? Meaning that it wont eat the IR readings? One would be just to ofcourse leave it outside extruded.

Also, does Arduino Micro have some kind of special sleep mode that I can use? That say, I have LEDs switched off. It only reads the IR. Once IR distance changes, it wakes up and shows on the LEDs the number I specified for each range? I'd like to maximize the longevity of the battery.
I found some tips on it, that after every 5 seconds, a IR would check. But I would do max 1 second, and would that save me a lot of power?

Maybe totally different is the use of an ATtiny board. ATtiny 25/45/85 if you really want the smallest footprint, ATtiny 24/44/84 if you want a few more pins. You can't get much smaller than that.

Combine with a port expander (I2C, SPI, 74HC595, ...) and maybe a few transistors to drive the current display (if you want to stay with that). That display does not always have to be on.

If your IR sensor works reliably and accurate in the current setup, there is no need to change that (except for number of pins?). I've just been playing with a HC-SR04, but accuracy is not the most brilliant. With a steady object at e.g. 20cm, it mostly gives 20cm but sometimes all at a sudden an odd 21cm. I might be doing something wrong though as I only have played a few hours.

I don't know what your project exactly needs to do. Can you use polling to only do measurements at given times (e.g. every second?). In that case sleep modes will definitely help: do measurement, switch display 'on', display value, switch display 'off' and go back to sleep; add button to switch display 'on' if you want to see the measurement.

GanDuumar:
I also have bought a simple IR LED (Can't remember specs, hope it's good) and an "Integrated IR receiver 38khz", which I hope is enough.

These won't be useful for distance measurement. They're intended for remote-control applications.
The LED needs to be driven with a modulated 38kHz carrier, and the receiver is tuned to demodulate that 38kHz and give a digital output of a remote control code. Since the output is digital and not analogue, there's no way to use received signal strength as an indication of distance.

You could just use a single arduino for both your 7 segment displays.

Connect the same segments of each display to each other. Instead of connecting the ground pin from the LEDs to ground , connect this to another output pin of your Arduino. so, 7 pins for the Segments, and 2 pins for ground. 9 pins in total.

When you show the display, quickly switch between the two numbers, setting the ground pin to low on the display you want to show.

tonywestonuk:
Instead of connecting the ground pin from the LEDs to ground , connect this to another output pin of your Arduino. so, 7 pins for the Segments, and 2 pins for ground. 9 pins in total.

If this is done directly, it's likely to blow the Arduino I/O pins switching the common-cathode of the 7-segment displays. An Arduino pin can only source/sink an absolute maximum of 40mA, and the safe level is considerably lower - 20mA to 30mA.
Transistors would be needed to interface between the Arduino output pins and the displays.