Micropython ESP32 Lesson 2.6 Side Scrolling the LCD1602

After finishing the LCD1602 lesson I was wanting to see if I could side scroll the message. Without loading up a different LCD1602 library, this is the solution I came up with:

After a few convoluted methods I decided to use deque() (doubled ended que) as this looks like the most efficient way to manipulate a string for this particular use. This required that I import it from the collections library:

from collections import deque

Doing so allows me to create a variable that is never more than 16 characters long. Appending to the end will automatically lop off the front and appending to front will lop off the end to not allow the variable to exceed 16 characters.

lcd_buffer = deque((), 16) #empty deque with a max size of 16

After that I could just loop through msg and clear the screen between each message.

for n in msg:
  lcd.clear()
  lcd_buffer.append(n)
  lcd.message(lcd_buffer)
  time.sleep(.1)

Then it got tricky, and honestly, I don’t even know think it’s proper at all. But, here’s the problem. When I ran this, it had a rapid blink when it cleared the screen and reprinted the new message. This looked sick, and not the good sick. So, I finagled.

Looking at the lcd.clear() command I noticed that 0x01 was getting sent to the send_command() in the lcd1602.py file. By trial and error I found out that if I sent 0x02 instead, it would return to the origin. Not sure if it is generating an internal error, but that’s the effect (so did 0x03 for that matter, but I stopped my search after that).

I wrote a new method called origin() in that file (added to the bottom of lcd1602.py) so I could call it by command:

def origin(self):
        self.send_command(0x02) # return to origin

After adding a call to that method, I could scroll a message without the annoying blink. Here’s the final code:

from lcd1602 import LCD
from collections import deque

import time

# Create an instance of the LCD class and assign it to the lcd variable
lcd = LCD()

lcd.clear()

lcd_buffer = deque((), 16) # empty deque with a max size of 16
    
msg = "This is an example of a side scrolling message." # The full message

for n in msg:
    lcd.origin() # return to 0,0
    lcd_buffer.append(n) # appends the next character but will not exceed 16 characters
    lcd.message(lcd_buffer) # send the message to the screen
    time.sleep(.1) # adjust to change the speed of the scroll

There are other things I could easily do to improve this such as loading the full line first and then starting the scroll, but this was good enough for now.

If you tried something different, had a goal of your own that you want to share or
know the method I should have used, please share below.