Thursday, 2 November 2017

Water Tank Depth Gauge - 3 (The Prototype)

Last night, I dismantled my clock (carefully placing the DS3231 RTC in a bag to prevent any accidental short-circuiting), and cobbled together the Uno, HC-SR04 ultrasonic sensor, and my (clock oriented) 4 digit LED, to make a very nerdy looking ultrasonic tape measure (Dr Who - eat your heart out - sonic screwdriver, pah!).


I kept the code very basic (in fact, it is less than 50 lines of code including comments and spacing), and this quick and dirty version just shows the distance measured in cms on the display.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <TM1637Display.h>
#include <NewPing.h>

// LED Module connection pins (Digital Pins)
#define CLK 9
#define DIO 8

// pins for HC-SR04
#define pinTrig 6
#define pinEcho 7
NewPing sonar(pinTrig, pinEcho, 400); // 400 is max claimed ability, though it actually keeps going till around 450

// Initialise LED
TM1637Display display(CLK, DIO);


void setup() {
// setup the sonar pins
  pinMode(pinTrig, OUTPUT);
  pinMode(pinEcho, INPUT);

// turn off annoying LED on 13
  pinMode(13, OUTPUT);
  digitalWrite(13,LOW);

// set brightness of LED 
// (0-7,true/false) where 0=min 7=max (0-3 have greatest impact), true=on and false=off
  display.setBrightness(1,true);

// set all segments off for all digits
  uint8_t data[] = { 0x00, 0x00, 0x00, 0x00 };
  display.setSegments(data);
}

void loop() {
  // Take 10 readings, and use median duration.
  int duration = sonar.ping_median(10);  // duration is in microseconds

  // Convert the averaged delay time to distance (water surface to sensor).
  int cms = duration / 29 / 2;  // speed of sound is 29 microsecs per cm (x2 for round trip)

  // display distance on LED
  display.showNumberDec(cms,false,4,4);
  
}


I did a few random measuring experiments, and was quite impressed with the apparent accuracy!  I haven't yet set up a test rig to check measurements across the entire range, but I did a few spot checks and found it to be within 1-2 cms at distances up to a couple of metres, though I have noticed occasional sporadic and momentary wild readings.  For example, while writing this, the sofa across the other side of the room is apparently 3.44 metres away - most of the time, but once in a while, just for a split second, it leaps forwards to 3.01 or even 2.88 metres, and immediately jumps back again!  Perhaps I shouldn't have made that comment about Dr Who... I think he is getting his own back, by playing around with my interspatial dimensionality continuum :-/  Or maybe it is just soft furnishings don't give a solid enough echo...

The ebay seller has agreed to send me a new display unit (he thinks his 'worker' has made a mistake), and says he will find a way to get the replacement to me quickly so I don't have to wait another 3-4 weeks.  Now I just hope that his 'worker' has actually understood the mistake, and doesn't just send the same thing again.  Oh well, if he does, then I'll just use it to display the 3 digit number and have a spare clock display I can mount onto my portable workbench.

No comments:

Post a Comment