Display the Temperature on LCD Using Swift Language

by madmachineio in Circuits > Arduino

177 Views, 0 Favorites, 0 Comments

Display the Temperature on LCD Using Swift Language

Mission10_Humiture_Sensor.jpg

In the previous mission, you learn about the LCD. Now you're going to know more about it, the LCD will be used to show the temperature in your room.

What You Will Need

humitureparts.png

The parts you will use are all included in this Maker kit.

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

The Humiture Sensor

The humiture sensor could sense the temperature and humidity at the same time.

It uses the I2C protocol to communicate with the SwiftIO board. You could find a file SHT3x.swift in this mission that you could use to read the values.

The Circuit

humitureschematics.png

Place the shield on top of the SwiftIO board.

Connect the humiture sensor and the LCD to the I2C0. There are three available pins, you could choose any two.

The 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 SwiftIOBoard

// Initialize the LCD and sensor to use the I2C communication.
let i2c = I2C(Id.I2C0)
let lcd = LCD1602(i2c)
let sht = SHT3x(i2c)

while true{
    // Read and display the temperature on the LCD and update the value every 1s.
    let temp = sht.readCelsius()

    lcd.write(x:0, y:0, "Temperature:")
    lcd.write(x: 0, y: 1, temp)
    lcd.write(x:4, y:1, " ")
    lcd.write(x:5, y:1, "C")

    sleep(ms: 1000)
}

Code Analysis

In this project, you could find the file LCD1602.swift for the LCD and the file SHT3x.swift for the sensor. You could directly use them to simplify your code and don't need to configure them according to their datasheet.

So let's come to the file main.swift.

import SwiftIO
import SwiftIOBoard

Import the necessary libraries: SwiftIO and SwiftIOFeather. 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)
let sht = SHT3x(i2c)

Initialize the I2C interface I2C0.

Then initialize the LCD and the sensor. Both of them need the I2C interface as their parameter.

let temp = sht.readCelsius()

To get the temperature, you need the method readCelsius() in the file SHT3x.swift. It will calculate the temperature into Celsius.

lcd.write(x:0, y:0, "Temperature:")
lcd.write(x: 0, y: 1, temp)
lcd.write(x:4, y:1, " ")
lcd.write(x:5, y:1, "C")

As you get the value, you could display it on the LCD. The four statements are all about the content to be displayed:

  • The first row of the LCD will display the text "Temperature:". It starts from the origin.
  • The temperature will be displayed on the second row from the first column. They will take up four characters.
  • The fifth is blank to separate the value from the unit.
  • The sixth is the unit.
sleep(ms: 1000)

The sensor will read value every 1s, so the value on the LCD will be refreshed per second.

Run the Project

display.png

After you download the code, the LCD starts to display the temperature. The value will slightly change.

Using Library

The code above includes the two files to configure the LCD and humiture sensor. However, there is a more convenient way - using libraries and you don't need to add hardware drivers in your projects.

Simply put, a library contains a block of code that is for specific functionality. Then you could use it in any of your projects to realize those functionalities.

Let's look at the 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 SwiftIOBoard

// Import LCD1602 and SHT3x driver from MadDrivers which is an online git repo.
import LCD1602
import SHT3x

// Initialize the LCD and sensor to use the I2C communication.
let i2c = I2C(Id.I2C0)
let lcd = LCD1602(i2c)
let sht = SHT3x(i2c)

while true{
    // Read and display the temperature on the LCD and update the value every 1s.
    let temp = sht.readCelsius()

    lcd.write(x:0, y:0, "Temperature:")
    lcd.write(x: 0, y: 1, temp)
    lcd.write(x:4, y:1, " ")
    lcd.write(x:5, y:1, "C")

    sleep(ms: 1000)
}<br>

In the previous code, the two files are included, so you could directly use them.

Now, you will use the online libraries - LCD1602 and SHT3x in your code. They are in the MadDriver that contains all the related hardware libraries and its location has been indicated in the project. Thus you don't need to add these files to your project and just import them into your code. The IDE will download them automatically when building your project.

Then the rest of the code is the same as the previous one.