Training
Learning path
Create and run simple C# console applications (Get started with C#, Part 2) - Training
Use Visual Studio Code to develop C# console applications that implement arrays, foreach loops, and if statements.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
LCD character displays are useful for displaying information without the need for an external monitor. Common LCD character displays can be connected directly to the GPIO pins, but such an approach requires the use of up to 10 GPIO pins. For scenarios that require connecting to a combination of devices, devoting so much of the GPIO header to a character display is often impractical.
Many manufacturers sell 20x4 LCD character displays with an integrated GPIO expander. The character display connects directly to the GPIO expander, which then connects to the Raspberry Pi via the Inter-Integrated Circuit (I2C) serial protocol.
In this topic, you will use .NET to display text on an LCD character display using an I2C GPIO expander.
Note
This tutorial is written assuming the target device is Raspberry Pi. However, this tutorial can be used for any Linux-based SBC that supports .NET, such as Orange Pi, ODROID, and more.
Note
There are many manufacturers of LCD character displays. Most designs are identical, and the manufacturer shouldn't make any difference to the functionality. For reference, this tutorial was developed with the SunFounder LCD2004.
Ensure your SBC is configured to support the following services:
For many devices, no additional configuration is required. For Raspberry Pi, use the raspi-config
command. For more information on raspi-config
, refer to the Raspberry Pi documentation.
Use jumper wires to connect the four pins on the I2C GPIO expander to the Raspberry Pi as follows:
Refer to the following figures as needed:
I2C interface (back of display) | Raspberry Pi GPIO |
---|---|
Image courtesy Raspberry Pi Foundation. |
Tip
A GPIO breakout board in conjunction with a breadboard is recommended to streamline connections to the GPIO header.
Complete the following steps in your preferred development environment:
Create a new .NET Console App using either the .NET CLI or Visual Studio. Name it LcdTutorial.
dotnet new console -o LcdTutorial
cd LcdTutorial
Add the Iot.Device.Bindings package to the project. Use either .NET CLI from the project directory or Visual Studio.
dotnet add package Iot.Device.Bindings --version 3.2.0-*
Replace the contents of Program.cs with the following code:
using System;
using System.Device.Gpio;
using System.Device.I2c;
using System.Threading;
using Iot.Device.CharacterLcd;
using Iot.Device.Pcx857x;
Console.WriteLine("Displaying current time. Press Ctrl+C to end.");
using I2cDevice i2c = I2cDevice.Create(new I2cConnectionSettings(1, 0x27));
using var driver = new Pcf8574(i2c);
using var lcd = new Lcd2004(registerSelectPin: 0,
enablePin: 2,
dataPins: new int[] { 4, 5, 6, 7 },
backlightPin: 3,
backlightBrightness: 0.1f,
readWritePin: 1,
controller: new GpioController(PinNumberingScheme.Logical, driver));
int currentLine = 0;
while (true)
{
lcd.Clear();
lcd.SetCursorPosition(0,currentLine);
lcd.Write(DateTime.Now.ToShortTimeString());
currentLine = (currentLine == 3) ? 0 : currentLine + 1;
Thread.Sleep(1000);
}
In the preceding code:
A using declaration creates an instance of I2cDevice
by calling I2cDevice.Create
and passing in a new I2cConnectionSettings
with the busId
and deviceAddress
parameters. This I2cDevice
represents the I2C bus. The using
declaration ensures the object is disposed and hardware resources are released properly.
Warning
The device address for the GPIO expander depends on the chip used by the manufacturer. GPIO expanders equipped with a PCF8574 use the address 0x27
, while those using PCF8574A chips use 0x3F
. Consult your LCD's documentation.
Another using
declaration creates an instance of Pcf8574
and passes the I2cDevice
into the constructor. This instance represents the GPIO expander.
Another using
declaration creates an instance of Lcd2004
to represent the display. Several parameters are passed to the constructor describing the settings to use to communicate with the GPIO expander. The GPIO expander is passed as the controller
parameter.
A while
loop runs indefinitely. Each iteration:
Build the app. If using the .NET CLI, run dotnet build
. To build in Visual Studio, press Ctrl+Shift+B.
Deploy the app to the SBC as a self-contained app. For instructions, see Deploy .NET apps to Raspberry Pi. Make sure to give the executable execute permission using chmod +x
.
Run the app on the Raspberry Pi by switching to the deployment directory and running the executable.
./LcdTutorial
Observe the LCD character display as the current time displays on each line.
Tip
If the display is lit but you don't see any text, try adjusting the contrast dial on the back of the display.
Terminate the program by pressing Ctrl+C.
Congratulations! You've displayed text on an LCD using a I2C and a GPIO expander!
The source for this tutorial is available on GitHub.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Training
Learning path
Create and run simple C# console applications (Get started with C#, Part 2) - Training
Use Visual Studio Code to develop C# console applications that implement arrays, foreach loops, and if statements.