Published on

Arduino for Bed

Published in: Education, Technology, Business
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Level II<br />Programming<br />Piezo Sounder Alarm & Sensors<br />In What???<br />Obviously easiest and fluent Arduino<br />
  2. 2. Flash Back of previous session<br />
  3. 3.
  4. 4. What will you learn Today???<br /> New methods of Arduino<br /><ul><li> tone ()
  5. 5. analogRead ()
  6. 6. analogWrite ()
  7. 7. noTone ()</li></ul>Project<br /><ul><li> Project 1: Piezo Sounder Alarm
  8. 8. Project 2: Piezo Knock Sensor
  9. 9. Project 3: Light Sensor</li></li></ul><li>Lets start with methods<br />tone () and noTone ()<br />tone() command to generate the frequency at the piezo sounder:<br />tone(8, toneVal);<br />The tone() command requires either two or three parameters, thus:<br />tone(pin, frequency)<br />tone(pin, frequency, duration)<br />The pin is the digital pin being used to output to the piezo and the frequency is the frequency of the tone in hertz. There is also the optional duration parameter in milliseconds for the length of the tone. <br />If no duration is specified, the tone will keep on playing until you play a different tone or you use the noTone(pin) command to cease the tone generation on the specified pin.<br />analogRead() and analogWrite() was described in last session.<br />
  10. 10. Project 1. Piezo Sounder Alarm<br />Why use?<br />By connecting a piezo sounder to a digital output pin, you can create a wailing alarm sound.<br />What is required?<br /> Piezo Sounder (or piezo disc)<br /> 2-Way Screw Terminal<br />Piezo Sounder<br />2-Way Screw Terminal<br />
  11. 11. How to connect?<br />Now you know what has to be included in your code by watching at <br />pictorial representation above.<br />But can you really code ??? Let’s try<br />So you understood what is it that’s remaining… <br />
  12. 12. // Project 1 - Piezo Sounder Alarm<br />float sinVal;<br />inttoneVal;<br />void setup() {<br />pinMode(8, OUTPUT);<br />}<br />void loop() {<br /> for (int x=0; x<180; x++) { <br />// convert degrees to radians then obtain sin value<br />sinVal = (sin(x*(3.1412/180)));<br />// generate a frequency from the sin value<br />toneVal= 2000+(int(sinVal*1000));<br /> tone(8, toneVal);<br /> delay(2);<br /> }<br />}<br />
  13. 13. The sinVal float variable holds the sin value that causes the tone to rise and fall<br />The toneValvariable takes the value in sinVal and converts it to the frequency you require.<br />You convert the value of x into radians :<br />sinVal = (sin(x*(3.1412/180)));<br />Then that value is converted into a frequency suitable for the alarm sound:<br />toneVal = 2000+(int(sinVal*1000));<br />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.<br />To make shrill sound and disturb someone who enters your room, I recommend you keep base value 1000 rather than 2000… :p<br />
  14. 14. Project 2: Piezo Knock Sensor<br />Why use?<br /> 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.<br />What is required?<br /> Piezo Sounder (or piezo disc)<br /> 2-Way Screw Terminal<br /> 5mm LED (any color)<br /> 1MΩ Resistor<br />Piezo Sounder<br />2-Way Screw Terminal<br />
  15. 15. //Project 2: Piezo knock sensor<br />intledPin = 9; <br />int piezoPin = 5; <br />int threshold = 120;<br />intsensorValue = 0; <br />float ledValue = 0;<br />void setup() {<br />pinMode(ledPin, OUTPUT); <br />digitalWrite(ledPin, HIGH); delay(150); <br />digitalWrite(ledPin, LOW); delay(150);<br />digitalWrite(ledPin, HIGH); delay(150); <br />digitalWrite(ledPin, LOW); delay(150);<br />}<br />void loop() {<br />sensorValue = analogRead(piezoPin); <br /> if (sensorValue >= threshold) { <br />ledValue= 255;<br /> }<br />analogWrite(ledPin, int(ledValue) ); <br />ledValue = ledValue - 0.05; <br />if (ledValue <= 0) { ledValue = 0;} <br />}<br />
  16. 16. Project 3: Light Sensor<br />Why use?<br /> 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.<br />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.<br />What is required?<br /> Piezo Sounder (or piezo disc)<br /> 2-Way Screw Terminal<br />Light Dependent Resistor<br /> 10kΩ Resistor<br />Piezo Sounder<br />2-Way Screw Terminal<br />
  17. 17. How is it connected…<br />
  18. 18. Code… <br />// Project 14 - Light Sensor<br />int piezoPin = 8; <br />int ldrPin = 0; <br />intldrValue = 0; <br />void setup() {<br />// nothing to do here<br />}<br />void loop() {<br />ldrValue = analogRead(ldrPin); <br />tone(piezoPin,1000); <br />delay(25); <br />noTone(piezoPin); <br />delay(ldrValue); <br />}<br />
  19. 19. Mechanism: LDR Working<br />Vout = Vin * R2/(R2+R1)<br />Table 4-1. Vout values for a LDR with 5v as Vin<br />R1 R2 (LDR) Vout Brightness<br />10kΩ 100kΩ 4.54v Darkest<br />10kΩ 73kΩ 4.39v 25%<br />10kΩ 45kΩ 4.09v 50%<br />10kΩ 28kΩ 3.68v 75%<br />10kΩ 10kΩ 2.5v Brightest<br />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.<br />