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+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
|---|---|
- |
|
- |
Wiring
Schematic
Code
Note
You can open the file
01_welcome.inounder the path ofelite-explorer-kit-main\fun_project\01_welcomedirectly.Or copy this code into Arduino IDE.
The
pitches.hfile is also required — place it in the same directory.
/*****************
Public Constants
*****************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 49
1/*
2 The code is for an Arduino project that plays a melody when motion is detected by
3 a PIR (Passive Infrared) motion sensor.
4
5 Board: Arduino Uno R4
6 Component: PIR Motion Sensor Module, Audio Module, and Speaker
7*/
8
9#include "analogWave.h"
10#include "pitches.h"
11
12analogWave wave(DAC);
13
14#define PIR_PIN 8
15
16int melody[] = {
17 NOTE_C4, 4, NOTE_E4, 4, NOTE_G4, 4, NOTE_C5, 4, NOTE_G4, 4, NOTE_E4, 4, NOTE_C4,4
18};
19
20int noteCounter = 0;
21int bpm = 60;
22float beatDuration = 60.0 / bpm * 1000;
23int divider = 0, noteDuration = 0; // Variables to hold note duration
24
25void setup() {
26 Serial.begin(9600);
27 pinMode(PIR_PIN, INPUT); // Set PIR motion sensor pin as input
28 wave.sine(10);
29}
30
31void loop() {
32 int pirValue = digitalRead(PIR_PIN); // Read the PIR sensor
33 Serial.println(pirValue);
34
35 if (pirValue == HIGH) {
36 playMelody();
37 delay(5000); // wait for 5 seconds before checking again to avoid repetitive playing
38 }
39}
40
41void playMelody() {
42 // Calculate the duration of the current note
43 while (1) {
44 divider = melody[noteCounter + 1];
45 if (divider > 0) {
46 // For regular notes
47 noteDuration = beatDuration / divider;
48 } else if (divider < 0) {
49 // For dotted notes (duration increased by 50%)
50 noteDuration = beatDuration / abs(divider);
51 noteDuration *= 1.5; // Increase the duration by 50% for dotted notes
52 }
53
54 // Play the note
55 wave.freq(melody[noteCounter]);
56 delay(noteDuration * 0.85); // Play the note for 85% of its duration
57 wave.stop();
58
59 // Pause between notes
60 delay(noteDuration * 0.15); // Pause for 15% of the note duration
61
62 // Increment the note counter by 2 (because each note is followed by its duration)
63 noteCounter += 2;
64
65 // Reset the counter when reaching the end of the melody
66 int totalNotes = sizeof(melody) / sizeof(melody[0]);
67 noteCounter = noteCounter % totalNotes;
68
69 // Exit the loop after the melody finishes playing
70 if (noteCounter == 0) {
71 break;
72 }
73 }
74}
How it works?
Here is a step-by-step explanation of the code:
Include Header Files:
Include two header files,
analogWave.handpitches.h. TheanalogWave.hfile contains the definition of theanalogWaveclass, whilepitches.hcontains the definitions of musical notes.Instantiate Objects and Define Constants:
Create a
waveobject using theanalogWaveclass and definePIR_PINas 2, which is the pin connected to the PIR sensor.Melody Array:
The
melodyarray defines a musical melody, with each note followed by a number representing its duration. Negative numbers represent dotted notes (increasing the duration by 50%).Global Variables:
Define some global variables for sharing data between functions.
setup():Initialize
PIR_PINas an input and set the frequency of the sine wave to 10 Hz usingwave.sine(10).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.playMelody():This function calculates the duration of each note based on the data in the
melodyarray and plays the corresponding note. There is a brief pause between notes. The function sets the frequency of the waveform usingwave.freq()and controls the duration of the notes and pauses between notes using thedelay()function.Note: Ensure that the
pitches.hheader file indeed exists before running this code.