Lecture 04 Experiment-4, 5,6 7
Lecture 04 Experiment-4, 5,6 7
• Connect the A0 pin of the Arduino to the same column where the LDR and
resistor is connected (Since the LDR gives out an analog voltage, it is connected
to the analog input pin on the Arduino. The Arduino, with its built-in ADC
(Analog to Digital Converter), then converts the analog voltage from 0-5V into a
digital value in the range of 0-1023).
Code
const int ledPin = 5; // digital pin 5
const int ldrPin = A0; // analog pin 0
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Here LED is determined as an ouput or an indicator.
pinMode(ldrPin, INPUT); // Here LDR sensor is determined as input.
}
void loop() {
int ldrStatus = analogRead(ldrPin);
Code
if (ldrStatus <= 200) {
digitalWrite(ledPin, HIGH); // If LDR senses darkness led pin high that means led will glow.
Serial.print("Darkness over here,turn on the LED : ");
Serial.println(ldrStatus);
} else {
digitalWrite(ledPin, LOW); // If LDR senses light led pin low that means led will stop glowing.
Serial.print("There is sufficeint light , turn off the LED : ");
Serial.println(ldrStatus);
}
}
In this sketch, we set a threshold light value as 200, but it can vary for your projects. You will need
to find out the particular value at which the light bulb should turn on. This needs to be done after
testing it empirically.
Interfacing Arduino with Speaker
Arduino has 2 functions built-in that handle making sounds as well.
The first is tone() which takes 2 required parameters (and an optional third).
• tone(pin, frequency, duration)
OR
• tone(pin, frequency)
• When the duration is over it will stop.
The second is noTone() which takes a single parameter:
• noTone(pin)
• It basically stops whatever tone is playing on that pin.
• (A strange warning. When the tone() function is running, PWM won’t run on pin
3 and pin 11. So if you are using a speaker in your sketch, you might want to
avoid using those pins as PWM entirely.
Interfacing Arduino with Speaker
Interfacing Arduino with Speaker
• You need to connect the buzzer’s GND to Arduino’s GND, and the other wire to
one GPIO of the Arduino (we have chosen pin 7). You can optionally add a small
resistor (~100 Ohm), between the GPIO and the buzzer.
• In the loop, we use the tone() function to create a 2000 Hz tone on the buzzerPin
for half a second, after which we stop the tone using the noTone() function, for
another half second. This goes on in the loop and creates the beeping effect.
Interfacing Arduino with Speaker
#define buzzerPin 7 // buzzer to arduino uno pin 7
void setup(){
pinMode(buzzerPin, OUTPUT); // Set buzzer - pin 7 as an output
}
void loop(){
tone(buzzerPin, 2000); // Send 2000Hz sound signal...
delay(500); // ...for 0.5 sec
noTone(buzzerPin); // Stop sound...
delay(500); // ...for 0.5 sec
}
Experiment 6
https://mixbutton.com/mixing-articles/music-note-to-frequency-chart/
Simple note
Simple note
• We need to have the speaker on (and then off) for 1136 microseconds. Run this program and you
should hear an A (musical note) that will not stop (until you pull power.)
const int kPinSpeaker = 9;
const int k_timeDelay = 1136;
void setup() {
pinMode(kPinSpeaker, OUTPUT);
}
void loop()
{
digitalWrite(kPinSpeaker, HIGH);
delayMicroseconds(k_timeDelay);
digitalWrite(kPinSpeaker, LOW);
delayMicroseconds(k_timeDelay);
}
Experiment 7
Music using functions
Maker+UNO+Plus-+Beginner+Learning+Guide.pdf
Music using functions
Components :-
void setup()
{
pinMode(kPinSpeaker, OUTPUT);
}
Music using functions
void loop()
{
ourTone(NOTE_C4, 500);
ourTone(NOTE_D4, 500);
ourTone(NOTE_E4, 500);
ourTone(NOTE_F4, 500);
ourTone(NOTE_G4, 500);
ourTone(NOTE_A4, 500);
ourTone(NOTE_B4, 500);
ourTone(NOTE_C5, 500);
noTone(kPinSpeaker);
delay(2000);
}
void setup() {
pinMode (8,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
tone(8, 392, 125);
delay(163);
tone(8, 392, 125);
delay(163);
tone(8, 440, 250);
delay(325);
tone(8, 392, 250);
delay(325);
tone(8, 523, 250);
delay(325);
tone(8, 494, 500);
delay(1000);
}
Music using functions
(Happy Birthday)
Music using functions
(Happy Birthday)
Music using functions
(Happy Birthday)
#include "pitches.h"
void setup() {
pinMode (8,OUTPUT);
}
int melody[] =
{
NOTE_G4, NOTE_G4,NOTE_A4, NOTE_G4, NOTE_C5,
NOTE_B4,
};
int noteLength[] = {
8, 8, 4, 4, 4, 2 };
void loop()
{
for (int thisNote = 0; thisNote < 6; thisNote++){
int duration = 1000 / noteLength[thisNote]; //(1000/8=125)
tone(8, melody[thisNote], duration);
int pauseBetweenNotes = duration * 1.3; //(125*1.3=163)
delay(pauseBetweenNotes);
noTone(8);
}
}