Temperature and humidity measurement

  • I. Implementation Function
  • Two, wiring diagram
  • List of materials
  • 4. Complete code
  • Five, code running results

I. Implementation Function

The temperature and humidity are measured by shT30/31 sensor module and displayed on the OLED screen or printed out by print() function.

Two, wiring diagram

List of materials

Sht30/31 temperature and humidity sensor module, 0.96 inch 4-pin IIC interface OLED screen, Core development board

4. Complete code

Define the initialization functionfunction Init(a)Configure USB to work in virtual serial mode, so calledprint(a)The function will print out the output at the serial port terminal of the computerLIB_UsbConfig("CDC")-- Set SHT3X sensor to occupy SCL0 and SDA0 pins, and start sensor to work with the frequency of 10 data output per second, REPEATability="HIGH"LIB_Sht3xConfig("IIC0"."10"."HIGH") - set0.96Inch OLED module occupies SCL1 and SDA1 pins LIB_0_96_OledConfig("IIC1") end -- initializes Init() -- starts the big loopwhile(GC(1) = =true)
do- delay200Ms LIB_DelayMs (200) - every200Sht3x_flag, sht3X_temp, sht3X_humi = LIB_Sht3xGetResult() if new temperature and humidity data is generated for the sensorif sht3x_flag == 1Then -- Displays temperature (degrees Celsius) and humidity (percent) on the first and second lines of the OLED, respectively LIB_0_96_OledPuts("1"."1".string.format("temprature: %.2f", sht3x_temp))
        LIB_0_96_OledPuts("2"."1".string.format("humidity: %.2f", sht3x_humi)) -- print(sht3x_humi)string.format("temp: %.2f\r\nhumi: %.2f", sht3x_temp, sht3x_humi))
    end
end
Copy the code

Five, code running results

(1) What is displayed on the Oled screen:

If you are connected to the Oled screen, it will display something like this (it doesn’t matter if you are not connected to the Oled screen, the code will still work)



(2) Contents displayed in log. TXT LOG file on TF card:

If you have a TF card plugged into Core, you can switch to a TF file and see the following contents in the log.txt file of the TF card



(3) Contents displayed on PC serial port terminal debugging software:

If you connect Core to computer via USB cable and configure serial terminal software in n, 8,1,115200 mode.



Read more at shineblink.comThe website links