Skip to main content

Mission9_LCD

If you want to show the messages or display data, the LCD is a great choice. You can find various LCDs. In this mission, you will look at a 16x2 character LCD and display the phrase "Hello World" on it.

What you need​

The parts you will need are all included in the Maker kit.

  • SwiftIO board
  • Shield
  • 16x2 LCD
  • 4-pin cable

Circuit​

  1. Place the shield on top of the SwiftIO board.

  2. Connect the LCD to the pins I2C0. You can see there are three available I2C0. You could choose any of them.

Circuit diagram

Example code​

// Import the SwiftIO library to use everything in it.
import SwiftIO

// Import the board library to use the Id of the specific board.
import MadBoard

// Initialize the I2C0 and the lcd.
let i2c = I2C(Id.I2C0)
let lcd = LCD1602(i2c)

// Set the display area and print the message on the LCD.
lcd.write(x: 0, y: 0, "Hello World!")

while true {

}

Background​

16x2 LCD​

This kind of LCD has 16 columns and 2 rows. So it can display 32 characters. Each character consists of 5 columns and 8 rows, that is, 40 pixels.

LCD

The upper left corner is the origin (0,0). Its coordinate system is as above.

This LCD supports I2C communication. All the commands to control it are in its datasheet. But you don't need to worry about it. The file LCD1602.swift in your project has done the work for you. You can directly use the methods to control your LCD.

I2C​

Inter-integrated circuit, I2C (I two C or I squared C) for short, is a protocol suitable for short-distance communication. It allows multiple slave devices to communicate with one master device using merely two wires, which constitutes its advantage compared to other protocols.

For the communication protocols, there are always master and slave devices. Master device controls the communication process. Slave devices would respond to the master device when it’s called. In our projects, the SwiftIO board is always the master, and the other devices serve as slaves.

The two wires contain an SCL and an SDA line:

I2C
  • SCL (serial clock) carries the clock signal generated by the master. With a preset speed, the devices know the time to transmit each bit and can predict if the data transmission is completed. Thus, it can ensure synchronous data transmission between devices.

  • SDA (serial data) is the data line. All the data is transferred on this line by master or slave devices. And the data is usually in bytes. Each communication protocol specifies its own system about how the data is sent. That's a little complicated, and you don't have to understand it when using it.

Why can only two wires support multiple devices without confusion? That is because each I2C device has its unique address. You can always find it in its datasheet. At the beginning of the communication, the master will send the address of the device that is wanted. Only the corresponding device would respond and prepare for the following process. Thus there won’t be confusion among different devices.

In brief, if you want to communicate with some devices, you need to find the corresponding data of the command in the datasheet, then send the data according to the rules of communication protocol.

Code analysis​

The file LCD1602.swift has configured the LCD according to its datasheet. It is included in the project, so you can directly use these methods to control your LCD.

Let's look at the file main.swift.

import SwiftIO
import MadBoard

Import the two libraries: SwiftIO and SwiftIOBoard. SwiftIO is used to control the input and output of the SwiftIO board. SwiftIOBoard defines the pin name of the board.

let i2c = I2C(Id.I2C0)
let lcd = LCD1602(i2c)

Initialize the I2C interface. To initialize the LCD, you need to tell the I2C interface, and the other parameters already have a default value.

lcd.write(x: 0, y: 0, "Hello World!") 

Then you will use the method write(x:y:_:) to display the string. It has three parameters. At first, you need to set the coordinate of the start point. Here you start from the origin (0,0). The third parameter is the content you would like to display. It is a string. So you add the content within the double quotation marks.

Since the text would always display on the LCD, you can write it above the loop. Even there is nothing in the loop, you'd better add it to your code.

Reference​

I2C - use I2C protocol to communicate with other devices.

SwiftIOBoard - find the corresponding pin id of SwiftIO board.