Thursday, 9 November 2017

A close shave

Last night, I thought about the fact that the new display module I have been sent, with its driver circuitry, only uses 4 pins rather than the 12 I was expecting on the naked 4 digit LED.  That means that I can connect the display module, the HC-SR04 ultrasonic sensor, AND the DS3231 RTC to the Uno all at the same time (and still have pins to spare).  By shuffling things up a bit on my breadboard, I could fit the RTC module alongside the ultrasonic module, so that a) it has a semi-permanent home and I don't have to worry about it getting shorted out any more, and b) I can use the display for either the clock or the rainwater tank gauge just by reloading the required sketch.  So I set to, and did just that.

Unfortunately, the combination of my poor eyesight, low light in the evening, and trying to line up small cables with small holes means - inevitably - I did something wrong - with style and panache!  Actually, it turns out I did a couple of somethings wrong, but please, read on...

Eager to see if the RTC was still keeping time after a week in its little bag, I plugged it all in and reloaded my little clock sketch.  Nothing... Zip... Nada! Well - what a let down.  So, hoping I had simply dislodged a wire to the display, I reloaded the tank gauge sketch as a test, but that worked OK.  I tried the clock again, but still nothing.  So it must be the RTC wiring or the RTC itself

I decided to pull out the RTC and the DuPont cables to it and start again.  I grabbed the RTC and burned my thumb on the battery - it was very hot!  I figured this meant I had a power problem and quickly found that I had reversed the Vcc and GND connections to the RTC board...  I'm really surprised I hadn't buggered up everything else on the breadboard - my Uno included! 

Anyway - I removed the battery and let everything cool down and swapped the connections back the way they should be, then tried again.  This time, the display came back to life (whoopee!), but I was a little disappointed and somewhat surprised to see the time was 13:65! 

I guess that just maybe, reversing the polarity and almost setting fire to the module may have had a slightly adverse affect on the data - possibly - so I reloaded the set time sketch, reset the time and tried again.  It was still showing 13:65 - weird...  Uh oh - I started to get that feeling of dread again - you know, the one I got just seconds after hitting the button to buy a Pro Mini that I realised I knew nothing about..?  After the first RTC I bought being DOA, perhaps I've now killed this RTC as well.

I reloaded the tank gauge sketch, just to make sure I hadn't fried the Uno as well, but that seemed to be working OK.  I tried the clock once more - and the time was still 13:65 - DAMMIT!  What was worse, was that the time display seemed static - even after 5 minutes (carefully keeping an eye on the temperature of the battery), it was still apparently 13:65 - I kind of hoped it might tick over each minute and show 13:70.

Well - I had removed and reseated the RTC module, and the Vcc/GND wires had been swapped, but I hadn't touched the SDA and SCL cables yet... Yes, you've guessed it - I had them round the wrong way too!  Good grief, just how much wronger (yes - its a word) could I possibly have got this?  After swapping those two around, and running the set sketch again, the display finally shows the correct time.  Just to make sure, I pulled the rest of the clock data (YYMMDD, DoW, and hhmmss) and displayed them on the Serial console - phew - all seems ok.  I haven't destroyed the chip after all, despite my best efforts.  Mind you, having abused the battery like that, I'm not sure how long that is going to last now.  It has kept the RTC going for around 24 hrs at the time of writing this, and the time is still correct to within about a second.  I'm just counting my blessings, and vow that I am not going to touch that module again until it goes into its permanent home on my portable workbench.

Lesson learned?  Accept that I'm getting older and my eyesight ain't what it used to be.  Don't struggle in dim light where you can't see the difference between a grey and lilac cable, or manage to line up the red and black cables with the appropriate pins on the Uno... TURN SOME LIGHTS ON!


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.

Tuesday, 31 October 2017

A fortuitous ebay mistake

Today, my ultrasonic sensor arrived with the 4 digit display.  BUT - instead of the bare 4 digit display, they sent me a display module with a 4 pin interface, which is good, but it is a clock module with a colon in the middle, rather than normal digits - each with a decimal point.
After a little digging around, I have found a library to drive the module and figured out how to use it.  As it happens - since I wouldn't need a decimal point, I could use this module in the water tank gauge project, but I have already pointed out the mistake to the seller, in the hope he will send me a new display of the type shown in the ebay listing.

But as I now have an LED display geared up for a clock type display, I thought - why not pair it up with my new RTC module and have a little clock.  Ok - so it only shows hours and minutes, but it will help me to test the longevity of the 2032 battery in the DS3231.

Here is my code for the clock

 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <DS3231.h>
#include <TM1637Display.h>


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

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

// Initialise RTC & RTC variables
DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

// other variables
long counter;
uint8_t mask = 0b00010000;


void setup() {

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

// set brightness of LED
  display.setBrightness(0x0f);

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

// store current millis()
  counter = millis();
}

void loop() {
// get Time from RTC
  int hrs = Clock.getHour(h12,PM);
  int mins = Clock.getMinute();

  if (hrs > 12) hrs -= 12;
  
// display the hours with colon enabled/disabled as each second passes
  // mask for colon on digit 2
  // 0x80>>3 = 0b00010000 (I have no idea WHY this works!)

  if (millis() - counter > 1000) {
    if (mask == 0b00010000) {
      mask = 0b00000000;
    } else {
      mask = 0b00010000;
    }
    counter = millis();
  }
  display.showNumberDecEx(hrs,mask,false,2,0);

// display the minutes
  display.showNumberDec(mins,true,2,2);

}

I have tried forcing the hours to am/pm mode, as the RTC always seems to show it in 24 hour format (I expect there is a way to do it using the library functions, but I didn't find it yet), and then apply leading zero suppression to the hours, so that it shows as 9:00 for example, rather than 09:00.  However, I'm not sure what'll happen between midnight and 1am - perhaps the hour will not show at all...?  I won't be awake to see it though - at least not tonight!

I have no clue why the mask that controls the colon being displayed works... it is a bitwise shift right by three places, but by my reckoning, as the left most bit in the mask is a shift of 0, then the mask should be using a shift of either 1 or 2 places (depending if the colon is linked to the right side of digit 2 or the left side of digit 3.  But - I got it working by trial and error, and have even managed to make it flash every second.

Tomorrow, hopefully, I may start playing with the ultrasonic sensor unit.  Exciting!!

Update - 2 days later:
The RTC is still holding the time well.  I did originally have some issues with it randomly resetting itself, but I think was down to my dodgy prototyping with modules just hanging off the Uno on the end of DuPont cables - things were possibly touching other things and shorting out, etc.  After I blu-tacked all the modules down on a base board - no more problems.
By the way - even with leading zero suppression, times between midnight and 1 o'clock DO show with a single zero for the hour, as I hoped.

Update - 3 weeks later:
It is still holding the time, but given that my display is only hours and minutes, I haven't confirmed just how accurate the time is.  But the fact it still has a valid time that is even roughly correct is a big win.