How works a TM1638:The TM1638 is a small board with a range of Leds, a range of digits and a range of buttons. It takes 3 pins to receive instructions ( plus on for vcc and one for ground). This 3 pins are : - STROBE, used to start an instruction. - CLCK used to inform that a bit is readable. - DATA used to send bit. It has 16 addresses used to control Leds and Digits. Even adresses are for digits and odd one for leds. You have a set of 4 instructions that you can send over: - ACTIVATE (0x8F) = activate the board, make it ready to receive other instructions. - WRITE_SINGLE (0x44) = write byte to single address - WRITE_CONSECUTIVE (0x40) = write byte consecutively starting at a specific address - READ (0x42) = read incoming byte. At the beginning all your Pins linked to the board must be in Output mode. STROBE pin must be set (to HIGH). All the other must be clear (to LOW).
How to send DATAData are sent bit by bit over the DATA pin. You first set your bit and then pulse (HIGH then LOW) CLCK Pin to inform the board a data is available. And so on, until the last bit.
How to send commandTo send a command you have to put STROBE to CLEAR (LOW). When your command number is sent (see How to send data), you SET STROBE (HIGH). If you command has parameters like an adress and a value, again you CLEAR STROBE before sending them, and SET STROBE when it's done.
Activate the boardTo activate the board you must first send the activate command. The command is 0x8 + the brightness. In our library we set the maximum brightness so we gonna send : 0x8F to the board. There is no parameters to this function. So typically we 'll send 1001111 bits. In pseudo code: STROBE:CLEAR DATA:SET CLCK:PULSE_HIGH DATA:SET CLCK:PULSE_HIGH DATA:SET CLCK:PULSE_HIGH DATA:SET CLCK:PULSE_HIGH DATA:CLEAR CLCK:PULSE_HIGH DATA:CLEAR CLCK:PULSE_HIGH DATA:SET CLCK:PULSE_HIGH STROBE:SET
LEDsYou control led over even addresses. The simplest way to switch on the led is to send the WRITE_SINGLE command. This command take two parameters (two bytes): the address and the byte to write. If you want to switch on you send 1. To switch off you send 0. To send the address the form is 1100xxxx where xxxx represent the address. This command is 0x44. So if you want to switch on led2 you have to: STROBE:CLEAR Send 0x44 SROBE:SET STROBE:CLEAR Send 0xC4 Send 0x01 SROBE:SET
ButtonsTo read the input value of button, you first have to send the read command. Read command is 0x42. Then you set your DATA pin in input mode. To read you first set CLK. Then you read on DATA pin. And clear CLK, and again... to read 4 bytes of data. When the byte is read, you can set STROBE. Returned bytes give you which buttons are pressed (bit to 1 if pressed). On the first one you have information about button One and Five (S1 and S5) on bit 1 and 5. On the second one you have information about button Two and Six (S2 and S6) on bit 2 and 6. On third one it's Three and Seven (S3,S7) on bit 3 and 7. And for the last one , information on button four and Eight (S4,S8) on bit 4 and 8.
DigitsTo control digits you will use the same command as to display Leds: WRITE_SINGLE. But you have to write on odd addresses this time. The digit is divided in 7 segments + the point. Here is the bit number which represent a segment. If the position is set to 1, then the segment is light on. So to print number 2 on the last digit, you can send: STROBE:CLEAR Send 0x44 SROBE:SET STROBE:CLEAR Send 0xCE Send 0x5B (01011011) STROBE:SET
TM1638 with etl library
Supported MCUThis code has been tested on ESP8266.
Build the driver with etl library
Issue a command
Using the driver
Using LEDsTo switch on led 1,2,5 and 8:
Using digitsSimply use:
Using ButtonsFirst create an Handler to receive input:
Then set the handler to the TM1638:
ReferencesEtl library: Etl library An other article on TM1638 but with arduino: Article Arduino