laitimes

Use the Xifang Starlight 2 to read SHTC3 temperature and humidity data

author:Saifang Technology
  1. This application note provides steps to run a sample program to read SHTC3 data using the IIC bus utilizing Starlight 2 via Python.
  2. The following diagram shows the location of the 40-pin GPIO header:
Use the Xifang Starlight 2 to read SHTC3 temperature and humidity data

40-Pin GPIO header definition

  1. Get ready:
  • Operating environment requirements:
    • Linux kernel version: Linux 5.15
    • Operating system: Debian 12
    • Hardware version: Fang Starlight 2
    • SoC: Fang Shocking 7110
  • Before executing the demo program, be sure to prepare the following hardware:
type M/O* project exegesis
general M Fang Starlight 2 single board computer -
general M
  • Micro-SD card with a capacity of not less than 32 GB
  • Micro-SD card reader
  • Computer (Windows/Mac OS/Linux)
  • USB to serial converter (3.3 V I/O)
  • Cable
  • Power adapter (5 V/ 3 A)
  • USB Type-C cable
The above project was used to burn Debian OS to Micro-SD.
I2C demo M
  • Sense Hat (B)
  • DuPont Line
-

Note: *: M: Required. O: Optional

The following table and image describe how to connect Sense HAT to a 40-Pin GPIO header:

Sense HAT (B) 40-Pin GPIO Header
Pin Number Pin Name
3V3 1 3.3V voltage
GND 9 GND
SDA 3 GPIO58 (I2C SDA)
SCL 5 GPIO57 (I2C SCL)
Use the Xifang Starlight 2 to read SHTC3 temperature and humidity data

Connect Sense Hat(B) to the 40-Pin GPIO header

  • Confirm that you are following these steps:

