PCF8523 and Trinket 5V

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
btsprague
 
Posts: 24
Joined: Sun Jan 15, 2017 12:06 pm

PCF8523 and Trinket 5V

Post by btsprague »

Hi,
For the last few weeks I've being working on and off to get a PCF8523 RTC breakout to work with the Trinket 5V. I think I have boiled the problem down to a commutation problem between the Trinket and the RTC or I might be running out of RAM. I am pretty sure that its not my code because I have run the same code on a Sparkfun Redboard(Arduino Uno) and it works perfectly. Here is the code if there's something I haven't spotted that would affect the trinket that isn't affecting the Redboard.

Code: Select all

#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include "RTClib.h"
#ifdef __AVR__
#include <avr/power.h>
#endif

RTC_PCF8523 rtc;

#define PIN 2 //CHANGE
Adafruit_NeoPixel strip = Adafruit_NeoPixel(21, PIN, NEO_GRB + NEO_KHZ800);
boolean pixelval[21];
int lastHr;
int lastMin;
int lastSec;
SoftwareSerial mySerial(4, 3); // RX, TX

void setup() {
  //Serial.begin(57600);
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  if (! rtc.begin()) {
    delay(10);
    mySerial.println("There was a problem");
    while (1);
  }

  if (! rtc.initialized()) {
    // following line sets the RTC to the date & time this sketch was compiled
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
     rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
     delay(5);
    mySerial.println("Time Set");
  }
  strip.begin();
  strip.setBrightness(50);
  strip.show(); // Initialize all pixels to 'off'
  mySerial.begin(9600);


}
void loop() {
  // put your main code here, to run repeatedly:
  DateTime now = rtc.now();
  if (now.hour() != lastHr) {
    for (int i = 0; i < 5; i++) {
      pixelval[i] = bitRead(now.hour(), i);
      lastHr = now.hour();
      mySerial.print(lastHr);
    }
  }

  if (now.minute() != lastMin) {
    for (int i = 0; i < 8; i++) {
      pixelval[i + 13] = bitRead(now.minute(), i);
      lastMin = now.minute();
      mySerial.print(lastMin);
    }
  }

  if (now.second() != lastSec) {
    for (int i = 0; i < 8; i++) {
      pixelval[i + 5] = bitRead(now.second(), i);
      lastSec = now.second();
      mySerial.print(lastSec);
    }
  }

  for (int i = 0; i < 21; i++) {
    if (pixelval[i] == true) {
      if (i < 5) {
        strip.setPixelColor(i, 0, 255, 0);
      }
      else if (i > 4 && i < 13) {
        strip.setPixelColor(i, 0, 0, 255);
      }
      else if (i > 12 && i < 21) {
        strip.setPixelColor(i, 255, 0, 0);
      }

    }
    else {
      strip.setPixelColor(i, 0, 0, 0);
    }
  }
  strip.show();
  mySerial.print(now.hour(), DEC);
  mySerial.print(':');
  mySerial.print(now.minute(), DEC);
  mySerial.print(':');
  mySerial.print(now.second(), DEC);
  mySerial.println();
}
Basically I am trying to make a binary clock, I have tested the clock part of the code without the rtc just using the time library and it worked perfectly, but know that I have added the RTC things have stopped working. The trinket will constantly print out that the time is 00:00:03 and that is reflected on the neopixels I'm using to display the time. What should happen is the correct time should be pulled from the RTC, printed to the Serial console and then displayed in binary on the neopixels. This is what happens when I use the redboard but not when I use the trinket.
Sorry if this is a little confusing, I tried my best to make it as clear as possible.
Thanks,
Ben

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: PCF8523 and Trinket 5V

Post by adafruit_support_mike »

Try adding pull-up resistors to the SDA and SCL lines.

Our PCF8523 breakout should have them, but it won't hurt to add another 1k or so.

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”