MATLAB: How to use MATLAB support package for Raspberry Pi to work with SunFounder LCD device

i2clcdlcd1602MATLABpisunfounder

I am doing a project utilizing a Raspberry Pi with MATLAB, and I have installed the MATLAB Support Package for Raspberry Pi. We have a SunFounder LCD screen that uses an I2C connection, but we cannot get it to display the text that we want. We have used the 'read' and 'write' functions, as well as the 'readRegister' and 'writeRegister' functions, but they do not function as expected. For example, writing '0x42' to the device shuts off the LCD screen instead of displaying a character. Our MATLAB code is below:
>> mypi = raspi;
>> scanI2CBus(mypi);
>> lcd = i2cdev(mypi, 'i2c-1', '0x27');
>> write(lcd, hex2dec('42'));

Best Answer

SunFounder's example C code for using the SunFounder LCD device over I2C executes series of initialization commands before any data is sent. This initialization should be performed in your MATLAB code as well before the device will display any data.
The SunFounder example code also includes functions for encoding data before it is sent to the LCD device using their 'wiringPiI2CWrite' function, which appears to be analogous to the MATLAB 'write' function.
It is necessary to perform this encoding on any commands or data being sent to the LCD device, so some supporting MATLAB functions should be written to handle encoding before using the 'write' function.