Search code examples
c#raspberry-pii2clcd

Connecting Waveshare LCD1602 RGB to Raspberry using C#


I am trying to connect LCD1602 RBG Waveshare to the Raspberry using C#. I connected it to the Raspberry and set the permissions, now trying to pass some data. The code below run all lines, but the LCD is not reacting. If anyone can advise me.

using System;
using System.Device.Gpio;
using System.Threading.Tasks;
using System.Device.I2c;
using Iot.Device.CharacterLcd;
using Iot.Device.Pcx857x;
using System.Threading;

namespace pi_project
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Test1");

            using I2cDevice i2c = I2cDevice.Create(new I2cConnectionSettings(1, 0x3e));
            //BCM2835
            using var driver = new Pcf8574(i2c);
            using var lcd = new Lcd1602(registerSelectPin: 0,
                                    enablePin: 2,
                                    dataPins: new int[] { 4, 5, 6, 7 },
                                    backlightPin: 3,
                                    backlightBrightness: 0.3f,
                                    readWritePin: 1,
                                    controller: new GpioController(PinNumberingScheme.Logical, driver));
            int counter = 0;

            while (counter <= 4)
            {
                lcd.Clear();

                lcd.SetCursorPosition(0, 0);
                lcd.Write("TestText1");

                lcd.SetCursorPosition(0, 1);
                lcd.Write("TestText2");

                Thread.Sleep(2000);
                counter++;
            }
        }
    }
}
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- 3e --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --

enter image description here


Solution

  • The controller for these displays is normally a HD44780 or compatible. If the python sample works fine, then this should do:

    var i2cLcdDevice = I2cDevice.Create(new I2cConnectionSettings(busId: 1, deviceAddress: 0x3E));
    var i2cRgbDevice = I2cDevice.Create(new I2cConnectionSettings(busId: 1, deviceAddress: 0x60));
    using LcdRgb lcd = new LcdRgb(new Size(16, 2), i2cLcdDevice, i2cRgbDevice);
    {
        lcd.Write("Hello World!");
        lcd.SetBacklightColor(Color.Azure);
    }
    

    (for reasons beyond me, the python sample defines the addresses as 0x7c >> 1 and 0xc0 >> 1, which are 0x3E and 0x60, which aligns with your device scan)