Bird
0
0
Arduinoprogramming~10 mins

Using sensor libraries in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Using sensor libraries
Include sensor library
Create sensor object
Initialize sensor in setup()
Read sensor data in loop()
Use sensor data (print, control)
Repeat loop
This flow shows how to include a sensor library, create and initialize the sensor, then read and use its data repeatedly.
Execution Sample
Arduino
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float temp = dht.readTemperature();
  Serial.println(temp);
  delay(2000);
}
This code uses the DHT sensor library to read temperature from a DHT11 sensor connected to pin 2 and prints it every 2 seconds.
Execution Table
StepActionEvaluation/Function CallResult/Output
1Include DHT library#include <DHT.h>Library functions available
2Define sensor pin and type#define DHTPIN 2 #define DHTTYPE DHT11Constants set
3Create sensor objectDHT dht(DHTPIN, DHTTYPE);Sensor object ready
4Start Serial communicationSerial.begin(9600);Serial ready at 9600 baud
5Initialize sensordht.begin();Sensor initialized
6Read temperaturefloat temp = dht.readTemperature();temp gets sensor value or NAN if error
7Print temperatureSerial.println(temp);Temperature printed on Serial Monitor
8Wait 2 secondsdelay(2000);Pause before next reading
9Loop back to Step 6Repeat reading and printingContinuous temperature updates
10ExitNever (loop runs forever)Continuous operation
💡 Loop runs forever to keep reading sensor data continuously.
Variable Tracker
VariableStartAfter 1 loopAfter 2 loops...final
tempundefined23.5 (example)23.7 (example)varies with sensor reading
Key Moments - 3 Insights
Why do we need to call dht.begin() in setup()?
dht.begin() initializes the sensor hardware and prepares it for reading. Without it, readings may fail or return errors as shown in Step 5 of the execution_table.
What happens if the sensor reading fails?
If reading fails, dht.readTemperature() returns NAN (not a number). This is important to check before using the value, as seen in Step 6 where temp may be NAN.
Why is there a delay(2000) after printing?
delay(2000) pauses the program for 2 seconds to avoid flooding the Serial Monitor with data and to give the sensor time between readings, as shown in Step 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the purpose of Step 4?
ATo start communication with the sensor hardware
BTo start Serial communication for output
CTo define sensor pin number
DTo read temperature from the sensor
💡 Hint
Check Step 4 in execution_table where Serial.begin(9600) is called.
At which step does the sensor actually provide a temperature reading?
AStep 3
BStep 5
CStep 6
DStep 7
💡 Hint
Look at Step 6 where dht.readTemperature() is called.
If delay(2000) was removed, what would happen to the output?
ATemperature would print faster and flood the Serial Monitor
BSensor would stop working
CTemperature readings would be more accurate
DProgram would crash
💡 Hint
Refer to Step 8 delay(2000) purpose in execution_table.
Concept Snapshot
#include <SensorLibrary.h>
Create sensor object with pin and type
Initialize sensor in setup() with sensor.begin()
Read sensor data in loop() with sensor.read()
Use data (print/control)
Add delay to space readings
Full Transcript
This example shows how to use a sensor library in Arduino. First, include the sensor library to get access to its functions. Then define the sensor pin and type. Create a sensor object with these details. In the setup function, start Serial communication and initialize the sensor with begin(). In the loop function, read the sensor data using the library's read function, then print the value to the Serial Monitor. Add a delay to avoid flooding the output and to give the sensor time between readings. The loop repeats forever to keep updating sensor data.