a. Burn Debian OS to a Micro-SD card in accordance with the "Flashing OS to Micro-SD" section of the "Fang Starlight 2 Single Board Computer Quick Reference Manual" (https://doc.rvspace.org/VisionFive2/PDF/VisionFive2_QSG.pdf).

b. Log in to Debian and make sure Starlight 2 is online. For detailed instructions, refer to the "Using SSH via Ethernet" or "Using a USB-to-Serial Converter" section of the Starlight 2 Single Board Computer Quick Reference Manual.

c. To extend the partition on Debian, see the "Extended Partition" section of the Quick Reference Manual for Single Board Computers in the Starlight 2.

Execute the following command to install PIP:apt-get install python3-pip on the Debian system

Execute the pip command on Starlight 2 Debian to install the VisionFive.gpio package: sudo pip install VisionFive.gpio, or you can execute the following command: sudo pip3 install VisionFive.gpio

f. (Optional) If you copy the source code to a local directory under Starlight 2 Debian, execute the following command in the source directory: sudo apt-get install python3-dev sudo python setup.py install

Tip: Click the link below to download the source code: https://pypi.org/project/VisionFive.gpio/.

Alternatively, you can execute the following command: sudo python3 setup.py install

  1. Do the following to run the demo code on the Debian system of Starlight 2:
    1. Find the directory where the test code I2C_Sense_Hat.py is located:

Execute the following command to get the directory where VisionFive.gpio is located: pip show VisionFive.gpio

Example result: Location: /usr/local/lib64/python3.9/site-packages

Note: The actual output depends on how the app is installed.

b. Go to the directory /usr/local/lib64/python3.9/site-packages: cd /usr/local/lib64/python3.9/site-packages as shown in the previous output

c. Execute the following command to enter the sample-code directory: cd ./VisionFive/sample-code/

2. In the sample-code directory, execute the following command to run the demo code: sudo python I2C_Sense_Hat.py, or you can execute the following command: sudo python3 I2C_Sense_Hat.py

Result: Temperature and humidity data output on the terminal:

[riscv@fedora-starfive sample-code]$ sudo python3 led.py Enter delay(seconds): /dev/i2c-1 Temperature = 27.85°C , Humidity = 56.59 % Temperature = 27.83°C , Humidity = 56.60 % Temperature = 27.85°C , Humidity = 56.61 % Temperature = 27.86°C , Humidity = 56.60 % Temperature = 27.86°C , Humidity = 56.60 % Temperature = 27.80°C , Humidity = 56.60 % Temperature = 27.87°C , Humidity = 56.60 %

  1. The resource code in this demo is for reference only.

I2C_Sense_Hat.py:

#!/usr/bin/python
'''
Please make sure the sense HAT(B) is connected to the correct pins.
The following table describes how to connect the Sense HAT(B) to the 40-pin header.
Sense HAT (B)--------------------------------------------
__Sense HAT (B)___Pin Number_____Pin Name
    3V3             1            3.3 V Power
    GND             9              GND
    SDA             3            I2C SDA
    SCL             5            I2C SCL
--------------------------------------------
'''

import sys
import struct
import fcntl
import os
import math
import time
import VisionFive.i2c as I2C

SHTC3_I2C_ADDRESS = 0x70
I2C_SLAVE = 0x0703
I2C_DEVICE = "/dev/i2c-1"

##Commands
cmd_dict = {
"SHTC3_WakeUp":  		0x3517,
"SHTC3_Sleep":  			0xB098,
"SHTC3_NM_CE_ReadTH":  	0x7CA2,
"SHTC3_NM_CE_ReadRH":  	0x5C24,
"SHTC3_NM_CD_ReadTH":  	0x7866,
"SHTC3_NM_CD_ReadRH":  	0x58E0,
"SHTC3_LM_CE_ReadTH":  	0x6458,
"SHTC3_LM_CE_ReadRH":  	0x44DE,
"SHTC3_LM_CD_ReadTH":  	0x609C,
"SHTC3_LM_CD_ReadRH":  	0x401A,
"SHTC3_Software_RES":  	0x401A,
"SHTC3_ID":  			0xEFC8,
"CRC_POLYNOMIAL":       0x131,
}


def SHTC3_CheckCrc(data, len, checksum): 
    crc = 0xff
    for byteCtr in range(0, len): 
        crc ^= data[byteCtr]
        for bit in range(8, 0, -1): 
            if(crc & 0x80): 
                crc = (crc << 1) ^ cmd_dict["CRC_POLYNOMIAL"]
            else: 
                crc = crc << 1
    if (crc != checksum): 
        return 1
    else: 
        return 0

def SHTC3_WriteCommand(cmd): 
    buf0 =  (cmd >> 8)& 0xff
    buf1 = cmd & 0xff
    buf = [buf0, buf1]
    I2C.write(buf)

def SHTC3_WAKEUP(): 
    SHTC3_WriteCommand(cmd_dict["SHTC3_WakeUp"])
    time.sleep(0.03)

def SHTC3_SLEEP(): 
    SHTC3_WriteCommand(cmd_dict["SHTC3_Sleep"])

def SHTC_SOFT_RESET(): 
    SHTC3_WriteCommand(cmd_dict["SHTC3_Software_RES"])
    time.sleep(0.03)

def getdata(): 
    time.sleep(0.02)
    buf_list = I2C.read(3)
    checksum = buf_list[2]
    DATA = 0
    if (not SHTC3_CheckCrc(buf_list, 2, checksum)): 
        DATA = (buf_list[0] << 8 | buf_list[1])
    return DATA

def SHTC3_Read_DATA(): 
    SHTC3_WriteCommand(cmd_dict["SHTC3_NM_CD_ReadTH"])
    TH_DATA = getdata()
    SHTC3_WriteCommand(cmd_dict["SHTC3_NM_CD_ReadRH"])
    RH_DATA = getdata()
    TH_DATA = 175 * TH_DATA /65536.0 -45.0   #Calculate the temperature value.
    RH_DATA = 100 * RH_DATA / 65536.0        #Calculate the humidity value.
    DATA = [TH_DATA,RH_DATA]
    return DATA

def getTem(): 
    SHTC3_WriteCommand(cmd_dict["SHTC3_NM_CD_ReadTH"])
    TH_DATA = getdata()
    TH_DATA = 175 * TH_DATA /65536.0 -45.0   #Calculate the temperature value.
    return TH_DATA

def getHum(): 
    SHTC3_WriteCommand(cmd_dict["SHTC3_NM_CD_ReadRH"])
    RH_DATA = getdata()
    RH_DATA = 100 * RH_DATA / 65536.0        #Calculate the humidity value.
    return RH_DATA

def main(): 
    #Open the Sense HAT by I2C.
    ret = I2C.open(I2C_DEVICE, SHTC3_I2C_ADDRESS)
    if (ret < 0): 
        return 0


    SHTC_SOFT_RESET()
    i = 0
    while i < 7: 
        Temp = getTem()
        Hum = getHum()
        SHTC3_SLEEP()
        SHTC3_WAKEUP()
        print("Temperature = {:.2f}°C , Humidity = {:.2f} %\n".format(Temp, Hum))
        i = i + 1

    I2C.close()
    return 0

if __name__ == "__main__": 
    sys.exit(main()) 
           

Read on