4 digits TM1637 Red Digital Tube LED Display Module With Clock for Arduino guide

I purchased on ebay, a 4 digit display and I wrote a guide while learning to use it. There is one thing I haven’t figured out yet, is the center colon.

I have included my guide in PDF format and would like to get your input to complete it.

This code turns on the center colon, but I don’t understand it.

segto = 0x80 | display.encodeDigit((cont / 100)%10);
  display.setSegments(&segto, 1, 1);

Complete program

#include <Arduino.h>
#include <TM1637Display.h>

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

// The amount of time (in milliseconds) between tests
#define TEST_DELAY   2000

int cont = 0;
TM1637Display display(CLK, DIO);

void setup()
{
  display.setBrightness(0xF);
  uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 };
  display.setSegments(data);

}

void loop()
{
  uint8_t segto;
  display.showNumberDec(cont, true);
  delay(500);
  cont++;
  segto = 0x80 | display.encodeDigit((cont / 100)%10);
  display.setSegments(&segto, 1, 1);
  delay(500);
  //if (++cont == 10000)
  //   cont = 0;
}

User guide for TM1637 4 digits display.pdf (318 KB)

Thanks so much for this guide/info

I got the same display/board as you and I really wanted to use the colon as it's going to serve as a countdown timer switch, I was about to try find another display before I saw this.

As far as explaining that code 0x80 seems to represent the bit for the colon segment which is connected to digit 1 (second from left).

The first line of your code above combines that bit with the segment data (created by the encodeDigit function) of whatever integer is put in the brackets on the right. All this data is written to whatever variable is on the left, segto in this case.

The second line simply prints this combined segment data to digit one.

It's kind of annoying that the authors of the libraries aren't putting that function in but i'm glad you've found it.

I have just add some code in library:

  1. variable bool “m_colon” (show or not colon)
  2. function setColon(bool colon)
  3. condition “if m_colon == true” - add 0x80 bit to second digit

and there is example in sketch

    display.setColon(true); // just call this function for show central colon

TM1637-1.0.0.zip (8.42 KB)

I own the same module.
For me, the setBrightness() method only works for values higher than 7.
For values lower than 8, the display is off.

Do you guys see the same behavior on yours or is it just me/mine?

:confused: @jobi-Wan: yes, mine is the same. Brightness is 0,8-15 where 11/12-14 are not distinguishable so enough is 0,8,9,10,12,15 in total.

@Sand_r: I made similarly but more flexible (I think). Your solution works only if You use showNumberDec which means You can not independently switch colon on/off and You need to write at least 2 chars to module.
To allow colon behave independent requires remembering segment layout for digit #2.

Chages:

TM1637Display.h:

private:
	uint8_t m_pinClk;
	uint8_t m_pinDIO;
	uint8_t m_brightness=0x0f;	// max default
	bool m_colon=false;		// colon flag

	// === mod by Ramotny
	uint8_t m_digit[1];		// 1 element array to store 2nd digit segments layout for colon on/off
	/* colon can be toggled without storing 2nd digit somewhere in main sketch and rewriting digit by main loop */

TM1637Display.cpp

void TM1637Display::setColon(bool colon)
{
	m_colon = colon;
	
	// === mod by Ramotny
	// force immediate effect of colon state

	setSegments(m_digit,1,1);	// m_digit stores segment layout from last write
}

and

void TM1637Display::setSegments(const uint8_t segments[], uint8_t length, uint8_t pos)
{
    // Write COMM1
	start();
	writeByte(TM1637_I2C_COMM1);
	stop();
	
	// Write COMM2 + first digit address
	start();
	writeByte(TM1637_I2C_COMM2 + (pos & 0x03));
	
	// Write the data bytes
	for (uint8_t k=0; k < length; k++)
	{
	  // === mod by Ramotny

	  if (k+pos==1)			//second digit access
	  {
	    m_digit[0]=segments[k];	//store segment #2 layout

	    if (m_colon)			// detect colon on/off
            {
	       writeByte(segments[k] | 0x80);	// set MSB = colon ON
	    } 
            else
	    {
	       writeByte(segments[k] & 0x7f);	// reset MSB = colon OFF
	    }
	  } 
	  else 
	  {	

             // === end of mod by Ramotny

	    writeByte(segments[k]);	//original version, normal write as-is
	  }

	}
	stop();

	// Write COMM3 + brightness
	start();
	writeByte(TM1637_I2C_COMM3 + (m_brightness & 0x0f));
	stop();
}

example: TM1637_stopwatch

//at 500ms setting internal “clock” arduino error: circa 3’40" late per 8 hours (0.5 sec/min)

#include <Arduino.h>
#include <TM1637Display.h>

/*
   simple stopwatch to demonstrate colon toggle without explicitly rewriting digit #2
   digits of minutes and second are written only when changed
   colon changes independently
*/

// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3

byte dummy0 = 0; //error elimination "collect2.exe: error: ld returned 5 exit status"
byte dummy1 = 0;
byte dummy2 = 0;
byte dummy3 = 0;
byte dummy4 = 0;
byte dummy5 = 0;
byte dummy6 = 0;
byte dummy7 = 0;
byte dummy8 = 0;
byte dummy9 = 0;

TM1637Display display(CLK, DIO);

