1
1

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 #8 - フォトレジスタ

Last updated at Posted at 2025-02-24

今回はフォトレジスタのサンプルプログラムです。
フォトレジスタは明るさで抵抗値が変化する電子パーツです。暗くなると点灯するライトなどに使用されています。
CdSセルと呼ばれるタイプを使用します。

ハードウェア

・Freenove ESP32-S3-WROOM Board or Espressif ESP32-S3-DevKitC
・フォトレジスタ
・ブレッドボード
・10kΩ レジスタ
・ジャンパーワイヤ

配線

Photoresistor.PNG

導入パッケージ

・nanoFramework.System.Device.Gpio
・nanoFramework.System.Device.Adc

プログラミング

・新規プロジェクトを作成します。プロジェクト名は「ESP32S3_Photoresistor」としました。

・コードを入力する前にパッケージは導入してください。

・コードは次のようになります。

Program.cs
using System.Device.Adc;
using System.Device.Gpio;
using System.Diagnostics;
using System.Threading;

namespace ESP32S3_Photoresistor
{
    public class Program
    {
        private static GpioPin led;
        
        public static void Main()
        {
            AdcController adcController = new AdcController();
            // ADC1_0を使用する
            AdcChannel adcChannel = adcController.OpenChannel(0);

            GpioController gpioController = new GpioController();
            led = gpioController.OpenPin(2, PinMode.Output);
            led.Write(PinValue.Low);

            while (true)
            {
                int value = adcChannel.ReadValue();
                Debug.WriteLine("value = " + value.ToString());

                //CdSセルは暗くなると数値が大きくなります。暗くなるとオンボードLEDを点灯させます。
                if (value > 3000)
                {
                    led.Write(PinValue.High);
                }
                else
                {
                    led.Write(PinValue.Low);
                }
                    Thread.Sleep(100);
            }
        }
    }
}

プログラムを実行してみてください。CdSセルを手で隠すとオンボードLEDが点灯するのを確認できます。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?