Order for your organisation.
Learn how to place education orders with Little Bird.
Sensors transform physical phenomena into electronic data, enabling the Arduino to perceive its surroundings. They are the eyes, ears, and skin of your projects. In this chapter, we'll explore some commonly used sensors and guide you on integrating them into your Arduino setup.
Temperature sensors detect heat to convert the data into an interpretable format for your Arduino.
1. LM35:
int tempPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(tempPin);
float voltage = reading * 5.0 / 1024.0;
float temperature = voltage * 100; // Convert voltage to temperature
Serial.println(temperature);
delay(1000);
}
Motion sensors detect movement in their vicinity.
1. PIR (Passive Infrared):
int pirPin = 7;
int motionState = 0;
void setup() {
pinMode(pirPin, INPUT);
Serial.begin(9600);
}
void loop() {
motionState = digitalRead(pirPin);
if (motionState == HIGH) {
Serial.println("Motion detected!");
delay(1000);
}
}
2. Ultrasonic Sensor (e.g., HC-SR04):
int trigPin = 9;
int echoPin = 10;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = (duration * 0.0344) / 2;
Serial.println(distance);
delay(1000);
}
These sensors detect gas concentrations and air quality.
1. MQ-2 (Gas Sensor):
int gasPin = A1;
void setup() {
Serial.begin(9600);
}
void loop() {
int gasLevel = analogRead(gasPin);
Serial.println(gasLevel);
delay(1000);
}
Conclusion
Sensors breathe life into Arduino projects, enabling them to interact with the environment. From measuring temperature fluctuations to detecting movement or monitoring air quality, sensors provide the data backbone for myriad applications. Familiarity with various sensors and their interfacing methods is invaluable for any Arduino enthusiast, paving the way for richer, more interactive, and responsive projects.
Leave a comment