Using 7-Segment Display TM1637 with Raspberry Pi Pico
Interfacing TM1637 4-digit 7-segment LED display with Raspberry Pi Pico using MicroPython Code to display numbers, text with library usage
- Suitable forB.Tech/Diploma RPi Pico students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
Overview
In this tutorial, we will learn how to use the TM1637 4-digit 7-segment LED display with Raspberry Pi Pico. These displays are cheaper & best for displaying sensor data, time, stopwatch, random numbers, etc. It is similar to other 4-digit 7-segment displays but has a TM1637 LED Driver IC embedded in it. This removes the extra wiring and using only 2 wires the display can be controlled.
In this guide, we will learn about the method to use TM1637 with Raspberry Pi Pico using the MicroPython Code. We will display some text, random numbers, or anything you like using the TM1637 MicroPython library and code. Earlier we used
TM1637 with Arduino
to display various numbers and characters.
Components Required
In this guide, I used
Elecrow Raspberry Pi Pico Starter Kit
to test different Modules. You can buy the kit and perform some other operations as well. From this kit, you can use the following components.
Raspberry Pi Pico Board -1
TM1637 Module – 1
Breadboard -1
Jumper Wires – 4
Micro-USB Cable
TM1637 4-digit 7-segment LED display
The Bare 4-digit 7-segment displays usually require 12 connection pins. But the TM1637 IC is mounted on the back of the display module, which reduces the total wire to 4. Two pins are required for the power connections and the other two pins are used to control the segments.
The TM1637 module includes four 0.36″ segment 7-segment displays. The module has a ‘colon’ at the center useed to create clock or time-based projects. At the back of the display, there is an inexpensive serial LED driver from Titan MicroElectronics called
TM1637
. The TM1637 supports many functions – including ON/OFF and brightness control of the LEDs as well as access to each of the segments.
The module operates between 3.3V to 5V with a current consumption of 80mA. We can interface TM1637 with Raspberry Pi Pico or any other microcontroller using the two data pins. There is a dedicated MicroPython Library Library for TM1637 that removes the complexities and makes it easier to communicate with the display.
TM1637 Module Pinout
There is a 4-pin male header on the module for making connections.
1. GND:
Ground Pin
2. VCC:
3.3V to 5V power supply Pin
3. DIO:
Data Input/Output Pin
4. CLK:
Clock Input Pin
Interfacing TM1637 with Raspberry Pi Pico
Now let us interface the TM1637 4-Digit 7-Segment Display with the Raspberry Pi Pico Board using the MicroPython Code. Here is the connection diagram.
Connect the VCC, GND, DIO & CLK pin of TM1637 to 3.3V, GND, GP27 & GP26 of Raspberry Pi Pico.
MicroPython Code/Program
The TM1637 requires MicroPython library for interfacing with Raspberry Pi Pico. Therefore we have codes here as “
tm1637.py
” & “
main.py
“.
tm1637.py
Open your Thonny IDE and paste the following code in Thonny Editor. Save the file to Raspberry Pi Pico with the name
tm1637.py
from
micropython
import
const
from
machine
import
Pin
from
time
import
sleep_us
sleep_ms
TM1637_CMD1
=
const
(
)
# 0x40 data command
TM1637_CMD2
=
const
(
)
# 0xC0 address command
TM1637_CMD3
=
const
(
)
# 0x80 display control command
TM1637_DSP_ON
=
const
(
)
# 0x08 display on
TM1637_DELAY
=
const
(
)
# 10us delay between clk/dio pulses
TM1637_MSB
=
const
(
)
# msb is the decimal point or the colon depending on your display
# 0-9, a-z, blank, dash, star
_SEGMENTS
=
bytearray
(
b
'\x3F\x06\x5B\x4F\x66\x6D\x7D\x07\x7F\x6F\x77\x7C\x39\x5E\x79\x71\x3D\x76\x06\x1E\x76\x38\x55\x54\x3F\x73\x67\x50\x6D\x78\x3E\x1C\x2A\x76\x6E\x5B\x00\x40\x63'
)
class
TM1637
(
object
)
:
""
"Library for quad 7-segment LED modules based on the TM1637 LED driver."
""
def
__init__
(
self
clk
dio
brightness
=
)
:
self
clk
=
clk
self
dio
=
dio
if
not
<=
brightness
<=
:
raise
ValueError
(
"Brightness out of range"
)
self
_brightness
=
brightness
self
clk
init
(
Pin
OUT
value
=
)
self
dio
init
(
Pin
OUT
value
=
)
sleep_us
(
TM1637_DELAY
)
self
_write_data_cmd
(
)
self
_write_dsp_ctrl
(
)
def
_start
(
self
)
:
self
dio
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
def
_stop
(
self
)
:
self
dio
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
self
dio
(
)
def
_write_data_cmd
(
self
)
:
# automatic address increment, normal mode
self
_start
(
)
self
_write_byte
(
TM1637_CMD1
)
self
_stop
(
)
def
_write_dsp_ctrl
(
self
)
:
# display on, set brightness
self
_start
(
)
self
_write_byte
(
TM1637_CMD3
TM1637_DSP_ON
self
_brightness
)
self
_stop
(
)
def
_write_byte
(
self
b
)
:
for
i
in
range
(
)
:
self
dio
(
(
b
>>
i
)
&
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
def
brightness
(
self
val
=
None
)
:
""
"Set the display brightness 0-7."
""
# brightness 0 = 1/16th pulse width
# brightness 7 = 14/16th pulse width
if
val
is
None
:
return
self
_brightness
if
not
<=
val
<=
:
raise
ValueError
(
"Brightness out of range"
)
self
_brightness
=
val
self
_write_data_cmd
(
)
self
_write_dsp_ctrl
(
)
def
write
(
self
segments
pos
=
)
:
""
"Display up to 6 segments moving right from a given position.
The MSB in the 2nd segment controls the colon between the 2nd
and 3rd segments."
""
if
not
<=
pos
<=
:
raise
ValueError
(
"Position out of range"
)
self
_write_data_cmd
(
)
self
_start
(
)
self
_write_byte
(
TM1637_CMD2
pos
)
for
seg
in
segments
:
self
_write_byte
(
seg
)
self
_stop
(
)
self
_write_dsp_ctrl
(
)
def
encode_digit
(
self
digit
)
:
""
"Convert a character 0-9, a-f to a segment."
""
return
_SEGMENTS
[
digit
&
0x0f
]
def
encode_string
(
self
string
)
:
""
"Convert an up to 4 character length string containing 0-9, a-z,
space, dash, star to an array of segments, matching the length of the
source string."
""
segments
=
bytearray
(
len
(
string
)
)
for
i
in
range
(
len
(
string
)
)
:
segments
[
i
]
=
self
encode_char
(
string
[
i
]
)
return
segments
def
encode_char
(
self
char
)
:
""
"Convert a character 0-9, a-z, space, dash or star to a segment."
""
o
=
ord
(
char
)
if
o
==
:
return
_SEGMENTS
[
]
# space
if
o
==
:
return
_SEGMENTS
[
]
# star/degrees
if
o
==
:
return
_SEGMENTS
[
]
# dash
if
o
>=
and
o
<=
:
return
_SEGMENTS
[
o
]
# uppercase A-Z
if
o
>=
and
o
<=
:
return
_SEGMENTS
[
o
]
# lowercase a-z
if
o
>=
and
o
<=
:
return
_SEGMENTS
[
o
]
# 0-9
raise
ValueError
(
"Character out of range: {:d} '{:s}'"
format
(
o
chr
(
o
)
)
)
def
hex
(
self
val
)
:
""
"Display a hex value 0x0000 through 0xffff, right aligned."
""
string
=
'{:04x}'
format
(
val
&
0xffff
)
self
write
(
self
encode_string
(
string
)
)
def
number
(
self
num
)
:
""
"Display a numeric value -999 through 9999, right aligned."
""
# limit to range -999 to 9999
num
=
max
(
min
(
num
9999
)
)
string
=
'{0: >4d}'
format
(
num
)
self
write
(
self
encode_string
(
string
)
)
def
numbers
(
self
num1
num2
colon
=
True
)
:
""
"Display two numeric values -9 through 99, with leading zeros
and separated by a colon."
""
num1
=
max
(
min
(
num1
)
)
nu
In this tutorial, we will learn how to use the TM1637 4-digit 7-segment LED display with Raspberry Pi Pico. These displays are cheaper & best for displaying sensor data, time, stopwatch, random numbers, etc. It is similar to other 4-digit 7-segment displays but has a TM1637 LED Driver IC embedded in it. This removes the extra wiring and using only 2 wires the display can be controlled.
In this guide, we will learn about the method to use TM1637 with Raspberry Pi Pico using the MicroPython Code. We will display some text, random numbers, or anything you like using the TM1637 MicroPython library and code. Earlier we used
TM1637 with Arduino
to display various numbers and characters.
Components Required
In this guide, I used
Elecrow Raspberry Pi Pico Starter Kit
to test different Modules. You can buy the kit and perform some other operations as well. From this kit, you can use the following components.
Raspberry Pi Pico Board -1
TM1637 Module – 1
Breadboard -1
Jumper Wires – 4
Micro-USB Cable
TM1637 4-digit 7-segment LED display
The Bare 4-digit 7-segment displays usually require 12 connection pins. But the TM1637 IC is mounted on the back of the display module, which reduces the total wire to 4. Two pins are required for the power connections and the other two pins are used to control the segments.
The TM1637 module includes four 0.36″ segment 7-segment displays. The module has a ‘colon’ at the center useed to create clock or time-based projects. At the back of the display, there is an inexpensive serial LED driver from Titan MicroElectronics called
TM1637
. The TM1637 supports many functions – including ON/OFF and brightness control of the LEDs as well as access to each of the segments.
The module operates between 3.3V to 5V with a current consumption of 80mA. We can interface TM1637 with Raspberry Pi Pico or any other microcontroller using the two data pins. There is a dedicated MicroPython Library Library for TM1637 that removes the complexities and makes it easier to communicate with the display.
TM1637 Module Pinout
There is a 4-pin male header on the module for making connections.
1. GND:
Ground Pin
2. VCC:
3.3V to 5V power supply Pin
3. DIO:
Data Input/Output Pin
4. CLK:
Clock Input Pin
Interfacing TM1637 with Raspberry Pi Pico
Now let us interface the TM1637 4-Digit 7-Segment Display with the Raspberry Pi Pico Board using the MicroPython Code. Here is the connection diagram.
Connect the VCC, GND, DIO & CLK pin of TM1637 to 3.3V, GND, GP27 & GP26 of Raspberry Pi Pico.
MicroPython Code/Program
The TM1637 requires MicroPython library for interfacing with Raspberry Pi Pico. Therefore we have codes here as “
tm1637.py
” & “
main.py
“.
tm1637.py
Open your Thonny IDE and paste the following code in Thonny Editor. Save the file to Raspberry Pi Pico with the name
tm1637.py
from
micropython
import
const
from
machine
import
Pin
from
time
import
sleep_us
sleep_ms
TM1637_CMD1
=
const
(
)
# 0x40 data command
TM1637_CMD2
=
const
(
)
# 0xC0 address command
TM1637_CMD3
=
const
(
)
# 0x80 display control command
TM1637_DSP_ON
=
const
(
)
# 0x08 display on
TM1637_DELAY
=
const
(
)
# 10us delay between clk/dio pulses
TM1637_MSB
=
const
(
)
# msb is the decimal point or the colon depending on your display
# 0-9, a-z, blank, dash, star
_SEGMENTS
=
bytearray
(
b
'\x3F\x06\x5B\x4F\x66\x6D\x7D\x07\x7F\x6F\x77\x7C\x39\x5E\x79\x71\x3D\x76\x06\x1E\x76\x38\x55\x54\x3F\x73\x67\x50\x6D\x78\x3E\x1C\x2A\x76\x6E\x5B\x00\x40\x63'
)
class
TM1637
(
object
)
:
""
"Library for quad 7-segment LED modules based on the TM1637 LED driver."
""
def
__init__
(
self
clk
dio
brightness
=
)
:
self
clk
=
clk
self
dio
=
dio
if
not
<=
brightness
<=
:
raise
ValueError
(
"Brightness out of range"
)
self
_brightness
=
brightness
self
clk
init
(
Pin
OUT
value
=
)
self
dio
init
(
Pin
OUT
value
=
)
sleep_us
(
TM1637_DELAY
)
self
_write_data_cmd
(
)
self
_write_dsp_ctrl
(
)
def
_start
(
self
)
:
self
dio
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
def
_stop
(
self
)
:
self
dio
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
self
dio
(
)
def
_write_data_cmd
(
self
)
:
# automatic address increment, normal mode
self
_start
(
)
self
_write_byte
(
TM1637_CMD1
)
self
_stop
(
)
def
_write_dsp_ctrl
(
self
)
:
# display on, set brightness
self
_start
(
)
self
_write_byte
(
TM1637_CMD3
TM1637_DSP_ON
self
_brightness
)
self
_stop
(
)
def
_write_byte
(
self
b
)
:
for
i
in
range
(
)
:
self
dio
(
(
b
>>
i
)
&
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
self
clk
(
)
sleep_us
(
TM1637_DELAY
)
def
brightness
(
self
val
=
None
)
:
""
"Set the display brightness 0-7."
""
# brightness 0 = 1/16th pulse width
# brightness 7 = 14/16th pulse width
if
val
is
None
:
return
self
_brightness
if
not
<=
val
<=
:
raise
ValueError
(
"Brightness out of range"
)
self
_brightness
=
val
self
_write_data_cmd
(
)
self
_write_dsp_ctrl
(
)
def
write
(
self
segments
pos
=
)
:
""
"Display up to 6 segments moving right from a given position.
The MSB in the 2nd segment controls the colon between the 2nd
and 3rd segments."
""
if
not
<=
pos
<=
:
raise
ValueError
(
"Position out of range"
)
self
_write_data_cmd
(
)
self
_start
(
)
self
_write_byte
(
TM1637_CMD2
pos
)
for
seg
in
segments
:
self
_write_byte
(
seg
)
self
_stop
(
)
self
_write_dsp_ctrl
(
)
def
encode_digit
(
self
digit
)
:
""
"Convert a character 0-9, a-f to a segment."
""
return
_SEGMENTS
[
digit
&
0x0f
]
def
encode_string
(
self
string
)
:
""
"Convert an up to 4 character length string containing 0-9, a-z,
space, dash, star to an array of segments, matching the length of the
source string."
""
segments
=
bytearray
(
len
(
string
)
)
for
i
in
range
(
len
(
string
)
)
:
segments
[
i
]
=
self
encode_char
(
string
[
i
]
)
return
segments
def
encode_char
(
self
char
)
:
""
"Convert a character 0-9, a-z, space, dash or star to a segment."
""
o
=
ord
(
char
)
if
o
==
:
return
_SEGMENTS
[
]
# space
if
o
==
:
return
_SEGMENTS
[
]
# star/degrees
if
o
==
:
return
_SEGMENTS
[
]
# dash
if
o
>=
and
o
<=
:
return
_SEGMENTS
[
o
]
# uppercase A-Z
if
o
>=
and
o
<=
:
return
_SEGMENTS
[
o
]
# lowercase a-z
if
o
>=
and
o
<=
:
return
_SEGMENTS
[
o
]
# 0-9
raise
ValueError
(
"Character out of range: {:d} '{:s}'"
format
(
o
chr
(
o
)
)
)
def
hex
(
self
val
)
:
""
"Display a hex value 0x0000 through 0xffff, right aligned."
""
string
=
'{:04x}'
format
(
val
&
0xffff
)
self
write
(
self
encode_string
(
string
)
)
def
number
(
self
num
)
:
""
"Display a numeric value -999 through 9999, right aligned."
""
# limit to range -999 to 9999
num
=
max
(
min
(
num
9999
)
)
string
=
'{0: >4d}'
format
(
num
)
self
write
(
self
encode_string
(
string
)
)
def
numbers
(
self
num1
num2
colon
=
True
)
:
""
"Display two numeric values -9 through 99, with leading zeros
and separated by a colon."
""
num1
=
max
(
min
(
num1
)
)
nu
Technologies used
Hardware components
- Arduino
- Raspberry Pi
Software & tools
- Thonny
- MicroPython
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.