0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#でIoTプログラミング on ESP32 #10 - SHT40

Posted at

I2C接続のデジタル温湿度センサーのサンプルを紹介します。

ハードウェア

・Freenove ESP32-S3-WROOM Board or Espressif ESP32-S3-DevKitC
・Adafruit SHT40

配線

SHT40.PNG

導入パッケージ

・nanoFramework.Hardware.Esp32
・nanoFramework.System.Device.I2c

プログラミング

Program.cs
using nanoFramework.Hardware.Esp32;
using System;
using System.Device.I2c;
using System.Threading;

namespace ESP32S3_SHT40
{
    public class Program
    {
        public static void Main()
        {
            Configuration.SetPinFunction(8, DeviceFunction.I2C1_DATA);
            Configuration.SetPinFunction(9, DeviceFunction.I2C1_CLOCK);

            var device = I2cDevice.Create(new I2cConnectionSettings(
                1,
                0x44,
                I2cBusSpeed.FastMode));

            while (true)
            {
                device.WriteByte(0xfd);
                Thread.Sleep(10);

                byte[] data = new byte[6];

                device.Read(data);

                ushort rawTemp = (ushort)(data[0] << 8 | data[1]);
                double temp = -45 + 175 * (rawTemp / 65535.0);

                ushort rawHum = (ushort)(data[3] << 8 | data[4]);
                double hum = -6 + 125 * (rawHum / 65535.0);

                Console.WriteLine($"Temp: {temp:F2}℃, Hum: {hum:F2}%");

                Thread.Sleep(1000);
            }
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?