powered by TinkerSpace.in
Join free
TinkerPy Tutorial ๐Ÿ“˜ Part 2 ยท Coder ยท Chapter 21 of 24

OLED & LCD Displays

A gadget that can show things feels alive: a name badge, a live thermometer, robot eyes that blink and smile. Learn every function of the little OLED screen and the classic 16ร—2 LCD.

๐Ÿ“–Key words

OLED display

A small screen (0.96 inch) with 4 text lines. Every pixel makes its own light. It is switched on in every new project: import oled_display and oled_begin() are already there.

16ร—2 LCD

A screen with 2 lines of 16 letters and a backlight. Tick 16x2 LCD in Circuit Connections: it adds import lcd_16x2 and lcd_begin(0x27).

I2C

Both screens use only two wires: o7 (SDA) and o8 (SCL). Each screen has an address โ€” the OLED 0x3C, most LCDs 0x27 or 0x3F.

Line and position

The OLED picks a line (1โ€“4) and an align (left, center, right). The LCD picks a line (1โ€“2) and a position (1โ€“16) where the text starts.

Animation

A moving picture that fills the OLED and plays by itself while your code runs: robot eyes or an emoji.

๐ŸŽฌSee it work

Write on the OLED

Type a message, pick a line and watch the oled_write() code and the screen change.

OLED or LCD?

Switch between the two screens and pick the right one for your project.

๐Ÿง How it works

Writing on the OLED

  • oled_write(line, align, size, weight, value) โ€” line 1โ€“4; align left / center / right; size small (21 letters, 1 line), medium (10 letters, 2 lines) or large (7 letters, 2 lines); weight regular / bold.
  • The value can be text in quotes, a number, a variable or a join: oled_write(2, center, large, bold, t + " C") (joins need firmware 2.24).
  • Each oled_write replaces the whole line. Medium and large use the line below too, so start them on line 1, 2 or 3.
  • oled_clear() wipes the screen, oled_clear_line(3) wipes one line. oled_begin() starts it (0x3C; a few OLEDs need oled_begin(0x3D)).

The 16ร—2 LCD

  • lcd_write(1, 1, "Temp:") writes on line 1 from place 1; lcd_write(1, 7, t) puts the number at place 7 โ€” a label and a number on one line.
  • lcd_clear() wipes both lines, lcd_clear_line(2) wipes one.
  • Old letters stay until something covers them: 100 then 99 shows โ€œ990โ€. Clear the line first, or add spaces after the number.
  • lcd_backlight(OFF) darkens the screen (the text stays); lcd_backlight(ON) lights it again.
  • If nothing shows, try lcd_begin(0x3F) โ€” or lcd_begin() and the board finds the address itself.

Scrolling and animations

  • oled_scroll(4, "Too hot! Drink water") or lcd_scroll(1, "โ€ฆ") keeps long text moving along a line by itself โ€” until that line is written or cleared. Call it once, not every loop.
  • oled_eye_animation("blink", 0, 5, 2000): name, repeat (0 = for ever), speed 1โ€“10, pause between repeats in ms. Eyes: blink, look_left, look_right, look_up, look_down, look_around, happy, sad, angry, sleepy, surprised, wink, love, dizzy, suspicious, crying, confused, wake_up.
  • oled_emoji_animation("heart", 0, 6, 0): smile, laugh, wink, love, sad, cry, angry, surprised, sleepy, cool, tongue, heart, star, thinking, kiss.
  • An animation plays while your code runs; oled_write or oled_clear stop it. See them all on the board: Menu โ†’ Animations.

๐Ÿ’ปCode

# Add the libraries you need
import oled_display

# Give your pins easy-to-remember names
i1 = set_name("hello_btn")

# ===== Digital name badge =====

