10. Project 1. Piezo Sounder Alarm Why use? By connecting a piezo sounder to a digital output pin, you can create a wailing alarm sound. What is required? Piezo Sounder (or piezo disc) 2-Way Screw Terminal Piezo Sounder 2-Way Screw Terminal
11. How to connect? Now you know what has to be included in your code by watching at pictorial representation above. But can you really code ??? Let’s try So you understood what is it that’s remaining…
12. // Project 1 - Piezo Sounder Alarm float sinVal; inttoneVal; void setup() { pinMode(8, OUTPUT); } void loop() { for (int x=0; x<180; x++) { // convert degrees to radians then obtain sin value sinVal = (sin(x*(3.1412/180))); // generate a frequency from the sin value toneVal= 2000+(int(sinVal*1000)); tone(8, toneVal); delay(2); } }
13. The sinVal float variable holds the sin value that causes the tone to rise and fall The toneValvariable takes the value in sinVal and converts it to the frequency you require. You convert the value of x into radians : sinVal = (sin(x*(3.1412/180))); Then that value is converted into a frequency suitable for the alarm sound: toneVal = 2000+(int(sinVal*1000)); You take 2000 and add the sinVal multiplied by 1000. This supplies a good range of frequencies for the rising and falling tone of the sine wave. To make shrill sound and disturb someone who enters your room, I recommend you keep base value 1000 rather than 2000… :p
14. Project 2: Piezo Knock Sensor Why use? A piezo disc works when an electric current is passed over the ceramic material in the disc, causing it to change shape and hence make a sound (a click). The disc also works in reverse: when the disc is knocked or squeezed, the force on the material causes the generation of an electric current. You can read that current using the Arduino and you are going to do that now by making a Knock Sensor. What is required? Piezo Sounder (or piezo disc) 2-Way Screw Terminal 5mm LED (any color) 1MΩ Resistor Piezo Sounder 2-Way Screw Terminal
16. Project 3: Light Sensor Why use? The more light, the lower the resistance. By reading the value from the sensor, you can detect if it is light, dark, or anywhere between. In this project, you use an LDR to detect light and a piezo sounder to give audible feedback of the amount of light detected. This setup could be used as an alarm that indicates when a door has been opened, for example. Alternatively, you could use it to create a musical instrument similar to a Theremin. What is required? Piezo Sounder (or piezo disc) 2-Way Screw Terminal Light Dependent Resistor 10kΩ Resistor Piezo Sounder 2-Way Screw Terminal
18. Code… // Project 14 - Light Sensor int piezoPin = 8; int ldrPin = 0; intldrValue = 0; void setup() { // nothing to do here } void loop() { ldrValue = analogRead(ldrPin); tone(piezoPin,1000); delay(25); noTone(piezoPin); delay(ldrValue); }
19. Mechanism: LDR Working Vout = Vin * R2/(R2+R1) Table 4-1. Vout values for a LDR with 5v as Vin R1 R2 (LDR) Vout Brightness 10kΩ 100kΩ 4.54v Darkest 10kΩ 73kΩ 4.39v 25% 10kΩ 45kΩ 4.09v 50% 10kΩ 28kΩ 3.68v 75% 10kΩ 10kΩ 2.5v Brightest As you can see, as the brightness increases, the voltage at Vout decreases. As a result, the value you read at the sensor gets less and the delay after the beep gets shorter, causing the beeps to occur more frequently. If you were to switch the resistor and LDR, the voltage would increase as more light fell onto the LDR. Either way will work; it just depends how you want your sensor to be read.