Help: need tutorial on DS3231 with ws2812b

Update: Hi please review my code and give some suggestions. i tried to avoid rapidly calling of fastled show command and delay function. Now i am interested to add alarm function and 12 hour format. i am using rtc lib. Any good tutorial ??

/* Digital Clock with WS2813 Strip Version 2*/

#include <FastLED.h>
#include <Wire.h>
#include "RTClib.h"

#define LED_PIN     7
#define NUM_LEDS    31
#define LED_TYPE   WS2812B
#define COLOR_ORDER GRB
#define BRIGHTNESS  100   // MAX Brightness 0---255

int firstdigithour;    // For storing first digit of hour 
int seconddigithour;   // For storing Second digit of hour
int firstdigitminute;  // For storing first digit of minute 
int seconddigitminute; // For storing Second digit of minute
int bitsecond;         // For storing last bit of seconds
int storeseconds;      // For storing current seconds

int previousfirstdigithour;
int previousseconddigithour;
int previousfirstdigitminute;
int previousseconddigitminute;
int previousbitsecond;


CRGB leds[NUM_LEDS];
CRGB color = CRGB::White;   

RTC_DS3231 rtc;
//char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () 
{
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(BRIGHTNESS);

  FastLED.delay(300) ;              // For initializing LED strip
  for(int x = 0; x < NUM_LEDS; x++)
  {
    leds[x] = color;        // For checking all SMD chips are working fine
  }
  FastLED.show();
  delay(1000);
  
  for(int x = 0; x < NUM_LEDS; x++)
  {
   leds[x] = CRGB::Black;;
  }
  FastLED.show();
  delay(500);    
  Serial.begin(9600);
  while (!Serial) { /* Wait until serial is ready */ }

  if (!rtc.begin()) 
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) 
  {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // For synchronize time with computer
    // 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));
  }
	gettime();
	firsttime();
	blinkseconds();
	leds[30] = CRGB::Red;
}

void gettime()
{
  DateTime now = rtc.now();
  firstdigithour = now.hour() / 10;
  seconddigithour = now.hour() % 10;
  firstdigitminute = now.minute() / 10;
  seconddigitminute = now.minute() % 10;
  storeseconds =now.second();
  bitsecond = bitRead(storeseconds, 0);
}

void firsttime()
{
  displayTime(0, seconddigitminute);    
  displayTime(7, firstdigitminute);
  displayTime(16, seconddigithour);    
  displayTime(23, firstdigithour); 
  
  previousfirstdigithour = firstdigithour; 
  previousseconddigithour = seconddigithour;
  previousfirstdigitminute = firstdigitminute;
  previousseconddigitminute = seconddigitminute;
  previousbitsecond = bitsecond;
  FastLED.show();
  
 /* Serial.println();
  Serial.print(firstdigithour);
  Serial.print(seconddigithour);
  Serial.print(":");
  Serial.print(firstdigitminute);
  Serial.print(seconddigitminute); */
  }

void blinkseconds()
{
  if(bitsecond==1)
  {
  leds[14] = CRGB::Red;
  leds[15] = CRGB::Red;
  }
  else
  { 
  leds[14] = CRGB::Black;
  leds[15] = CRGB::Black;
  }
}

void displayTime(int startindex, int number) {

  byte numbers[] = {
    0b00111111, // 0    
    0b00000110, // 1
    0b01011011, // 2
    0b01001111, // 3
    0b01100110, // 4
    0b01101101, // 5
    0b01111101, // 6
    0b00000111, // 7
    0b01111111, // 8
    0b01101111, // 9   
  };
  for (int i = 0; i < 7; i++) {
    leds[i + startindex] = ((numbers[number] & 1 << i) == 1 << i) ? color : CRGB::Black;
  }
}
void loop()
{
gettime();

  if(bitsecond!=previousbitsecond)
  {
   previousbitsecond = bitsecond;
   Serial.println("seconds command executed");
   blinkseconds();
   FastLED.show();
  }
  
  if(seconddigitminute!=previousseconddigitminute)
  {
   previousseconddigitminute = seconddigitminute;
   Serial.println("second digit minute command executed");
   displayTime(0, seconddigitminute);    
   FastLED.show();
  }  
  
  if(firstdigitminute!=previousfirstdigitminute)
  {
   previousfirstdigitminute = firstdigitminute;
   Serial.println("first digit minute command executed");
   displayTime(7, firstdigitminute);
   FastLED.show();
  }
 
  if(seconddigithour!=previousseconddigithour)
  {
   previousseconddigithour = seconddigithour;
   Serial.println("second digit hour command executed");
   displayTime(16, seconddigithour);     
   FastLED.show();
  }
  
  if(firstdigithour!=previousfirstdigithour)
  {
   previousfirstdigithour = firstdigithour; 
   Serial.println("first digit hour command executed");
   displayTime(23, firstdigithour); 
   FastLED.show();
  }
}