unsigned long czas;     // milliseconds holder
unsigned long czasold;	// old milliseconds holder
int sek = 0;            // second holder
int mins = 0;           // minutes holder
bool tick = false;      // colon flag

void setup()
{
  display.setBrightness(0x0a);    // set medium
  czasold = millis();             // store start "time"
  display.showNumberDec(mins, true, 2, 0);  // initialize mins display
  display.showNumberDec(sek, true, 2, 2);   // initialize sec display
}

void loop()
{
  czas = millis();            // get current "time"
  if (czas - czasold > 496)   // tic-tac each 1/2 sec
  {
    czasold = czas;           // store new "time"

    display.setColon(tick);   // display colon

    if (tick)                 // colon ON = increment seconds
    {
      sek++;
      if (sek > 59)           // if 60 sec, increment minutes
      {
        sek = 0;
        mins++;
        if (mins > 59)        // if 60 minutes, increment hours
        {
          mins = 0;           // start from 00:00 again. May add here hour counter
        }
        display.showNumberDec(mins, true, 2, 0);
      }
      display.showNumberDec(sek, true, 2, 2);
    }
    tick = !tick;             // toggle colon on/off
  }
}

All files attached (ZIP has .h and .cpp)

TM1637Display.zip (3.88 KB)

TM1637_stopwatch.ino (1.76 KB)

Thank you for cleaning it up with code tags.

"Auto Format" is in the IDE under "Tools", not part of the forum. :grinning:

Sorry Paul - I thought it will be better but was wrong... Happens :)

I will stick to rules but before I correct my post:

  1. I don't see "Auto format" on my toolbar just above post edit window

  2. What is the best/most favourable way to mark all changes to original code to spare readers analysis what has been changed/modified? Only comment lines //? Or maybe MOD-BEGIN + MOD-END? I have not seen any tips in introduction about that except to post whole code...

And again - I'm sorry for my "garbage"...

In the Arduino IDE, press ctl-T to format your code. It is not as clever as Unix indent but it makes everything easier to read.

You can just add a comment like "//mod by Ramotny" or "// #############" or just describe your mod in English.

David.

Post cleaned up (I hope).

In example both lines displaying minutes and second could be just at the end of loop

void loop()
{
  czas = millis();            // get current "time"
  if (czas - czasold > 495)   // tic-tac each 1/2 sec
  {
    czasold = czas;           // store new "time"

    display.setColon(tick);   // display colon

    if (tick)                 // colon ON = increment seconds
    {
      sek++;
      if (sek > 59)           // if 60 sec, increment minutes
      {
        sek = 0;
        mins++;
        if (mins > 59)        // if 60 minutes, increment hours
        {
          mins = 0;           // start from 00:00 again. May add here hour counter
        }
      }
    }
    display.showNumberDec(mins, true, 2, 0);
    display.showNumberDec(sek, true, 2, 2);
    tick = !tick;             // toggle colon on/off
  }
}

or even very end

    }
    tick = !tick;             // toggle colon on/off
  }
  display.showNumberDec(mins, true, 2, 0);
  display.showNumberDec(sek, true, 2, 2);
}

but I wanted do show that even they change rarely the colon can change independently without need to remember/store 2nd digit in main loop

Regards Gregory

my current code for the colon is this:

  int hours = Clock.getHour(h12, PM);
  int mins = Clock.getMinute();

  disptime =  (hours * 100) + mins; // sets the digital display to the time 
  
  uint8_t segto;
  int value = disptime;
  segto = 0x80 | display.encodeDigit((value / 100)%10);
  display.setSegments(&segto, 1, 1);

but my colon blinks does any one know how to stop that from happening?

-thank you

max_malfoy: my current code for the colon is this:

  int hours = Clock.getHour(h12, PM);
  int mins = Clock.getMinute();

  disptime =  (hours * 100) + mins; // sets the digital display to the time     uint8_t segto;   int value = disptime;   segto = 0x80 | display.encodeDigit((value / 100)%10);   display.setSegments(&segto, 1, 1);




but my colon blinks does any one know how to stop that from happening?

-thank you

I don't see anything in there to control colon blinking. But maybe I am mistaken.

Please post your entire sketch so we have a better idea of what is going on.

I just published a simpler library which displays a string instead of the multiple interfaces used by previous libraries. This allows you to more easily control the display of numbers and the colon. For example:

void setup()
{
   tm1637Init(CLOCK_PIN, DATA_PIN);
   tm1637SetBrightness(4);
}

void loop()
{
   char szTemp[8];
#ifdef SHOW_COLON
   sprintf(szTemp, "%02d:%02d", minutes, seconds); // show colon
#else
   sprintf(szTemp, "%02d %02d", minutes, seconds); // don't show colon
#endif
   tm1637ShowString(szTemp);
}

You can get it here:

Larry's TM1637 Library

Greetings;

Good job (even though not much can be useful for newbies).

By the way: Why delay? Doesn't it prevent the usage of buttons (for instance)?

Regarding the Colon: Why not some functionality to enable or disable the blink?

In one fo the TM1637 Libraries I'm using; I achived this with a simple line of code:

 void PrintTime(String currTime)
{
  display.printTime(currTime, true);  // display time

  display.showNumberDecEx(t, 0b01000000, true, 4) (Colon ON)
  if (millisDelay(1000))
  {
    display.showNumberDec(t); //Display the Variable value; (Colon OFF)
    delay(500);
  }
}