温度と湿度を読み取る
ライブラリを呼び出して実際の環境データを取得します。
「温度と湿度を読み取る」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Time to Read
Your sensor is wired and the library is ready. Now you will ask it for real numbers and print them to the Serial Monitor. 🌡️
Read Temperature
Call readTemperature() to get the current value in Celsius. Store it in a float because the reading often has decimals.
float t = dht.readTemperature();Read Humidity
Call readHumidity() to get the moisture in the air as a percentage. Like temperature, save it in a float variable.
float h = dht.readHumidity();Want Fahrenheit?
Pass true to readTemperature to get Fahrenheit instead of Celsius. One small argument switches the whole unit.
float f = dht.readTemperature(true);Print the Values
Send your readings to the Serial Monitor so you can watch them live. Labels next to numbers make the output easy to read.
Serial.print("Temp: ");
Serial.println(t);Show Both at Once
You can print temperature and humidity together on one line. A short label before each value keeps the data clear.
Serial.print(h);
Serial.println("% humidity");DHT Is Slow
A DHT updates only about once per second. Reading faster than that just returns stale or failed values, so be patient.
Pace Your Loop
Add a short delay() at the end of loop so you read at a sensible pace. Two seconds is a safe, comfortable gap.
delay(2000);Compute Heat Index
The library can also estimate the heat index, how hot it feels, from temperature and humidity together. It is one handy call.
float hi = dht.computeHeatIndex(t, h, false);Use the Readings
Once you have the numbers, you can act on them: turn on a fan above a threshold or log values for later analysis.
Read Once, Reuse
Save each reading into a variable once per loop, then reuse it. Calling the sensor many times in a row only slows you down.
Quick Check
You want the temperature in Fahrenheit. Which call returns it?
Recap
You read temperature and humidity into floats, printed them with labels, paced the loop, and even computed how hot it feels. 🎉
よくある質問
「温度と湿度を読み取る」レッスンは無料ですか?
はい。「温度と湿度を読み取る」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。
「温度と湿度を読み取る」で何を学びますか?
ライブラリを呼び出して実際の環境データを取得します。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Arduino & IoT Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「温度と湿度を読み取る」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このArduino & IoT Academyレッスンでコードを書いて実行できますか?
はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。