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.
Connect the display using the appropriate SPI pins as follows:
Pin | Connection | Arduino Mega 2560 |
---|---|---|
GND | Ground | GND |
DIO | MOSI | 51 |
CLK | SCK | 52 |
STB | Enable | 10 |
VCC | +5V | 5V |
The third column shows the pins I used when testing the program on an Arduino Mega 2560. You can use any suitable pin as the Enable pin; specify it with this defvar:
(defvar en 10)
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)))
The following list gives the segment definitions for the digits 0 to 9 and the hexadecimal digits A to F:
(defvar seg '(#x7e #x30 #x6d #x79 #x33 #x5b #x5f #x70
#x7f #x7b #x77 #x1f #x4e #x3d #x4f #x47))
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))))
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
(logior
(ash
(logand
(ash
(nth (nth c dig) seg)
(- n 6))
1)
(- 7 c))
byt)))
(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, or 8 for no decimal point.
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)
Reading the keypad is left as an exercise for the reader.