Often, viewers of my YouTube channel or the readers of this blog will let me know if new devices that might improve or add new features to my existing projects. I've used this information to make upgrades to my stair lighting system and my garage multi-sensor. But occasionally, they will share a device that I haven't used... but it is so cool, I just make up a reason to use it. The TM1638 is one of those devices!
The TM1638 is an eight digit, seven-segment display. It also has eight independently controllable LED lights and eight push buttons that can be used for a lot of different purposes. Only 3 wires in addition to +5VDC and ground are needed. But of course, this needs to be connected to a controller. A Wemos D1 Mini is a good choice, but it can be connected pretty much to any ESP or Arduino board... or even a Raspberry Pi. I'll be using a Wemos D1 mini. You can also watch a YouTube video of this project.
Parts List
The parts list is pretty short for this project!
Note: Some of these links may be Amazon affiliate links. Use of these links will not affect your pricing, but this blog may earn a small commission if you make a purchase. Use of these Amazon links is one the best ways to show your support for the blog... at absolutely no cost to you!
Pretty much any microUSB cable and 5V charger will work. If you have a cable and old phone charger, you probably do not need to purchase the ones listed above. You may need a USB cable that supports data for the initial setup, but after that, just a cord that supplies power will suffice.
I did create a 3D printed enclosure for mine, but this is not absolutely necessary if you do not have access to a 3D printer. You can find small project boxes online or even use something like a plastic soap dish as an enclosure. You can find the 3D design files for my enclosure in the Github repository that also includes the Arudino code (more on this in a bit).
But note that there are many different models/sellers of the TM1638 module. Due to this, there are very slight variations in the dimensions, particularly for the mounting holes and the top LED spacing. The 3D printed enclosure that I am linking to is designed for the exact model linked above and a Wemos D1 Mini. If you opt for a different model or for a different controller (like an ESP32), the 3D printed enclosure may need to be modified.
Desodering/Replacing pin headers
Unfortunately, the TM1638 board comes with pins already soldered on.
This configuration would interfere with the planned enclosure and mean that wiring would have to protrude out of the front of the device. Ideally, the board would have been delivered with header pins, but not soldered on so you could select to have the pins protruding out the front or the back (like with a D1 Mini and many other components). But that wasn't the case, so the first step was to carefully desolder these pins from the board.
If you have a desoldering station or have done a lot of desoldering before, you can probably just skip this section and move straight on to the wiring. But if you haven't desoldered pins before by just using a soldering iron, I'll show you the technique I use so that it might help you out a bit.
The first thing I do is gently remove the plastic housing that holds the five pins together. That way, I can apply heat from the soldering iron and remove one pin at a time instead of trying to heat all five pins at the same time. If you use some wire snips on each end to just crack the plastic a bit, you should be able to gently pry up and remove the plastic block with a small screwdriver. Just don't apply too much pressure and crack or damage the PCB. Just keep working at it and eventually it will slide off.
Now flip the board over, grab a pair of needle nose pliers and heat up your soldering iron.
Apply just enough heat to melt the solder and gently pull down with the pliers (don't try to use your fingers... that little pin will got hot FAST!). Repeat this until all five pins are removed.
Odds are, the holes will be plugged with a bit of solder that was left behind. This solder will need to be removed so that new pins (or wire) can be soldered in place. There are a number of ways to go about this, but I've found that for me, a simple desoldering pump
like this one works best.
Simply depress the spring on the pump, align the nozzle on one side of the hole and apply the soldering iron for just a couple of seconds on the other side. Once the solder melts, just push the button on the pump and the molten solder will be sucked right out of the hole!
It might take you a couple of times on each hole, and you might also need to flip the board over and apply the soldering iron and pump on the opposite sides, but the solder really should come out pretty easily. You can also try solder wick that is designed to pull out solder when heated, but I've had better luck just using a pump.
You can now solder new pins, either straight pins or right-angled, onto the board. Or you can forgo new pins altogether and just solder your wires straight into the holes.
If you are planning on using the 3D printed case I am sharing here, then you'll want to use the right-angled pins as straight pins will be too tall and bump into the D1 Mini. In fact, I recommend that you don't use pins at all and solder wire directly through the holes. Regardless, at this point the board prep is done and it's time to wire things up.
Wiring
The wiring is pretty simple and straightforward. Again, I did not use any header pins on the D1 Mini and soldered the wire directly through the holes on the D1 Mini like I did on the TM1638. In fact, if you put header pins on the D1 Mini, it will not fit in the 3D printed enclosure.
Wire VCC on the TM1638 to the 3V3 pin* on the D1 Mini and GND to G. Then make the following connections:
- STB to D5
- CLK to D6
- DIO to D7
I selected these particular pins because they are not pulled high or low at boot time. D1 or D2 could be used as well. If you opt to use different pins, then you will need to modify the ESPHome and/or Arduino code to reflect your pin use.
*Also note that while the board is rated for 5V, it doesn't appear to be any regulator on the output pins (that I could find in a spec sheet), meaning that if the board is fed 5V in may output 5V on the STB, CLK and DIO pins. Since the D1 Mini GPIO pins are only rated for 3.3V, this could be a problem! The TM1638 operates just fine on 3.3V, so this is the safest bet to assure that 5V isn't fed to the D1 Mini's GPIO pins. If anyone reading this can find an official spec sheet on this board (not the TM1638 chip itself, but this board) that shows the output voltage of the pins based on the source voltage, please let me know down in the comments!
If you plan on using my 3D printed enclosure (or a similar design, try to keep the wiring fairly short... no more than 2"-3". Also note that the wire for the D1 Mini should be soldered so that the wiring is on the back side of the D1 Mini... in other words, the actual ESP chip should be facing away from the TM1638. This is because the enclosure is designed with a few ventilation slots for the ESP chip, so the D1 Mini will be installed with the actual processor chip facing down towards the bottom of the enclosure.
Installing in 3D printed Enclosure
Again, if not using my 3D enclosure, you can just skip this section.
The enclosure is designed for M2 x 10 or M2 x 12 screws. I added small nuts to the back side of the M2 screws to assure the TM1638 stays flush against the front face place.
Place the D1 Mini in the mount, with the USB port facing the side opening and the ESP chip facing downwards over the ventilation slots.
Carefully tuck all wiring inside the box and screw the faceplate to the base. It's now time to install the code. I'm providing two different options here, each with their own pros and cons.
Arduino with MQTT vs. ESPHome
While I'm going to cover both Arduino/C++ and ESPHome code for the TM1638 and ESP, these are not the only options. Tasmota also has a template for the TM1638 (see it here), although at the time of this article, it did not support the buttons or individual LEDs. What I am covering are just a couple of examples and starting points for your own projects.
Whether you opt to use ESPHome, Arduino code (or something else altogether) will depend upon your intended use, integration needs with Home Assistant and your comfort level with different coding languages.
ESPHome
ESPHome, at the time of this writing, has support for the TM1638. However, output to the 7-segment display has to be done using lambas, basically sections of C++ code that formats the data in a certain way, using functions such as printf, like this:
printf(0, "%.1f", id(my_sensor).state)
or
printf("S%3.0f", id(my_sensor).state)
If you are familiar with C++, this is probably not a big deal. But if this seems a little foreign, or if you are more comfortable with MQTT, then the Arduino code might be a better choice.
On the plus side, ESPHome supports the 8 individual LEDs as switches or outputs and the 8 pushbuttons as binary sensors, making it very easy to use these in Home Assistant automations.
You can find all the details on how to use the TM1638 with ESPHome, along with installation, on the ESPHome web site so I'm not going to repeat those details here.
Arduino Code
The Arduino sketch that I've provided makes use of MQTT for integration and control from Home Assistant. It provides connection to your wifi, MQTT broker and after initial installation, over-the-air (OTA) updates. It provides some wrappers around the excellent tm1638plus library for use with MQTT.
Here are some of the MQTT topics and payloads you can use to interact with the TM1638. This list may grow and expand in future versions:
Topics published from Home Assistant or other MQTT system to control the TM1638 and/or display data.
Topic | Payload | Description |
/brightness | 0 -7 | Sets the brightness of the display |
/cleardisplay | 1 | Clears the 7-segment display |
/reset | 1 | Clears the 7-segment display and all LEDs |
/displaytext | cccccccc | Displays up to eight numbers or characters on the display |
/displaychar | n, c | Displays a single character at position n |
/displaychardot | n, c | Displays a single character at position n with decimal |
/setled | nn | Turn led at position ‘n’ on (1) or off (0) |
/setleds | nnnnnnnn | Set all LED to on or off (e.g. 01001010 would turn on LEDs 2, 5 and 7, while turning the others off) |
/hatime | hh:mm | Publish time for display (e.g. clock mode) |
/hadate | yyyy-mm-dd | Publish date for display (e.g. date mode) |
/mode | 0 - x | Set the display to a particular display mode (3 included but you can add to that). |
Using Home Assistant, sending a temperature from a sensor to the display would look something like this:
action:
- service: mqtt.publish
data:
topic: "cmnd/tm1638/displaytext"
payload: "{{ states('sensor.mytemperature') }}
This can also be done in the same manner using the Home Assistant automation UI:
The TM1638 will also publish some MQTT topics that can be used in Home Assistant automations as well.
Topics published by the TM1638 that can be used in Home Assistant or other MQTT-enabled automation platforms
Topic
|
Payload
|
Description
|
/buttons
|
nnn
|
Returns a decimal
value for which button(s) are pressed
|
/brightness
|
n
|
Returns the
current brightness level of the display
|
/mode
|
n
|
Returns the
current display mode
|
So, an automation in Home Assistant using a trigger when button two is pressed on the TM1638 might look something like this:
- trigger:
platform: mqtt
topic: "../buttons"
payload: "2"
The Arudino code will also require you to use the Arduino IDE (or other editor) to specify your wifi and MQTT broker information and upload via USB the first time.
You can find full documentation on the Arduino code, along with additional Home Assistant automation examples using the TM1638, in the Github respository.
Whether you opt to use ESPHome, Arduino or something else will depend on your intended use and comfort level with the different technologies. If you have familiarity with Arduino code or MQTT, then the Arduino code might be a great starting framework for your own project. However, if Arduino code is completely foreign, then ESPHome or even Tasmota might be a better choice.
Conclusion
The TM1638 is a pretty low-cost and versatile board for displaying information and providing multiple push buttons for your home automation needs, with lots of flexibility and an easy-to-wire configuration. Let me know down in the comments how you are using the TM1638 display!
Links
Supporting this blog and related YouTube channel
If you'd like to support future content on this blog and the related YouTube channel, or just say thanks for something that helped you out, you can use any of my Amazon links to make a purchase at absolutely no cost to you. Or if you prefer to say thanks directly, you can buy me a one-off cup of coffee at:
No comments:
Post a Comment
To help eliminate spam and to keep all conversations civil, submitted comments are moderated. Therefore your post may not show up immediately. Please be patient as most reviews are completed within a few hours.