# This code block executes only once
setup:
    oled_begin()  # starts the OLED
    o7 = set_function(SDA)  # cannot be changed
    o8 = set_function(SCL)  # cannot be changed
    hello_btn = set_input(push_button, active_low)
    oled_write(1, center, large, bold, "ASHA")  # write "ASHA" on OLED line 1 (large bold, centred)
    oled_write(3, center, small, regular, "Class 7 B")  # write "Class 7 B" on OLED line 3 (small, centred)
    oled_write(4, center, small, regular, "TinkerPy coder")  # write "TinkerPy coder" on OLED line 4 (small, centred)

# This code block executes forever
loop:
    if hello_btn == ON:  # when hello_btn is ON
        oled_emoji_animation("smile", 2, 5, 0)  # a smiling emoji fills the screen
        wait(3000)  # pause here for 3000 milliseconds before continuing
        oled_clear()  # clear everything off the OLED
        oled_write(1, center, large, bold, "ASHA")  # write "ASHA" on OLED line 1 (large bold, centred)
        oled_write(3, center, small, regular, "Class 7 B")  # write "Class 7 B" on OLED line 3 (small, centred)
        oled_write(4, center, small, regular, "TinkerPy coder")  # write "TinkerPy coder" on OLED line 4 (small, centred)

A name badge: the name in large bold letters, then two small lines. Press the button for a smiling emoji, then the badge comes back. Change ASHA to your own name (7 letters fit in large).

# Add the libraries you need
import oled_display
import dht11

# Give your pins easy-to-remember names
i1 = set_name("weather")

# ===== Live temperature display =====

# This code block executes only once
setup:
    oled_begin()  # starts the OLED
    o7 = set_function(SDA)  # cannot be changed
    o8 = set_function(SCL)  # cannot be changed
    weather = set_input(dht11)
    oled_write(1, left, small, bold, "Room temperature")  # write "Room temperature" on OLED line 1 (small bold, from the left)
    warned = 0  # set warned to 0

# This code block executes forever
loop:
    t = dht_temperature(weather)  # read the temperature in ยฐC
    oled_write(2, center, large, bold, t + " C")  # write the value of t + " C" on OLED line 2 (large bold, centred)
    if t >= 35 and warned == 0:  # when t >= 35 and warned == 0
        oled_scroll(4, "Too hot! Drink water")  # keep scrolling "Too hot! Drink water" along OLED line 4
        warned = 1  # set warned to 1
    if t < 35 and warned == 1:  # when t < 35 and warned == 1
        oled_clear_line(4)  # also stops the scrolling
        warned = 0  # set warned to 0
    wait(2000)  # pause here for 2000 milliseconds before continuing

A live thermometer with a DHT11 on i1. The temperature and C are joined into one large text. At 35 ยฐC a warning scrolls along line 4 once; the flag warned stops it from restarting every loop.

# Add the libraries you need
import dht11
import lcd_16x2

# Give your pins easy-to-remember names
i1 = set_name("weather")
i2 = set_name("light_btn")

# ===== Room meter on the LCD =====

# This code block executes only once
setup:
    lcd_begin(0x27)  # the LCDโ€™s I2C address (most: 0x27 or 0x3F; lcd_begin() finds it)
    weather = set_input(dht11)
    light_btn = set_input(push_button, active_low)
    o7 = set_function(SDA)  # cannot be changed
    o8 = set_function(SCL)  # cannot be changed
    lcd_scroll(1, "Welcome to the room meter")  # keep scrolling "Welcome to the room meter" along LCD line 1
    wait(6000)  # pause here for 6000 milliseconds before continuing
    lcd_clear()  # clear everything off the LCD

# This code block executes forever
loop:
    t = dht_temperature(weather)  # read the temperature in ยฐC
    h = dht_humidity(weather)  # read the humidity in %
    lcd_clear_line(1)  # clear line 1 of the LCD
    lcd_write(1, 1, "Temp:")  # write "Temp:" on LCD line 1, from place 1
    lcd_write(1, 7, t)  # write the value of t on LCD line 1, from place 7
    lcd_write(1, 10, "C")  # write "C" on LCD line 1, from place 10
    lcd_clear_line(2)  # clear line 2 of the LCD
    lcd_write(2, 1, "Humidity:")  # write "Humidity:" on LCD line 2, from place 1
    lcd_write(2, 11, h)  # write the value of h on LCD line 2, from place 11
    lcd_write(2, 14, "%")  # write "%" on LCD line 2, from place 14
    if light_btn == ON:  # when light_btn is ON
        lcd_backlight(ON)  # switch the LCD backlight ON
    else:  # otherwise
        lcd_backlight(OFF)  # switch the LCD backlight OFF
    wait(2000)  # pause here for 2000 milliseconds before continuing