UPDATE: i purchased DS3231 and have ws2813 let strips 60 leds per meter. The tutorial for DS3231 was not found on net for beginners. Does anyone have reference from where i start? I am no longer using RGB diodes.

Hi everyone,

I have arduino nano and want to build simple digital clock in 7 segment display (hours, minutes, blinking seconds )with customized colours.

My first question is what components are required ? which is better option RGB leds with common Anode or Cathode? DS3231 module i am using.

Thanks in advance

jaspal23:
Which is better option RGB LEDs with common Anode or Cathode? DS3231 module I am using.

RGB LEDs? A lot of them, to be individually controlled?

WS2812 - "NeoPixels."

Paul__B:
RGB LEDs? A lot of them, to be individually controlled?

WS2812 - "NeoPixels."

I know WS2812 easy to control but it is expensive then RGB Leds. i am thinking to make 7 segment digit with RGB leds like 7 segment single digit works

jaspal23:
I know WS2812 easy to control but it is expensive then RGB Leds.

Driving standard RGB LEDs is more difficult. When you factor in the cost of the driver components, the ws2812 will work out cheaper.

PaulRB:
Driving standard RGB LEDs is more difficult. When you factor in the cost of the driver components, the ws2812 will work out cheaper.

so should i go with WS2812 rather then RGB leds???

jaspal23:
so should i go with WS2812 rather then RGB leds???

it's up to you, but as mentioned already, WS2812 will be easier to build than common RGB LEDs. Just do your calculation ... 6 x 7 Segments x 3 colors ... how much work (and money) will you spend on drivers, resistors, ...

By the way, we had a similar discussion some weeks ago in the German forum. In post #11 you will find a prototype implementation of a Neopixel 7 Segment display class. It will enable you to easily "print" to your display like you are used with LCD or Serial.

display.print("114832"); // print 11 49 32 to your display

If you go towards this way, leave a note. I'll probably expand this prototype to a library.

OK, I do confess - each MAX7219 will drive 16 RGB LEDs. Common cathode or common anode is irrelevant.

So let's see, six digits, 7 segments - 42 RGB LEDs. Three MAX7219s. And you can add plenty of two-colour or single colour as well.

Just buy four of these kits:

Or these ones

which used to be more expensive but are now actually cheaper and more useful if you wish to stack matrix arrays.

The point is that you do not install the matrix arrays from the kits themselves - or their socket pins, but just solder to the positions on the PCB and you have a durable and reliable assembly to drive your own RGB arrays.

Why did I say four? Well, you can fully assemble the first one as the matrix with which it comes and practice programming it. Then the others for your current project! :grinning:

Considering the cost, it makes no sense to just buy the bare minimum!

If you use max7219 drivers, you will have a choice of only 7 colours (red, green, blue, cyan, yellow, magenta, white). Ws2812 will allow many more colours.

Another possibility is ws2811. You can buy ws2811 strips that require a 12V supply. The LEDs cannot be controlled individually, only in groups of 3, and you can only cut the strip after a group of 3. So you could make each segment for your digits by cutting lengths of 3, 6 or even 9 LEDs, making the digits quite large. You may find ws2811 strips cheaper than ws2812.

Thanks to all,

i dropped an idea of using RGB leds. i will order ws2812 individual chips and DS3231 RTC module when global trade opens.

Also i will use max7219 for other project.

once again thanks

Any good reference on DS3231 tutorial?????????

Try this one.

i dropped an idea of using RGB leds. i will order ws2812 individual chips and DS3231 RTC module when global trade opens.

in the meantime I put together a library to simulate 7 segment displays with Neopixels, to handle them as one display and to use the print methods to print to the display

https://werner.rothschopf.net/202005_arduino_neopixel_display_en.htm

Hi,

i am trying to blink seconds light LED 14 & 15 with seconds on off. I am unable to apply logic and code. Any type of help appreciated and check my code is it good or it can be optimized in a better way…

#include <FastLED.h>
#include <Wire.h>
#include "RTClib.h"

