I figured that if I was going to end up building some kind of aquarium controller / monitoring system based around the Arduino, I’d need a way of keeping track of time. Either to automate certain events like turning lights and off, or simply to log the time temperature readings we taken, I’d like something more advanced that milli() built in to the Arduino to track time since it was last turned on. Plus, if the power goes out (we lost power for a few hours on Monday after a big storm came through…), then I’d have to tell the Arduino to reset it’s time and calculate afresh when it needed to carry out certain events.
There’s a few basic little circuits built around the DS1307 chip that act as a real time clock – I went with this little kit from Adafruit. It only took 5-10 minutes to assemble and solder, and then I had fun playing with outputting various time formats to the serial monitor and calculating dates in the past and future. Ultimately, I wanted to hook it up to the LCD screen though.
I also got a tiny TMP36 temperature sensor, with the plan being to craft a waterproof housing so it could sit in the aquarium to record water temperature. It only uses one analog pin (in it’s most basic form of connecting), which is great since I’m basing things of the Arduino Uno so don’t have a massive number of pins available. But, with the LCD backpack reducing the numbers of pins required, it’s not too bad. I discovered to output the degrees symbol (°) is written to the LCD using
lcd.print((char)223); or to the serial monitor with
Anyway, after figuring out running both the LCD backpack and the RTC off analog pins 4 + 5, I managed to get the LCD displaying the current time and temperature in both degrees celsius and fahrenheit as shown above. Here’s the actual diagram of the circuit (Fritzing file available via Github below):
It was a pretty cool set up, though it was kinda annoying that there’s nothing built in to Arduino or the RTC library that allows for a leading zero on the time if only a single digit is present, such as 7 seconds when on a clock it should ideally display 07 seconds. A simple
if (now.second() < 10) lcd.print("0"); checks if the value returned from the RTC requires this leading zero and outputs it to the LCD before the actual time value.
The code and circuit schematic for this little project is available on Github at Arduino-LCD-time-temp, and I guess is the first little program beyond basically copying and pasting example code that I've written. It was largely based on the DS1307 RTC tutorial by Ladyada along with her TMP36 temperature sensor tutorial. The Ardiuno forums also had a good few little snippets on how to write out the degrees symbol both to the LCD and to serial when I was testing, and different methods to write out the leading zeros when dealing with time.