Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Welcome

In this project, we will use a PIR sensor to detect human presence and a speaker to simulate a doorbell, similar to the entrance doorbells in convenience stores. When a pedestrian appears within the range of the PIR sensor, the speaker will ring, mimicking a doorbell.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Elite Explorer Kit

300+

Elite Explorer Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

Arduino Uno R4 WiFi

-

Breadboard

BUY

Jumper Wires

BUY

Resistor

BUY

PIR Motion Sensor Module

BUY

Audio Module and Speaker

-

Wiring

../_images/01_welcome_bb.png

Schematic

../_images/01_welcome_schematic.png

Code

Note

  • You can open the file 01_welcome.ino under the path of elite-explorer-kit-main\fun_project\01_welcome directly.

  • Or copy this code into Arduino IDE.

How it works?

Here is a step-by-step explanation of the code:

  1. Include Header Files:

    Include two header files, analogWave.h and pitches.h. The analogWave.h file contains the definition of the analogWave class, while pitches.h contains the definitions of musical notes.

  2. Instantiate Objects and Define Constants:

    Create a wave object using the analogWave class and define PIR_PIN as 2, which is the pin connected to the PIR sensor.

  3. Melody Array:

    The melody array defines a musical melody, with each note followed by a number representing its duration. Negative numbers represent dotted notes (increasing the duration by 50%).

  4. Global Variables:

    Define some global variables for sharing data between functions.

  5. setup():

    Initialize PIR_PIN as an input and set the frequency of the sine wave to 10 Hz using wave.sine(10).

  6. loop():

    Continuously monitor the value of the PIR sensor. If human presence is detected (pirValue is HIGH), call the playMelody() function to play the melody and wait for 10 seconds to prevent repetitive playback of the melody.

  7. playMelody():

    This function calculates the duration of each note based on the data in the melody array and plays the corresponding note. There is a brief pause between notes. The function sets the frequency of the waveform using wave.freq() and controls the duration of the notes and pauses between notes using the delay() function.

    Note: Ensure that the pitches.h header file indeed exists before running this code.