#define LED_PIN     7
#define NUM_LEDS    30
#define LED_TYPE   WS2812B
#define COLOR_ORDER GRB
#define BRIGHTNESS  100   // MAX Brightness 0---255

CRGB leds[NUM_LEDS];
CRGB color = CRGB::White;   

RTC_DS3231 rtc;

void setup () 
{
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(BRIGHTNESS);

  Serial.begin(9600);
  while (!Serial) { /* Wait until serial is ready */ }

  if (!rtc.begin()) 
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) 
  {
    Serial.println("RTC lost power, lets set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // For synchronize time with computer
 }
}

void loop () 
{

DateTime now = rtc.now();
  
  int hl = now.hour() / 10;
  //Serial.print(hl);
  int hr = now.hour() % 10;
  //Serial.print(hr);
  int ml = now.minute() / 10;
  //Serial.print(ml);
  int mr = now.minute() % 10;
  //Serial.print(mr);

  displayTime(0, mr);    
  displayTime(7, ml);
  displayTime(16, hr);    
  displayTime(23, hl); 
  // for dot leds 14 & 15 
  FastLED.show();
}

void displayTime(int startindex, int number) {

  byte numbers[] = {
    0b00111111, // 0    
    0b00000110, // 1
    0b01011011, // 2
    0b01001111, // 3
    0b01100110, // 4
    0b01101101, // 5
    0b01111101, // 6
    0b00000111, // 7
    0b01111111, // 8
    0b01101111, // 9   
  };
  
  for (int i = 0; i < 7; i++) {
    leds[i + startindex] = ((numbers[number] & 1 << i) == 1 << i) ? color : CRGB::Black;
  }

  // Dots
  leds[14] = CRGB::Red;
  leds[15] = CRGB::Red;
}

You are updating the strip and calling FastLED.show() too frequently, probably several hundred times per second. You only need to do this 1 or 2 times per second, to animate the dots.

PaulRB:
You are updating the strip and calling FastLED.show() too frequently, probably several hundred times per second. You only need to do this 1 or 2 times per second, to animate the dots.

I tried to use bit of second to blink seconds, i dont know it is effective or not, but it works

PaulRB:
If you use max7219 drivers, you will have a choice of only 7 colours (red, green, blue, cyan, yellow, magenta, white). Ws2812 will allow many more colours.

Another possibility is ws2811. You can buy ws2811 strips that require a 12V supply. The LEDs cannot be controlled individually, only in groups of 3, and you can only cut the strip after a group of 3. So you could make each segment for your digits by cutting lengths of 3, 6 or even 9 LEDs, making the digits quite large. You may find ws2811 strips cheaper than ws2812.

Hi, Due to covid19, i am unable to order ws2812b and not available at local shops. so i have to purchased
Max7219 4x8x8 module (product no 1088AS). After google and checked datasheet, it emit only red light. Does my max7219 module not emit different colours???

Correct. The display contains 64 red LEDs. You may be able to buy other colour matrix displays which you can use to replace your current displays (they normally unplug from the max7219 modules), but they are hard to find, and still only single colour. You can buy bi-colour matrix displays, and RGB matrix displays, but your max7219 modules will not be compatible with those.

When I mentioned using max7219 before, that was to drive up to 20 separate RGB leds. Only 7 colours.

PaulRB:
Correct. The display contains 64 red LEDs. You may be able to buy other colour matrix displays which you can use to replace your current displays (they normally unplug from the max7219 modules), but they are hard to find, and still only single colour. You can buy bi-colour matrix displays, and RGB matrix displays, but your max7219 modules will not be compatible with those.

When I mentioned using max7219 before, that was to drive up to 20 separate RGB leds. Only 7 colours.

:frowning: hmm. money has been wasted.It was expensive. Anyhow could you share any RGB matrix reference for future development

As I explained in #6, each MAX7219 will drive 16 RGB LEDs given that you assemble and wire the matrices yourself. Common cathode or common anode is irrelevant.

The cheap (or used to be before Covid-19) boards are a neat way of mounting the chips and you put aside the original red display provided.

The RGB matrix modules are substantially more expensive, which is why WS2812s are probably going to be the cheaper option as well as having (many) more colours. Availability will be a problem with all options.

jaspal23:
Due to covid19, i am unable to order ws2812b and not available at local shops.

You are much more likely to catch covid19 at the local shops than from a package delivered from overseas. I have not heard that anyone has ever contacted covid19 except by contact with other infected people. So not being able to order is crazy. Is it a government restriction? I have had packages delivered to UK from Wuhan since lockdown.