HOW to Display Numbers on 4-Digit Tube with ESP32
Overview
This module is mainly composed of a 0.36 inch red common cathode 4-digit digital tube, and its driver chip is TM1650. When using it, we only need two signal lines to make the single-chip microcomputer control a 4-bitdigit tube, which greatly saves the IO port resources of the control board.
TM1650 is a special circuit for LED (light emitting diode display) drive control. It integrates MCU input and output control digital interface, data latch, LED drivers, keyboard scanning, brightness adjustment and other circuits.
TM1650 has stable performance, reliable quality and strong anti-interference ability.
It can be applied to the application of long-term continuous working for 24 hours.
TM1650 uses 2-wire serial transmission protocol for communication (note that this data transmission protocol is not a standard I2C protocol). The chip can drive the digital tube and save MCU pin resources through two pins and MCU communication.
Working Principle
TM1650 adopts IIC treaty, which uses DIO and CLK buses.
Data command setting: 0x48 means that we light up the digital tube, instead of enable the function of key scanning
Command display setting:
bit[6:4]: set the brightness of tube display, and 000 is brightest
bit[3]: set to show decimal points
bit[0]: start the display of the tube display
Components
Connection Diagram
Test Code
//**********************************************************************************
/*
* Description : TM1650 Four Digital Tube shows 0-9999
* Auther : http//www.keyestudio.com
*/
#include "TM1650.h"
#define CLK 22 //pins definitions for TM1650 and can be changed to other ports
#define DIO 21
TM1650 DigitalTube(CLK,DIO);
void setup(){
DigitalTube.setBrightness(); //set brightness, 0---7, default : 2
DigitalTube.displayOnOFF(); //display on or off, 0=display off, 1=display on, default : 1
for(char b=1;b<5;b++){
DigitalTube.clearBit(b); //DigitalTube.clearBit(0 to 3); Clear bit display.
}
// DigitalTube.displayDot(1,true); //Bit0 display dot. Use before displayBit().
DigitalTube.displayBit(1,0); //DigitalTube.Display(bit,number); bit=0---3 number=0---9
}
void loop(){
for(int num=0; num<10000; num++){
displayFloatNum(num);
delay(100);
}
}
void displayFloatNum(float num){
if(num > 9999)
return;
int dat = num*10;
//DigitalTube.displayDot(2,true); //Bit0 display dot. Use before displayBit().
if(dat/10000 != 0){
DigitalTube.displayBit(1, dat%100000/10000);
DigitalTube.displayBit(2, dat%10000/1000);
DigitalTube.displayBit(3, dat%1000/100);
DigitalTube.displayBit(4, dat%100/10);
return;
}
if(dat%10000/1000 != 0){
DigitalTube.clearBit(1);
DigitalTube.displayBit(2, dat%10000/1000);
DigitalTube.displayBit(3, dat%1000/100);
DigitalTube.displayBit(4, dat%100/10);
return;
}
if(dat%1000/100 != 0){
DigitalTube.clearBit(1);
DigitalTube.clearBit(2);
DigitalTube.displayBit(3, dat%1000/100);
DigitalTube.displayBit(4, dat%100/10);
return;
}
DigitalTube.clearBit(1);
DigitalTube.clearBit(2);
DigitalTube.clearBit(3);
DigitalTube.displayBit(4, dat%100/10);
}
//**********************************************************************************
Test Result
Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on. The 4-digit tube display will show integer from 0 to 99999, add 1 for each 10ms. Increase to 9999 then start from 0.