Interfacing TM1637 with Arduino UNO

I have described about TM1637 interface protocol here. In this post we will see how control TM1637 using arduino uno board. We will implement a counter that counts the number of seconds passed since the board is switched on.

what you need

Arduino UNO
TM1637 based 4 digit 7-segment display
few jumpers and a PC with Arduino installed

Connecting TM1637 with Arduino UNO

The display board only required 4 connections. Thanks to the on board pull up resistors, we don’t need to place external pull up resistors.

Arduino UNODisplay board
5VVCC
GNDGND
3CLK
2DIO
Arduino UNO to TM1637 connections

Code

The code not that different from the pseudo code present here. We add additional logic for counting the seconds and converting the 4 digit decimal string. We also need to implement the methods to control the IO lines.

Load the following code into uno, and it will starting counting seconds on the display. Though I am not using any library for communicating with TM1637, it is recommended to use some library such as this instead of writing custom driver. AceSegment library has support for many 7 segment display driver chips.

#include <Arduino.h>
const uint8_t CLK_CYCLE_DELAY = 100;// us
const uint8_t CLK_PIN = 3;
const uint8_t DIO_PIN = 2;
const uint8_t NUM_DIGITS = 4;

uint8_t buffer[NUM_DIGITS+1] = {0xC0}; // first byte will be the register address
// LED segment patterns.
const uint8_t NUM_PATTERNS = 10;
const uint8_t PATTERNS[NUM_PATTERNS] = {
  0x3F, // 0
  0x06, // 1
  0x5B, // 2
  0x4F, // 3
  0x66, // 4
  0x6D, // 5
  0x7D, // 6
  0x07, // 7
  0x7F, // 8
  0x6F, // 9
};

void setDataPin(uint8_t _bit){
  digitalWrite(DIO_PIN,_bit);
}
void setClkPint(uint8_t _bit){
  digitalWrite(CLK_PIN,_bit);
}

void sleep(int sleep){
  delayMicroseconds(sleep);
}

void start() {
    setDataPin(0);
    sleep(CLK_CYCLE_DELAY);
    setClkPint(0);
    sleep(CLK_CYCLE_DELAY);
}

void stop() {
    setDataPin(0);
    sleep(CLK_CYCLE_DELAY);
    setClkPint(1);
    sleep(CLK_CYCLE_DELAY);
    setDataPin(1);
}
    

void writeByte(uint8_t _byte){
    for(uint8_t i=0;i<8;i++){
      setDataPin(_byte >> i & 0x01) ;
      sleep(CLK_CYCLE_DELAY);
      setClkPint(1);
      sleep(CLK_CYCLE_DELAY);
      setClkPint(0);
      sleep(CLK_CYCLE_DELAY);
    }
      
    setClkPint(0);
    sleep(CLK_CYCLE_DELAY);
    setClkPint(1);
    sleep(CLK_CYCLE_DELAY);
    setClkPint(0);
    sleep(CLK_CYCLE_DELAY);
}

void writeDataCmd(uint8_t cmd){
    start();
    writeByte(cmd);
    stop();
}

void sendByteStream(uint8_t *bytes,uint8_t len) {
    writeDataCmd(0x40);
    start();
    for(uint8_t i=0;i<len;i++){
        writeByte(bytes[i]);
    }
    stop();
}

void displayCount(int count){
  for(uint8_t i=0;i<NUM_DIGITS;i++){
    uint8_t digit = count % 10;
    count = count/10;
    buffer[NUM_DIGITS - i] = PATTERNS[digit];
  } 
  sendByteStream(buffer,NUM_DIGITS+1);
}

void setup() {
  Serial.begin(115200);
  Serial.println("TM1637 Demo");
  pinMode(CLK_PIN,OUTPUT);
  pinMode(DIO_PIN,OUTPUT);
  stop();
  // switch on the display
  uint8_t displayControlCmd[1]={0x8F};// display on with full brightness
  sendByteStream(displayControlCmd,1);
}

void loop() {  
  displayCount(millis()/1000);
  delay(100);
}

Add a Comment

Your email address will not be published. Required fields are marked *