ESP32 and TM1637 LED DIsplay

Tgibbs99
Posts: 2
Joined: Wed May 26, 2021 10:15 pm

ESP32 and TM1637 LED DIsplay

Postby Tgibbs99 » Sat Sep 04, 2021 10:18 pm

see "Display empty on ESP32 #15" for previous discussion.

Hey All,

Anyone been to drive a TM1637 LED display successfully with ESP32?

My display and sketch work fine on Arduino, but when moving over to ESP32 and arduino IDE, the sketch complies, but the LED remains blank. (Same as previous post 5 years ago!) I have exactly the same problem now.

code:

/*
* Example for 6-digit TM1637 based segment Display
* The number of milliseconds after start will be displayed on the 6-digit screen
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*sxxaxaxa
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "TM1637_6D.h"

#define CLK 21 //pins definitions for TM1637 and can be changed to other ports
#define DIO 22

TM1637_6D tm1637_6D(CLK,DIO);

void setup()
{
tm1637_6D.init();
// You can set the brightness level from 0(darkest) till 7(brightest) or use one
// of the predefined brightness levels below
tm1637_6D.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
}
void loop()
{
// Print random float values to the display
tm1637_6D.displayFloat(-1.2345);
delay(2000);
tm1637_6D.displayFloat(987.2345);
delay(2000);
tm1637_6D.displayFloat(1.2345);
delay(2000);
tm1637_6D.displayFloat(192837);
delay(2000);
tm1637_6D.displayFloat(0);
delay(2000);
}

Any help greatly appreciated!

Tim

sporniket
Posts: 4
Joined: Mon Aug 23, 2021 5:52 pm

Re: ESP32 and TM1637 LED DIsplay

Postby sporniket » Tue May 02, 2023 5:17 am

Hello, I guess the original poster has moved on, but trying to do it since yesterday and just having succeeded now, I believe that it is usefull to post here.

First, I did not use existing libraries that do bit banging, but I used standard i2c driver. With pull-up resistors of 270 ohms, I could use a serial clock of 420KHz (I lost data at 430KHz, and I did not try values in between).

Second, by default the controller send data with the MSB first, and I could not succeed in changing to sending the LSB first, so I have to inverse the bits myself.

Now the snippets, straight from my code, first the setting up of I2C, nothing surprising, just that I used explicit value for the clock speed for the sake of testing :

Code: Select all

  // -- i2c
  int i2c_master_port = 0;
  i2c_config_t conf = {
    .mode = I2C_MODE_MASTER,
    .sda_io_num = CONFIG_PIN_IIC_1_SDA,         // select SDA GPIO specific to your project
    .scl_io_num = CONFIG_PIN_IIC_1_SCL,         // select SCL GPIO specific to your project
    .sda_pullup_en = GPIO_PULLUP_DISABLE,
    .scl_pullup_en = GPIO_PULLUP_DISABLE,
    .master = {.clk_speed = 420000/*CONFIG_IIC_1_CLK_FREQ_HZ*/},  // select frequency specific to your project
    .clk_flags = 0,                          // optional; you can use I2C_SCLK_SRC_FLAG_* flags to choose i2c source clock here
  };

  i2c_param_config(i2c_master_port, &conf);

  ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode, 0, 0, 0));



Then the snippet to setup the display of "00 00", the principle is to send each part separately :

Code: Select all

          // sample i2c sequence
          int i2c_master_port = 0;

          // -- sample command
          uint8_t comm1 = 0x02 ; //reversed bits of 0x40
          uint8_t comm2 = 0x03 ; //reversed bits of 0xC0
          uint8_t comm3 = 0xf1 ; //reversed bits of 0x8f
          bool ack_mode = true ;
          uint8_t demo1[] = {
            comm1
          } ;
          uint8_t demo2[] = {
            comm2,0xfc,0xfc,0xfc,0xfc
          } ;
          uint8_t demo3[] = {
            comm3
          } ;
          i2c_cmd_handle_t cmd ;
          cmd = i2c_cmd_link_create();
          i2c_master_start(cmd) ;
          i2c_master_write(cmd,demo1,1,ack_mode);
          i2c_master_stop(cmd);
          ESP_ERROR_CHECK(i2c_master_cmd_begin(i2c_master_port, cmd,10));
          i2c_cmd_link_delete(cmd);
          ESP_LOGI(TAG_MAIN, "done i2c command 1");

          cmd = i2c_cmd_link_create();
          i2c_master_start(cmd) ;
          i2c_master_write(cmd,demo2,7,ack_mode);
          i2c_master_stop(cmd);
          ESP_ERROR_CHECK(i2c_master_cmd_begin(i2c_master_port, cmd,10));
          i2c_cmd_link_delete(cmd);
          ESP_LOGI(TAG_MAIN, "done i2c command 2");

          cmd = i2c_cmd_link_create();
          i2c_master_start(cmd) ;
          i2c_master_write(cmd,demo3,1,ack_mode);
          i2c_master_stop(cmd);
          ESP_ERROR_CHECK(i2c_master_cmd_begin(i2c_master_port, cmd,10));
          i2c_cmd_link_delete(cmd);
          ESP_LOGI(TAG_MAIN, "done i2c command 3");


sporniket
Posts: 4
Joined: Mon Aug 23, 2021 5:52 pm

Re: ESP32 and TM1637 LED DIsplay

Postby sporniket » Wed May 03, 2023 5:56 am

sporniket wrote: Second, by default the controller send data with the MSB first, and I could not succeed in changing to sending the LSB first, so I have to inverse the bits myself.
Today I finally saw how I failed to set data mode to LSB, here is updated code with normal values for commands.

setup, I use 'i2c_set_data_mode' after 'i2c_param_config':

Code: Select all

  // -- i2c
  int i2c_master_port = 0;
  i2c_config_t conf = {
    .mode = I2C_MODE_MASTER,
    .sda_io_num = CONFIG_PIN_IIC_1_SDA,         // select SDA GPIO specific to your project
    .scl_io_num = CONFIG_PIN_IIC_1_SCL,         // select SCL GPIO specific to your project
    .sda_pullup_en = GPIO_PULLUP_DISABLE,
    .scl_pullup_en = GPIO_PULLUP_DISABLE,
    .master = {.clk_speed = 420000/*CONFIG_IIC_1_CLK_FREQ_HZ*/},  // select frequency specific to your project
    .clk_flags = 0,                          // optional; you can use I2C_SCLK_SRC_FLAG_* flags to choose i2c source clock here
  };

  i2c_param_config(i2c_master_port, &conf);
  
  // Set data mode to LSB
  ESP_ERROR_CHECK(i2c_set_data_mode(i2c_port, I2C_DATA_MODE_LSB_FIRST, I2C_DATA_MODE_LSB_FIRST));

  ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode, 0, 0, 0));



Send display data, now using "normal" values (not manually bit reversed values), now it display "bb:bb" :

Code: Select all

          // sample i2c sequence
          int i2c_master_port = 0;

          // -- sample command
          uint8_t comm1 = 0x40; //Command : Set a batch of digits
          uint8_t comm2 = 0xc0; //Address : digit #0
          uint8_t comm3 = 0x8b; //Brightness : ON + level 3/7
          bool ack_mode = true ;
          uint8_t demo1[] = {
            comm1
          } ;
          uint8_t demo2[] = {
             // LSB : 4 times 'b' with dot ; MSB : 4 times '0' without dot
            comm2,0xfc,0xfc,0xfc,0xfc
          } ;
          uint8_t demo3[] = {
            comm3
          } ;
          i2c_cmd_handle_t cmd ;
          cmd = i2c_cmd_link_create();
          i2c_master_start(cmd) ;
          i2c_master_write(cmd,demo1,1,ack_mode);
          i2c_master_stop(cmd);
          ESP_ERROR_CHECK(i2c_master_cmd_begin(i2c_master_port, cmd,10));
          i2c_cmd_link_delete(cmd);
          ESP_LOGI(TAG_MAIN, "done i2c command 1");

          cmd = i2c_cmd_link_create();
          i2c_master_start(cmd) ;
          i2c_master_write(cmd,demo2,7,ack_mode);
          i2c_master_stop(cmd);
          ESP_ERROR_CHECK(i2c_master_cmd_begin(i2c_master_port, cmd,10));
          i2c_cmd_link_delete(cmd);
          ESP_LOGI(TAG_MAIN, "done i2c command 2");

          cmd = i2c_cmd_link_create();
          i2c_master_start(cmd) ;
          i2c_master_write(cmd,demo3,1,ack_mode);
          i2c_master_stop(cmd);
          ESP_ERROR_CHECK(i2c_master_cmd_begin(i2c_master_port, cmd,10));
          i2c_cmd_link_delete(cmd);
          ESP_LOGI(TAG_MAIN, "done i2c command 3");

Those snipped are from my sandbox project here : https://github.com/sporniket/esp32-idf- ... b354adc11a

Remainder : I reached 420KHz clock using 270ohms as pull-up resistors.

bidrohini
Posts: 202
Joined: Thu Oct 27, 2022 12:55 pm

Re: ESP32 and TM1637 LED DIsplay

Postby bidrohini » Wed May 03, 2023 9:18 am

This is a tutorial on interfacing TM1637 with ESP32. You can check this out.
https://youtu.be/fKMeP0dKEPU

Who is online

Users browsing this forum: No registered users and 286 guests