10. Output Devices

Group assignments

Summary

This week of output divice I explored diffferent output modules. These include servo motors, Passive Buzzer Module and Liquid Crystal Display. I focused on Passive Buzzer Module to sound an alarm when a object is detected. I also used LCD screen to display distace where an object is located. I used ultrasonic sensor to detect this object.

KY-006 Passive Buzzer Module

This is an electronic component used to generate sound. It requires a signal (usually a PWM or square wave) to produce sound. It is commonly used in alarms, sound effects in projects, notifications in embedded systems and more.

Buzzer

I paired this module with HC-SR501 PIR module Sensor. The aim was to produce sound (sound an alarm) when motion is detected.

Buzzer and microcontroller board

Arduino code

      
        // 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);
    }
    
  
buzzer script

Result

As shown in this video, when motion is detected the buzzer will send an alarm.

Liquid Crystal Display (LCD)

It is a flat-panel display divice commonly used in electronic projects to visually display text, numbers, and symbols. It works by manipulating liquid crystals with electric signals to block or allow light.

I used this screen to display distance at which an object is located detected by an ultrasonic sensor.

LDC Display

Code

I first installed Wire.h and LiquidCrystal_I2C.h libraries.

        
      #include 
      #include 
      LiquidCrystal_I2C lcd(0x27, 16, 2);

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

      // Setting pins mode
      void setup() {
        Serial.begin(9600);
        pinMode(TRIG, OUTPUT);
        pinMode(ECHO, INPUT);
        pinMode(BLUE_LED, OUTPUT);
        //Display setup
        lcd.init();
        lcd.backlight();
        lcd.clear();
        lcd.print("Ready...");
      }
      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");
        // Printing to screen
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dist= ");
        lcd.print(distance);
        lcd.print(" cm");

        // Calling blink function
        // blink_led(distance);
        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);
      }
        
      
LDC Display code

Result

As shown here, serial monitor as well LCD screen display the distance where an object is located. The LCD screen is used doen't not display many characters. Therefore I reduced the number of characters for only LCD display unlike serial monitor which is basically a pc screen.

Download files

Here are source code files for Ultrasonic and PIR Sensors

PIR sensor and buzzer

Ultrasonic and LCD display