Tuesday, July 11, 2017

TM1638 8 bit digital Led module (1)


On Aliexpress i found this Key Display "For AVR Arduino" "New" 8-Bit Digital LED Tube 8-Bit TM1638 Module. I did put some quotes" in the name as this module is not for Arduino only, it can be used also in projects with other microcontrollers e.g. the ESP8266.
TM1638 module - Front
For this module i payed 2017Q1 only € 1,32 . Relative cheap for a module that only needs 3 lines from your microcontroller and gives you
  • 8 keys
  • 8 LEDs common cathode
  • 8 a digital 7 segment displays
The magic is done using the TM1638 digital tube drive chip.
For the wiring connect VCC GND to a 5V supply and connect STB CLK DIO to the microcontroller IO ports. Scanning display and key scan don't need microcontroller intervention, only need to register to display data related to reading and writing or testing buttons, save MCU resources.
The only disadvantage i found is, when putting this module in an enclosure the female connections pins of this module are pointing to the front. Depending of the enclosure you need to remove, bend or put them on the back side of the module.

I tested it with an Arduino (clone).
Sketch to test the display:
/*
 * Testing the TM1638 board
 Display test:
Programs dislights some leds and display
See code for details
Hardware connections: 
 Arduino              TM1638 based board
 3.3V   ------------------ VCC
 GND    ------------------ GND
 PIN #7 ------------------ STB
 PIN #8 ------------------ DIO
 PIN #9 ------------------ CLK

 Source
 https://blog.3d-logic.com/2015/01/10/using-a-tm1638-based-board-with-arduino/
 8 july 2017  JanJeronimus
*/

const int strobe = 7;
const int clock = 9;
const int data = 8;

void sendCommand(uint8_t value)
{
  digitalWrite(strobe, LOW);
  shiftOut(data, clock, LSBFIRST, value);
  digitalWrite(strobe, HIGH);
}

void reset()
{
  sendCommand(0x40); // set auto increment mode
  digitalWrite(strobe, LOW);
  shiftOut(data, clock, LSBFIRST, 0xc0);   // set starting address to 0
  for(uint8_t i = 0; i < 16; i++)
  {
    shiftOut(data, clock, LSBFIRST, 0x00);
  }
  digitalWrite(strobe, HIGH);
}

void setup()
{
  pinMode(strobe, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(data, OUTPUT);

  sendCommand(0x8f);  // activate and set brightness to max
  reset();
}

void loop()
{
  sendCommand(0x44);  // set single address

  digitalWrite(strobe, LOW);
  shiftOut(data, clock, LSBFIRST, 0xc0); // 1st digit
  shiftOut(data, clock, LSBFIRST, 0xff);
  digitalWrite(strobe, HIGH);

  digitalWrite(strobe, LOW);
  shiftOut(data, clock, LSBFIRST, 0xc5); // 3rd LED
  shiftOut(data, clock, LSBFIRST, 0x01);
  digitalWrite(strobe, HIGH);

  digitalWrite(strobe, LOW);
  shiftOut(data, clock, LSBFIRST, 0xcb); // 3rd LED
  shiftOut(data, clock, LSBFIRST, 0x01);
  digitalWrite(strobe, HIGH);

  digitalWrite(strobe, LOW);
  shiftOut(data, clock, LSBFIRST, 0xce); // last digit
  shiftOut(data, clock, LSBFIRST, 0xff);
  digitalWrite(strobe, HIGH);
}

Arduino Uno (clone) with TM1638 display (and key) module.


Sketch to test input keys
./*
 * Testing the TM1638 board
 Button test:
 Progam scans buttons. If button pressed the led over the button will be lit
Hardware connections: 
 Arduino              TM1638 based board
 3.3V   ------------------ VCC
 GND    ------------------ GND
 PIN #7 ------------------ STB
 PIN #8 ------------------ DIO
 PIN #9 ------------------ CLK

 Source
 https://blog.3d-logic.com/2015/01/10/using-a-tm1638-based-board-with-arduino/
 8 july 2017  JanJeronimus
*/

const int strobe = 7;
const int clock = 9;
const int data = 8;

void sendCommand(uint8_t value)
{
  digitalWrite(strobe, LOW);
  shiftOut(data, clock, LSBFIRST, value);
  digitalWrite(strobe, HIGH);
}

void reset()
{
  sendCommand(0x40); // set auto increment mode
  digitalWrite(strobe, LOW);
  shiftOut(data, clock, LSBFIRST, 0xc0);   // set starting address to 0
  for(uint8_t i = 0; i < 16; i++)
  {
    shiftOut(data, clock, LSBFIRST, 0x00);
  }
  digitalWrite(strobe, HIGH);
}

void setup()
{
  pinMode(strobe, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(data, OUTPUT);

  sendCommand(0x8f);  // activate and set brightness to max
  reset();
}

uint8_t readButtons(void)
{
  uint8_t buttons = 0;
  digitalWrite(strobe, LOW);
  shiftOut(data, clock, LSBFIRST, 0x42);

  pinMode(data, INPUT);

  for (uint8_t i = 0; i < 4; i++)
  {
    uint8_t v = shiftIn(data, clock, LSBFIRST) << i;
    buttons |= v;
  }

  pinMode(data, OUTPUT);
  digitalWrite(strobe, HIGH);
  return buttons;
}

void setLed(uint8_t value, uint8_t position)
{
  pinMode(data, OUTPUT);

  sendCommand(0x44);
  digitalWrite(strobe, LOW);
  shiftOut(data, clock, LSBFIRST, 0xC1 + (position << 1));
  shiftOut(data, clock, LSBFIRST, value);
  digitalWrite(strobe, HIGH);
}

void loop()
{
  uint8_t buttons = readButtons();

  for(uint8_t position = 0; position < 8; position++)
  {
    uint8_t mask = 0x1 << position;

    setLed(buttons & mask ? 1 : 0, position);
  }
}

Back side
There exists also a library to easy program this module.

No comments: