Using with Python

In order to interact with the display, you must have permissions to control the GPIO pins on the Raspberry Pi. This typically means you need to run as the root user.

Initialise the display

In order to connect to the display, you can use the defaults from the standalone program and use GPIO 0 for data, and GPIO 1 for clock. You can do this with:

>>> from tm1640 import *
>>> display = TM1640()

If the library is not installed correctly, this will throw an error:

Traceback (most recent call last):
  File "tm1640.py", line 37, in __init__
    raise ImportError, 'Could not find libtm1640 in PATH'
ImportError: Could not find libtm1640 in PATH

If this occurs, make sure that libtm1640.so is in your dynamic loader’s path. This typically means it should be located at /usr/lib/libtm1640.so, and you need to refresh the dynamic loader’s cache by executing ldconfig. For more information, see ld.so(8).

If you wish to use a different set of display pins, you can set this in the TM1640 constructor as follows:

>>> display = TM1640(clock_pin=3, data_pin=2)

You must then switch on the display. By default, the Python library will turn on the display at maximum brightness with the TM1640.on() method:

>>> display.on()

You can also set a specific brightness. To turn on the display at brightness level 3 (about half brightness):

>>> display.on(3)

Clear the display

If you turn on the display, it may have the contents of whatever was in the TM1640 IC’s memory last time it was used. You can clear the display with:

>>> display.clear()

Turn off the display

In order to turn off the display, and keep the contents of the display in memory, you can issue the off command:

>>> display.off()

Write to the display

You can write up to 16 characters to the display at once. If you write less than 16 characters, it will leave the cells that you have not specified in the state that they currently are in.

For example:

>>> display.write('1234567890123456')

To print the current date and time to the screen, you could use datetime.datetime.strftime():

>>> import datetime
>>> display.write(datetime.datetime.now().strftime('%Y-%m-%d  %H%M'))

Note

Unlike regular PCs, the Raspberry Pi lacks a backup battery for it’s clock, so will reset back to 1970-01-01 00:00 UTC on losing power. In order to display accurate time, the Pi’s clock will need to be synchronised with an external source, for example, an NTP server.

Writing to part of the display at an offset

You can update parts of the screen instead of the entire display at once. This allows you to push updates partial updates to the screen from different parts of your program, or improve the speed of updates.

In the previous example, you could update the time only in the previous example with:

>>> display.write(datetime.datetime.now().strftime('%H%M'), offset=12)

Vertically-inverted mode

If you are viewing the display in a mirror, you can invert the display vertically, using the tm1640.INVERT_MODE_VERTICAL parameter:

>>> display.write('1234567890123456', invert_mode=tm1640.INVERT_MODE_VERTICAL)

Class Reference

src/python/tm1640.py - Python interface shim for libtm1640. Copyright 2013 Michael Farrell <http://micolous.id.au/>

This program is free software: you can redistribute it and/or modify it under the terms of the version 3 GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

class tm1640.TM1640(clock_pin=1, data_pin=0)

Bases: object

Interface class for connecting to a TM1640 display IC over the Raspberry Pi’s GPIO pins.

Initialises a connection to the TM1640 display.

Parameters:
  • clock_pin (int) – WiringPi GPIO that has the clock pin plugged into.
  • data_pin (int) – WiringPi GPIO that has the data pin plugged into.
Throws ImportError:
 

If libtm1640.so could not be found by your dynamic linker. Typically libtm1640.so should be installed in to /usr/lib.

Throws Exception:
 

If there is a problem accessing BCM GPIO, typically caused by lack of permissions.

clear()

Clears the contents of the display.

off()

Switches off the display, retaining the contents of the display in the controller’s memory.

on(brightness=7)

Turns on the display.

Parameters:brightness – The brightness level to set on the display, between 1 and 7. 1 is dimmest, 7 is brightest.
write(string, offset=0, invert_mode=0)

Writes a string to the display.

String must be less than 16 bytes, less any offset bytes.

Throws an exception on error.

Parameters:
  • string (str) – String to write to the display
  • offset (int) – Where to start writing the string to on the display.
  • invert_mode – How to invert the display segments. This must be set to one of INVERT_MODE_NONE or INVERT_MODE_VERTICAL.
tm1640.INVERT_MODE_NONE = 0

Used by TM1640.write() to indicate that the display output should be sent normally.

tm1640.INVERT_MODE_VERTICAL = 1

Used by TM1640.write() to indicate that the display output segments should be flipped vertically, in order to be displayed correctly through a mirror.