9. Input Devices

Group assigment

Link to a Group page of Input Devices

Individual assigment

Summary

This week of input device is explored different kind of input sensors mainly Ultrasonic sensor and PIR sensor. The goal was to understand the working principles of these sensor and able to use them to detect an abstacle or motion.

HC-SR04 Ultrasonic sensor

This is an electronic device that measures distance by using ultrasonic sound waves. It works very similarly to how bats and dolphins use echolocation; mainly to locate prey when hunting.

It has two main parts: Transmitter that emits a high-frequency sound wave (usually above 20 kHz, which is inaudible to humans) and Receiver that dects the sound wave when it bounces back from an object.

It calculates the distance to the object using the time it took for the echo to return:

Distance= Speed of sound x Time delay / 2

Note: The division by 2 accounts for the sound traveling to the object and back.

Ultrasonic sensor

Circuit connection

On Xiao RP2040,

  1. I connected TRIG to D2
  2. ECHO to D1
  3. LED is connected to D0

Arduino code

      
    // Defining pin connections
    #define TRIG D2
    #define ECHO D1
    #define BLUE_LED D0


    // Setting pins mode
    void setup() {
      Serial.begin(115200);
      pinMode(TRIG, OUTPUT);
      pinMode(ECHO, INPUT);
      pinMode(BLUE_LED, OUTPUT);
    }

    // Main function
    void loop() {
      digitalWrite(TRIG, HIGH);
      delayMicroseconds(10);
      digitalWrite(TRIG, LOW);

      int duration, distance;
      duration = pulseIn(ECHO, HIGH);
      distance = duration * 0.0343 / 2;
      Serial.print("distance is ");
      Serial.print(distance);
      Serial.println(" cm");

      if (distance < 10){
        digitalWrite(BLUE_LED, HIGH);
        delay(300);
        digitalWrite(BLUE_LED, LOW);
        delay(300);
        Serial.println("Too close");
      }
      else{
        digitalWrite(BLUE_LED, LOW);      
      }
      
      delay(800);
    }
          
      
Arduino code Ultrasonic sensor

Result

As shown in the result, as move I move an object far and closer to the sensor, the distance is displayed onto the serial output.

Ultrasonic sensor

HC-SR501 PIR module Sensor

This is a motion sensor divice that detects motion by measuring changes in infrared (IR) radiation from its environment. It's widely used in homemade(DIY) electronics, automation systems, and security applications.

PIR motion sensor

Arduino code HC-SR501

      
        // Define the pin connected to the PIR sensor OUT pin
    const int pirPin = D2;

    void setup() {
      // Start the serial communication
      Serial.begin(115200);
      
      // Setting the PIR pin as an input
      pinMode(pirPin, INPUT);
    }

    void loop() {
      // Reading the value from the PIR sensor (HIGH or LOW)
      int motionDetected = digitalRead(pirPin);
      
      if (motionDetected == HIGH) {
        // Motion detected
        
        Serial.println("Motion Detected!");
      
      } else {
        // if no motion
        Serial.println("No motion detected.");
      }
      
      // Add a small delay to avoid flooding the Serial Monitor
      delay(1000);
    }
    
  
PIR motion sensor PIR motion sensor

Prevously, the module was detecting every motion even the slighithest motion of curtain waving due to wind. This made quite difficult to actual motion of an abject.

PIR motion sensor

In the end, I found that I have to adjust sensitivity and timer accordingly by turning the sensitivity potentiometer to reduce the sensor's sensitivity. Meaning it will only trigger when larger movements are detected. This is the easiest way to avoid false positives.

PIR motion sensor

Result

Download files

Here are source code files for Ultrasonic and PIR Sensors

Ultrasonic Sensor

HC-SR501 PIR Motion Sensor