TM1638 8-Digit Display Module

These TM1638 modules are available very cheaply from sites such as eBay, AliExpress, and Banggood, and are ideal for displaying a value from a sensor or calculation:


They use an SPI protocol, and you write to the displays by sending a sequence of bytes to the displays. However, they are slightly cumbersome to drive because each byte in the stream sets the state of a single segment in all the displays.

Connecting the display

Connect the display using the appropriate SPI pins as follows:

GND Ground
STB Enable

For details of which pins are used for the SPI interface on different processors see Language reference: with-spi.

You can use any suitable pin as the Enable pin; specify it as follows:

(defvar en 10)

Initialising the display

The following routine on turns on the display and sets the brightness; the parameter can be from 0 (dimmest) to 7 (brightest):

(defun on (bri)
  (with-spi (str en 1000 0 2)
    (write-byte (+ #x88 bri) str)))

Defining the segment definitions

The following list gives the segment definitions for the digits 0 to 9, the hexadecimal digits A to F, and space and minus:

(defvar seg '(#x7e #x30 #x6d #x79 #x33 #x5b #x5f #x70 #x7f 
              #x7b #x77 #x1f #x4e #x3d #x4f #x47 #x00 #x01))

Clearing the display

This routine clr clears the display:

(defun clr ()
  (with-spi (str en 1000 0 2)
    (write-byte #x40 str))
  (with-spi (str en 1000 0 2)
    (write-byte #xC0 str)
    (dotimes (n 16)
      (write-byte 0 str))))

Displaying digits

Finally this routine put writes eight digits to the display:

(defun put (dp &rest dig)
  (with-spi (str en 1000 0 2)
    (write-byte #x40 str))
  (with-spi (str en 1000 0 2)
    (write-byte #xc0 str)
    (dotimes (n 7)
      (let ((byt 0))
        (dotimes (c 8)
          (setq byt
                    (nth (nth c dig) seg)
                    (- n 6))
                  (- 7 c))
        (write-byte byt str)
        (write-byte 0 str)))
    (write-byte (ash 1 (- 7 dp)) str)))

The first parameter specifies the decimal point position, from 0 to 7.

The remaining eight parameters specify the number to show on each display. For example, to display "0123.4567" give the command:

(put 3 0 1 2 3 4 5 6 7)

and to display "FACE-OFF" give:

(put 8 15 10 12 14 17 0 15 15)