Skip to main content

Mission11_Reproduce_Mission10

This mission is similar to Mission10. You will display the temperature on the LCD. But you will use an external library to realize it.

What you need

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

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

Circuit

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

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

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

// 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 second.
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 previous mission, the code 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 to your projects.

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

In the previous code, the two files are included, so you can 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 to 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.

Reference

I2C - use I2C protocol to communicate with other devices.

LCD1602 - display some characters on a 16x2 LCD.

SHT3x - measure temperature and humidity and communicate using I2C protocol.

SwiftIOBoard - find the corresponding pin id of SwiftIO board.