0% found this document useful (0 votes)
28 views36 pages

Lecture 04 Experiment-4, 5,6 7

This document describes experiments with interfacing an Arduino board with various sensors and components. It includes code examples to interface a switch and LED, an LDR light sensor, a speaker, and produce simple musical tones. The experiments cover reading input from a switch and controlling an LED, using an LDR to detect light levels and control an LED, producing tones with a speaker, and using functions to play simple musical notes on the speaker. Exercises are provided to modify the switch/LED code and additional experiments are described to interface other sensors and components to the Arduino.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views36 pages

Lecture 04 Experiment-4, 5,6 7

This document describes experiments with interfacing an Arduino board with various sensors and components. It includes code examples to interface a switch and LED, an LDR light sensor, a speaker, and produce simple musical tones. The experiments cover reading input from a switch and controlling an LED, using an LDR to detect light levels and control an LED, producing tones with a speaker, and using functions to play simple musical notes on the speaker. Exercises are provided to modify the switch/LED code and additional experiments are described to interface other sensors and components to the Arduino.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

ECE 2030

Arduino Based System


Design

Daw Khaing Su Wai


Experiment 4

Interfacing with a switches and LED


Circuit Diagram
Schematic Diagram
Code
#define ledPin 3 // choose the pin for the LED
#define switchPin 2 // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(switchPin, INPUT); // declare pushbutton as input
}
void loop()
{
val = digitalRead(switchPin); // read input value
if (val == HIGH)
{ // check if the input is HIGH (button released)
digitalWrite(ledPin, HIGH); // turn LED OFF
} else {
digitalWrite(ledPin, LOW); // turn LED ON } }
}
}
Exercises
1. You want the LED to light when the button is pressed, write HIGH out when the
value read from the pin connected to the button is LOW. Try this program and
make sure it works. (If it doesn’t, rotate the button 90 degrees and try again.)
2. Change it so the LED is normally on and pressing the button turns it off.
Experiment 5

Interfacing with LDR and Speaker


Interfacing Arduino with LDR
• LDR ( light dependent resistor ) also called photoresistors are responsive to light.
Photoresistors are used to indicate the intensity or the presence or the absence of light. When
there is darkness the resistance of photoresistor increases and when there is sufficient light it
dramatically decreases.
• LDR ( light dependent resistor ) which has two terminals. Terminal one is the signal pin which
should be connected according to the code. Another terminal is considered as the ground pin
which should be connected to the ground of the system.
Interfacing Arduino with LDR
• In order to detect the intensity of light or darkness, we use a sensor called an LDR (light
dependent resistor). The LDR is a special type of resistor that allows higher voltages to pass
through it (low resistance) whenever there is a high intensity of light, and passes a low voltage
(high resistance) whenever it is dark.
• In the dark, a photoresistor can have a resistance as high as several megohms (MΩ), while in
the light, a photoresistor can have a resistance as low as a few hundred ohms.
Interfacing Arduino with LDR
• The LDR gives out an analog voltage when connected to VCC (5V), which varies in magnitude in
direct proportion to the input light intensity on it. That is, the greater the intensity of light, the
greater the corresponding voltage from the LDR will be. 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). When there is sufficient light in its environment or on its
surface, the converted digital values read from the LDR through the Arduino will be in the
range of 800-1023.
Interfacing Arduino with LDR

• First, you need to connect the LDR to the analog


input pin 0 on the Arduino. You have to use a
voltage divider configuration to do this.
• One leg of the LDR is connected to VCC (5V) on the
Arduino, and the other to the analog pin 0 on the
Arduino. A 100K resistor is also connected to the
same leg and grounded.
Interfacing Arduino with LDR

int sensorPin = A0; // select the input pin for LDR


int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); //sets serial port for communication
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
delay(100);
}
Turn LED On and Off Through LDR
Turn LED On and Off Through LDR
• Connections of LDR sensor : First terminal should be connected to analog pin 0
(A0) of Arduino. Second terminal should be connected any one led pf the
resistor. Another leg of resistor(10k ohm) should be connected to Gnd of
Arduino.

• Led connections : Positive pin should be connected to digital pin 5 of Arduino.


Negative pin should be connected any one led pf the resistor(10k ohm).
Another leg of resistor should be connected to Gnd of Arduino.

• 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

Sound, simple note


Sound
• In order to make a sound, we turn the speaker on and off a certain number of
times per second.
• Specifically, middle A (a musical note) is 440 Hz. (Hz is short for and is
pronounced “Hertz” - the number of times (or cycles) per second.)
• So all we need to do to play a middle A is to make a sound wave that cycles 440
times per second.
• In order to calculate how much time we need to have the speaker on for:

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

Arduino Uno (Any Type)


Speaker (Any Type)
Jumper Wires (Female To Male [Any Type])
Music using functions
Music using functions
Music using functions
void setup() {
// put your setup code here, to run once:
pinMode (8,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
tone(8, 262, 250);
delay(325);
tone(8, 294, 250);
delay(325);
tone(8, 330, 250);
delay(325);
tone(8, 349, 250);
delay(325); There is always a delay after tone. The delay has to drag 30%
tone(8, 392, 250); longer than the tone duration to ensure the tone is completed.
delay(325); e.g- The tone duration for 1 beat is 250ms, thus we need to
tone(8, 440, 250); allow a 325ms delay.
delay(325);
tone(8, 494, 250);
delay(500);
}
Music using functions

#define NOTE_C4 262


#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C5 523

const int kPinSpeaker = 9;

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 ourTone(int freq, int duration)


{
tone(kPinSpeaker, freq, duration);
delay(duration);
}
Music using functions
(Happy Birthday)
Music using functions
(Happy Birthday)

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

You might also like