15
Level II Programming Piezo Sounder Alarm & Sensors In What??? Obviously easiest and fluent Arduino

Session3

Embed Size (px)

DESCRIPTION

Arduino for Bed

Citation preview

Page 1: Session3

Level IIProgramming

Piezo Sounder Alarm & SensorsIn What???

Obviously easiest and fluent Arduino

Page 2: Session3

Flash Back of previous session

Page 3: Session3
Page 4: Session3

What will you learn Today???

New methods of Arduino• tone ()• analogRead ()• analogWrite ()• noTone ()

Project Project 1: Piezo Sounder Alarm

Project 2: Piezo Knock Sensor

Project 3: Light Sensor

Page 5: Session3

Lets start with methodstone () and noTone ()

tone() command to generate the frequency at the piezo sounder:

tone(8, toneVal);

The tone() command requires either two or three parameters, thus:

tone(pin, frequency)tone(pin, frequency, duration)

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. 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.

analogRead() and analogWrite() was described in last session.

Page 6: Session3

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

Page 7: Session3

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…

Page 8: Session3

// Project 1 - Piezo Sounder Alarm

float sinVal;int toneVal;

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);}

}

Page 9: Session3

The sinVal float variable holds the sin value that causes the tone to rise and fallThe toneVal variable 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

Page 10: Session3

Project 2: Piezo Knock SensorWhy 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 Terminal5mm LED (any color)1MΩ Resistor

Piezo Sounder2-Way Screw Terminal

Page 11: Session3

//Project 2: Piezo knock sensorint ledPin = 9; int piezoPin = 5; int threshold = 120;int sensorValue = 0; float ledValue = 0;

void setup() {pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, HIGH); delay(150); digitalWrite(ledPin, LOW); delay(150);digitalWrite(ledPin, HIGH); delay(150); digitalWrite(ledPin, LOW); delay(150);}

void loop() {sensorValue = analogRead(piezoPin);

if (sensorValue >= threshold) {

ledValue = 255;}

analogWrite(ledPin, int(ledValue) ); ledValue = ledValue - 0.05; if (ledValue <= 0) { ledValue = 0;} }

Page 12: Session3

Project 3: Light SensorWhy 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 TerminalLight Dependent Resistor10kΩ Resistor

Piezo Sounder2-Way Screw Terminal

Page 13: Session3

How is it connected…

Page 14: Session3

Code… // Project 14 - Light Sensor

int piezoPin = 8; int ldrPin = 0; int ldrValue = 0;

void setup() {// nothing to do here}

void loop() {ldrValue = analogRead(ldrPin); tone(piezoPin,1000); delay(25); noTone(piezoPin); delay(ldrValue); }

Page 15: Session3

Mechanism: LDR Working

Vout = Vin * R2/(R2+R1)

Table 4-1. Vout values for a LDR with 5v as Vin

R1 R2 (LDR) Vout Brightness10kΩ 100kΩ 4.54v Darkest10kΩ 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.