A room meter on the 16x2 LCD: a welcome message scrolls first, then each line shows a label, a number and its unit. Hold the button to light the backlight.

# Add the libraries you need
import oled_display

# Give your pins easy-to-remember names
i1 = set_name("hand")

# ===== Robot pet eyes =====

# This code block executes only once
setup:
    oled_begin()  # starts the OLED
    o7 = set_function(SDA)  # cannot be changed
    o8 = set_function(SCL)  # cannot be changed
    hand = set_input(ir_sensor, active_low)
    mood = 0  # set mood to 0

# This code block executes forever
loop:
    if hand == ON and mood != 1:  # when hand is ON and mood != 1
        oled_eye_animation("happy", 0, 6, 500)  # happy eyes, for ever
        mood = 1  # set mood to 1
    if hand == OFF and mood != 2:  # when hand is OFF and mood != 2
        oled_eye_animation("sleepy", 0, 3, 2000)  # sleepy eyes, for ever
        mood = 2  # set mood to 2

Robot pet eyes: happy when a hand is near the IR sensor, sleepy when it goes away. The mood variable makes each animation start only once, so it can play smoothly.

Open TinkerPy Code

๐ŸŽ‰Fun examples

๐Ÿค–Give your robot a face

Put an OLED on the front of your robot car and play look_left before it turns left. It suddenly looks like it is thinking!

๐ŸŽ‚Birthday surprise

Scroll โ€œHappy birthday Ammaโ€ along the LCD and play the happy_birthday tune on a passive buzzer.

๐ŸŽฎScoreboard

Use the large size for the score and small for the player names โ€” a mini stadium screen on your desk.

๐ŸŒWhere itโ€™s used

๐Ÿ› ๏ธProject ideas

Digital name badge

Show your name, class and a fun line on the OLED. Add a second button that plays a different emoji, and a third screen with your hobby that appears every 5 seconds.

๐Ÿงฐ You need: TinkerPy board, OLED display, 2 push buttons, power bank
Live temperature display

Read a DHT11 and show temperature and humidity. Make the screen scroll a warning when it is too hot and show a happy emoji when the room is comfortable.

๐Ÿงฐ You need: TinkerPy board, DHT11, OLED or 16x2 LCD

โญPoints to remember

  1. The OLED is on in every new project: import oled_display and oled_begin().
  2. oled_write(line, align, size, weight, value) replaces one whole line.
  3. OLED: 4 lines; small = 21 letters, medium = 10, large = 7.
  4. Medium and large take 2 lines โ€” start them on line 1, 2 or 3.
  5. Join text and numbers with +: t + " C".
  6. oled_clear() wipes all; oled_clear_line(n) wipes one line.
  7. The LCD has 2 lines of 16 letters: lcd_write(line, position, value).
  8. lcd_begin(0x27) or lcd_begin(0x3F) โ€” lcd_begin() finds it.
  9. Old LCD letters stay: clear the line or add spaces.
  10. lcd_backlight(OFF) darkens the LCD; the text stays.
  11. oled_scroll / lcd_scroll keep moving text along a line โ€” call them once.
  12. Eye and emoji animations play by themselves; oled_write stops them.
  13. Both screens use I2C: o7 (SDA) and o8 (SCL).
๐Ÿคฏ
Did you know?

In an OLED every tiny pixel makes its own light. A black pixel is simply switched off, which is why OLED screens show such deep black.

โ“Mini quiz

๐Ÿ”‘
Log in to try the mini quiz

5 quick questions to check yourself โ€” free with your TinkerPy account.

Log in