# Trinket RGB Shield Clock

## Overview

This project was inspired by a forum member who asked a simple question: Can you interface a Trinket mini microcontroller to the Adafruit RGB LCD Shield. The shield is made to interface with more "classic" Arduino microcontrollers with an Arduino standard shield pin layout. Obviously the shield cannot stack onto Trinket but with four wires, the display shield can hook up to a Trinket project well. This is accomplished as both use the I2C or two-wire bus to communicate. As a further demonstration, the Adafruit I2C based DS1307 real-time clock module is used to display the time and date. The display shield's buttons allow for changing the hour in case of daylight savings time and toggle the backlight.

![](https://cdn-learn.adafruit.com/assets/assets/000/013/678/medium800/trinket_Layout-Pic-Crop.jpg?1390336302)

Info: 

## Libraries

The project uses three code libraries:

- Wire.h&nbsp;- the Arduino standard library for I2C/two wire communication (already installed with the Arduino IDE)
- [RTClib](https://github.com/adafruit/RTClib "Link: https://github.com/adafruit/RTClib") - Used to communicate with the DS1307 real-time clock
- [Adafruit\_RGBLCDShield](https://github.com/adafruit/Adafruit-RGB-LCD-Shield-Library "https://github.com/adafruit/Adafruit-RGB-LCD-Shield-Library")&nbsp;- communicates with the Adafruit RGB LCD Shield

You may click on the links&nbsp;to download from the Adafruit Github repository.  
  
These should be installed in your Arduino folder where your sketches are stored in the libraries folder. For an in-depth discussion of installing and managing Arduino libraries, see tutorial [All About Arduino Libraries](http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use).

## Getting Ready

If you use the Arduino IDE version 1.6.5 or later, you can download the Adafruit board file in the Tools -\> Board -\> Board Manager. &nbsp;You will then be able to select "Trinket 8 MHz" as a board and all is well.

Danger: 

# Trinket RGB Shield Clock

## Hook-up

![](https://cdn-learn.adafruit.com/assets/assets/000/013/673/medium800/trinket_RGB-Clock-Fritzing.jpg?1390329166)

Since we're using I2C for the shield and real time clock, hookup is fairly straightforward. Don't forget, I2C allows you to use multiple devices on two shared pins, perfect for when you don't have a lot of pins like the Trinket!  
  
1) Assemble the RGB LCD Shield per [this tutorial](http://learn.adafruit.com/rgb-lcd-shield)  
2) Assemble the DS1307 clock per [this tutorial](http://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit "Link: http://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit")  
3) Solder supplied male headers to Trinket  
  
**Wiring**  
  
All components are hooked to 5 volts and ground. **Note the ground pin used for the shield** (the ground pin next to the 5 volt pin will NOT work, it isn't connected!)  
  
Hookup of the display is easier with some [male-female extension wires](http://www.adafruit.com/products/824 "Link: http://www.adafruit.com/products/824") especially if the shield has male headers already soldered on. For breadboarding this allows the display to just plug in.

![](https://cdn-learn.adafruit.com/assets/assets/000/013/676/medium800/trinket_Shield-side.jpg?1390335798)

The clock and display are connected to Pin 0 and Pin 2 on Trinket. Pin 0 is the I2c data line (SDA). Pin 2 is the I2C clock line (SCL). Both the display and the clock can share this bus as they each have a different address assigned during manufacture. The sharing of the bus saves pins in a Trinket project. Use of the buttons on the LCD shield is also through the I2C bus, again saving pins.   
  
If an LCD display with I2C backpack were substituted for the shield, other data pins on Trinket could be used for buttons. Use of an I2C expander chip such as the [MCP23008](http://www.adafruit.com/products/593 "Link: http://www.adafruit.com/products/593") or [MCP23017](http://www.adafruit.com/products/732 "Link: http://www.adafruit.com/products/732") to gain an additional 8 or 16 pins respectively.  
  
You can easily fit the parts for permanent mounting on a [half perma-proto board](http://www.adafruit.com/products/1609 "Link: http://www.adafruit.com/products/1609"), perhaps a [quarter perma-proto board](http://www.adafruit.com/products/1608) with some moving of parts. If you decide on a permanent mount, I suggest use of [female header](http://www.adafruit.com/products/598) to mount the Trinket, perhaps even the clock module, in case the part has issues you can quickly swap it out.

# Trinket RGB Shield Clock

## Code

Two programs are used to save code space. The first one should be needed only once to set the battery-backed DS1307 real-time clock. It may also be needed if the battery runs out but the battery life is expected to be quite long, like 7 years or so. It sets the clock according to the Arduino date and time so the system clock on your computer should be accurate for this to set correctly.

```auto
/*********************

Sketch to set the time and date for the DS1307 Real Time Clock
  with an Adafruit Trinket mini microcontroller

**********************/

// include the library code:
#include &lt;Wire.h&gt;
#include &lt;RTClib.h&gt;

RTC_DS1307 RTC;

void setup() {
  RTC.begin();
  if(!RTC.isrunning()) {
     RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop() {
}
```

The main clock code is below.

```auto
/*********************
Trinket RGB LCD Shield Clock

Example code for the Adafruit RGB Character LCD Shield and Library
for Trinket 

The DS1307 Real Time Clock must be initialized with a separate sketch.

Version 2.0 Use with Arduino IDE Version 1.6.5 or later and Adafruit 
   libraries modified December 2015 or later.  Mike Barela for Adafruit.
**********************/

// include the library code:

#include &lt;Wire.h&gt;
#include &lt;Adafruit_RGBLCDShield.h&gt;  // RGB LCD Shield communications
#include &lt;RTClib.h&gt;                 // DS1307 clock communications

// These defines make it easy to set the backlight color
#define OFF 0x0
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7

// The shield uses the I2C SCL and SDA pins. 
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

RTC_DS1307 RTC;      // Establish clock object
DateTime Clock;      // Holds current clock time
int8_t offset = 0;   // Hour offset set by user with buttons
uint8_t backlight = WHITE;  // Backlight state

void setup() {
  lcd.begin(16, 2);         // initialize display colums and rows
  RTC.begin();              // Initialize clock
  lcd.setBacklight(WHITE);  // Set to OFF if you do not want backlight on boot
}

void loop() {
  uint8_t buttons;                       // button read value
  uint8_t hourval, minuteval, secondval; // holds the time
 
  DateTime Clock;                    // variable to hold our time
  char* colon = ":";                 // static characters save a bit 
  char* slash = "/";                 //   of memory
  
  Clock = RTC.now();                 // get the RTC time
  
  hourval = Clock.hour()+offset;     // calculate hour to display
  if(hourval &gt; 23) hourval-=24;      // adjust for over 23 hour
  else if(hourval &lt; 0) hourval+=24;  //   or under 0 hours
  
  minuteval = Clock.minute();        // This block prints the time
  secondval = Clock.second();        //  to the LCD Shield
  lcd.setCursor(0,0);
  if(hourval &lt; 10) printzero();      // print function does not print
  lcd.print(hourval);                //   leading zeros so this will
  lcd.print(colon);
  if(minuteval &lt; 10) printzero();
  lcd.print(minuteval);
  lcd.print(colon);
  if(secondval &lt; 10) printzero();
  lcd.print(secondval);

  buttons = lcd.readButtons();  // read the buttons on the shield

  if(buttons!=0) {                     // if a button was pressed
      if (buttons &amp; BUTTON_UP) {       // if up pressed, increment hours
         offset +=1;
       }
      if (buttons &amp; BUTTON_DOWN) {     // if down pressed, decrement hours
         offset -=1;
       }
      if (buttons &amp; BUTTON_SELECT) {   // if select button pressed
         if(backlight)                 // if the backlight is on
            backlight=OFF;             //   set it to off
         else                          // else turn on the backlight if off 
            backlight=WHITE;           //   (you can select any color)
         lcd.setBacklight(backlight);  // set the new backlight state
      }
   }
  lcd.setCursor(0,1);                  // This block prints the date
  if(Clock.month()&lt;10) printzero();    //   to the LCD Shield
  lcd.print(Clock.month());
  lcd.print(slash);
  if(Clock.day()&lt;10) printzero();
  lcd.print(Clock.day());
  lcd.print(slash);
  lcd.print(Clock.year());
  
  delay(1000);  // wait one second
}

void printzero() {  // prints a zero to the LCD for leading zeros
  lcd.print("0");   // a function saves multiple calls to the print function
}
```

The code displays the clock value and polls the buttons. If the up or down buttons are pressed, the value _offset_ is incremented/decremented. This is added to the RTC clock time to form the hour. A more robust program would have the hour written back to the DS1307 but that one function takes about 300+ bytes of code which is too much for our mighty Trinket.

# Trinket RGB Shield Clock

## Use and Going Further

# Use It!
Supply 5 volts to the 2.1mm jack via a wall supply, or use a USB wall adapter and a long USB cable. Remember that initially the real-time clock module will not be set, if you have never programmed it. Run the first program to set the clock! The coin cell battery will provide timekeeping for 7 years.  
  
Load the main program. You should see the time and date on the display after the 10 second bootloader time. If not, check your connections.  
  
You can use the UP and DOWN buttons to adjust the hour for daylight savings time.  
  
You can use the SELECT button to turn on or off the LCD backlight (useful for night time use). If you want a different color display, the RGB shield provides 8 different colors noted in the code.  
  
Since the buttons are only polled every second, the buttons may not seem to register easily. Press and hold for a second and the desired function should happen.   
## Going Further
The combination of Trinket and the RGB LCD Shield is a good combination for display and input. There is enough code space to hook a number of sensors for real-time readout. If you believe the shield form factor is not ideal, use of the LCD with the I2C backpack is a good combination. See the tutorial for the [Trinket Ultrasonic Rangefinder](http://learn.adafruit.com/trinket-ultrasonic-rangefinder) as an example.  
  
The combination of the Trinket, display, and DS1307 clock makes for some trade-offs for projects. The amount of code space is limited to 5,310 bytes so you can easily go over if you include too many display or clock function calls. Even updating the real-time clock with RTC.adjust takes precious code, hence the use of an offset in the program here. The same with alarm functionality. Code optimization is crucial: use of floating point numbers is probably not possible. Use of signed integers even adds code. If you have heavy code, moving up to Arduino Uno might be necessary. Trinket provides a significant benefit in terms of cost and size if your project can fit in the code space available.  
  
If you want a more precise clock, [you can swap the DS1307 for a Chronodot](http://www.adafruit.com/products/255), its code-compatible and is ultra-precise!  

## Featured Products

### Adafruit Trinket - Mini Microcontroller - 5V Logic

[Adafruit Trinket - Mini Microcontroller - 5V Logic](https://www.adafruit.com/product/1501)
 **Deprecation Warning: The Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Trinket so that people can maintain some older projects, we no longer recommend it.** <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1501)
[Related Guides to the Product](https://learn.adafruit.com/products/1501/guides)
### RGB LCD Shield Kit w/ 16x2 Character Display - Only 2 pins used!

[RGB LCD Shield Kit w/ 16x2 Character Display - Only 2 pins used!](https://www.adafruit.com/product/714)
This new Adafruit shield makes it easy to use a 16x2 Character LCD. We really like the RGB LCDs we stock in the shop both the [RGB negative](http://www.adafruit.com/products/399) and [RGB positive](http://www.adafruit.com/products/398). Unfortunately, these LCDs do...

Out of Stock
[Buy Now](https://www.adafruit.com/product/714)
[Related Guides to the Product](https://learn.adafruit.com/products/714/guides)
### DS1307 Real Time Clock breakout board kit

[DS1307 Real Time Clock breakout board kit](https://www.adafruit.com/product/264)
**[We've upgraded this RTC breakout and made it even easier to use! Now available as a fully assembled board, it has the same components, chip, size, etc but you don't have to put it together. It's also less expensive! Check out...](https://www.adafruit.com/product/3296)**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/264)
[Related Guides to the Product](https://learn.adafruit.com/products/264/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Solid-Core Wire Spool - 25ft - 22AWG - Black

[Solid-Core Wire Spool - 25ft - 22AWG - Black](https://www.adafruit.com/product/290)
Perfect for bread-boarding, free wiring, etc. This spool of solid-core wire is easy to solder to. When bent it keeps its shape pretty well. We like to have a few spools of this stuff around. We suggest picking up wire strippers to match. Wire gauge is 22 AWG which we've found is the best...

In Stock
[Buy Now](https://www.adafruit.com/product/290)
[Related Guides to the Product](https://learn.adafruit.com/products/290/guides)
### Solid-Core Wire Spool - 25ft - 22AWG - Yellow

[Solid-Core Wire Spool - 25ft - 22AWG - Yellow](https://www.adafruit.com/product/289)
Perfect for bread-boarding, free wiring, etc. This spool of solid-core wire is easy to solder to. When bent it keeps its shape pretty well. We like to have a few spools of this stuff around. We suggest picking up wire strippers to match. Wire gauge is 22 AWG which we've found is the best...

In Stock
[Buy Now](https://www.adafruit.com/product/289)
[Related Guides to the Product](https://learn.adafruit.com/products/289/guides)
### Solid-Core Wire Spool - 25ft - 22AWG - Red

[Solid-Core Wire Spool - 25ft - 22AWG - Red](https://www.adafruit.com/product/288)
Perfect for bread-boarding, free wiring, etc. This spool of solid-core wire is easy to solder to. When bent it keeps its shape pretty well. We like to have a few spools of this stuff around. We suggest picking up wire strippers to match. Wire gauge is 22 AWG which we've found is the best...

In Stock
[Buy Now](https://www.adafruit.com/product/288)
[Related Guides to the Product](https://learn.adafruit.com/products/288/guides)
### Premium Female/Male 'Extension' Jumper Wires - 40 x 12" (300mm)

[Premium Female/Male 'Extension' Jumper Wires - 40 x 12" (300mm)](https://www.adafruit.com/product/824)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 12" (300mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on one end and 0.1" female header contacts...

In Stock
[Buy Now](https://www.adafruit.com/product/824)
[Related Guides to the Product](https://learn.adafruit.com/products/824/guides)

## Related Guides

- [Lie Ren's Stormflower Gun Blade](https://learn.adafruit.com/stormflower-gun-blade.md)
- [3D Printed Adabot Figurine](https://learn.adafruit.com/3d-printed-adabot-figurine.md)
- [Trinket / Gemma IR Control](https://learn.adafruit.com/trinket-gemma-ir-remote-control.md)
- [NeoPixel Bracelet](https://learn.adafruit.com/neopixel-bracelet.md)
- [Trinket “Question Block” Sound Jewelry](https://learn.adafruit.com/trinket-question-block-sound-jewelry.md)
- [Trinket USB Volume Knob](https://learn.adafruit.com/trinket-usb-volume-knob.md)
- [Zelda: Breath of the Wild – 3D Printed Guardian Sword with NeoPixel LEDs](https://learn.adafruit.com/breath-of-the-wild-guardian-sword-led-3d-printed.md)
- [Larson Scanner Shades (Trinket-Powered NeoPixel LED Strip Glasses)](https://learn.adafruit.com/larson-scanner-shades.md)
- [Sous-vide controller powered by Arduino - The SousViduino!](https://learn.adafruit.com/sous-vide-powered-by-arduino-the-sous-viduino.md)
- [Let’s Put LEDs in Things!](https://learn.adafruit.com/lets-put-leds-in-things.md)
- [LED Trinket Tree Topper](https://learn.adafruit.com/neopixel-led-trinket-tree-topper.md)
- [Trinket / Gemma Space Invader Pendant](https://learn.adafruit.com/trinket-slash-gemma-space-invader-pendant.md)
- [Simple Arduino-based USB VID & PID tester](https://learn.adafruit.com/simple-arduino-based-usb-vid-and-pid-tester.md)
- [Kaleidoscope Eyes (Trinket-Powered NeoPixel LED Ring Goggles)](https://learn.adafruit.com/kaleidoscope-eyes-neopixel-led-goggles-trinket-gemma.md)
- [NeoPixel Cyber Falls Wig](https://learn.adafruit.com/neopixel-cyber-falls.md)
