Since the SHT4x Trinkey is designed to be most at home in a USB port, it makes it a great fit for desktop CPython scripts. In this example, the Trinkey runs code that reads temperature and humidity data from the SHT4x sensor. The CPython script logs that data coming in on the USB port and saves it to a .CSV file.
CPython SHT4x Trinkey Logger Code
To run the script you will need a desktop or laptop computer with Python 3 installed.
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT # Written by Liz Clark (Adafruit Industries) with OpenAI ChatGPT v4 Feb 13, 2024 build # https://help.openai.com/en/articles/6825453-chatgpt-release-notes # https://chat.openai.com/share/430869c1-e28f-4203-9750-c6bbabe18be6 import os import time import csv import serial # Configuration com_port = 'COM121' # Adjust this to your COM port baud_rate = 115200 # Adjust this to the baud rate of your sensor base_csv_file_path = 'sensor_readings' def find_next_file_name(base_path): """Finds the next available file name with an incremented suffix.""" counter = 0 while True: new_path = f"{base_path}_{counter}.csv" if counter else f"{base_path}.csv" if not os.path.isfile(new_path): return new_path counter += 1 def read_sensor_data(ser): line = ser.readline().decode('utf-8').strip() temperature, humidity = line.split(',') return float(temperature), float(humidity) def save_to_csv(file_path, temperature, humidity): with open(file_path, mode='a', newline='') as file: writer = csv.writer(file) # Convert struct_time to a string for the CSV timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) writer.writerow([timestamp, temperature, humidity]) def main(): csv_file_path = find_next_file_name(base_csv_file_path) # Write headers to the new file with open(csv_file_path, mode='w', newline='') as file: writer = csv.writer(file) writer.writerow(["Timestamp", "Temperature", "Humidity"]) with serial.Serial(com_port, baud_rate, timeout=1) as ser: print(f"Starting data logging to {csv_file_path}... Press Ctrl+C to stop.") try: while True: temperature, humidity = read_sensor_data(ser) save_to_csv(csv_file_path, temperature, humidity) print(f"Logged: Temperature={temperature}°C, Humidity={humidity}%") time.sleep(1) except KeyboardInterrupt: print("Data logging stopped.") if __name__ == '__main__': main()
SHT4x Trinkey Code
The code running on the Trinkey is the basic temperature and humidity Arduino demo. It has been compiled into a convenient .UF2 file that you can drag and drop onto the Trinkey.
CPython Dependencies
First, you'll use pip
to install the Python library required to run the script:
pip install pyserial
Customize the Script
Open the sht4x_trinkey_logger.py script in your preferred text editor or IDE. At the top of the code, you'll need to update com_port
to match the COM port that your Trinkey is plugged into. Otherwise, the script will not work.
You can also customize the name of the .CSV file that is generated by editing base_csv_file_path
.
# Configuration com_port = 'COM123' # Adjust this to your COM port baud_rate = 115200 # Adjust this to the baud rate of your sensor base_csv_file_path = 'sensor_readings'
Update the COM port in the script to match your SHT4x Trinkey's COM port!
Run the Script
After adding your COM port, you can run the sht4x_trinkey_logger.py script inside a terminal window on your computer with:
python sht4x_trinkey_logger.py
As the script runs, you'll see the temperature and humidity readings from the SHT4x Trinkey print to your terminal window. You can press Ctrl+C on your keyboard to stop the script.
Page last edited January 22, 2025
Text editor powered by tinymce.