Skip to content

Chapter 10: Interfacing with Sensors

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

Temperature sensors detect heat to convert the data into an interpretable format for your Arduino.

1. LM35:

  • Features: Analog output proportional to the temperature.
  • Connection:
    Vout (sensor) → Analog Pin (Arduino)
    Vcc (sensor) → 5V (Arduino)
    GND (sensor) → GND (Arduino)
  • Code Snippet:
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

Motion sensors detect movement in their vicinity.

1. PIR (Passive Infrared):

  • Features: Detects motion based on infrared light changes.
  • Connection:
    OUT (sensor) → Digital Pin (Arduino)
    Vcc (sensor) → 5V (Arduino)
    GND (sensor) → GND (Arduino)
  • Code Snippet:
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):

  • Features: Measures distance by sending ultrasonic waves and calculating the time they take to bounce back.
  • Connection:
    Trig (sensor) → Digital Pin (Arduino)
    Echo (sensor) → Digital Pin (Arduino)
    Vcc (sensor) → 5V (Arduino)
    GND (sensor) → GND (Arduino)
  • Code Snippet:
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);
}

Gas and Air Quality Sensors

These sensors detect gas concentrations and air quality.

1. MQ-2 (Gas Sensor):

  • Features: Detects flammable gases and smoke.
  • Connection:
    AOUT (sensor) → Analog Pin (Arduino)
    DOUT (sensor) → Digital Pin (Arduino, optional for threshold detection)
    Vcc (sensor) → 5V (Arduino)
    GND (sensor) → GND (Arduino)
  • Code Snippet:
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.

Previous article Chapter 11: Actuators and Motors
Next article Chapter 9: Advanced Arduino Programming

Leave a comment

Comments must be approved before appearing

* Required fields