'Using python smbus2 library to control lcd 1602 via I2C

I've purchased a generic 1602 lcd display and I2C adapter from ebay and am interested to control it over I2C.

I've used one of the many python librarys to confirm that the display is working.

Now I'd like to look a bit under the hood and learn how to drive the display by sending bits & bytes directly rather than passing a string to a function without understanding how it works.

I've chosen the smbus2 library to send data to the display.

The datasheet shows how to drive the LCD directly via its 16 pins but I am not sure how this translates to I2C.

The datasheet gives instructions on how to clear the display: enter image description here

The smbus2 documentation provides several methods to send data to an I2C device.

This is where I am getting stuck. To control the lcd display I have to control 10 inputs.

-> RS to specify whether I am sending data or a command;

-> R/W to specify whether I am reading or writing (in case of the LCD display I will only be writing);

-> DB7-DB0 to pass a command/data.

I am getting stuck on how to put the 10 inputs into byte format and what part is data and what is the register I need to write to.

I've tried using the write_word_data(i2c_addr, register, value, force=None) method:

from smbus2 import SMBus

address = 0x27

with SMBus(1) as bus:
    bus.write_word_data(address, 0, 1)

My reasoning was the following:

I want to pass a command to the display so RS and R/W should both be 0. Register expects an int so 00 turns into 0. Value expects an int aswell so 00000001 from binary to decimal turns into 1.

Unfortunately that did not work and the display still shows me the characters from when I used another library to write to it.

To summarise: What data makes up the register and value?

So far I am yet to find a python tutorial on I2C that does not simply import a module that does everything in the background so I'd be extremely happy if someone could point me to an appropriate resource.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source