0% found this document useful (0 votes)
13 views4 pages

8 PhotoResistorOrgan

The document describes a lab exercise to build a circuit using an Arduino, light dependent resistor, and relay to generate musical tones based on light intensity. The circuit will map light sensor values to notes in a pentatonic scale. Students will write code to read the sensor, map values to frequencies, and use the relay to play the notes.

Uploaded by

modyalt002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

8 PhotoResistorOrgan

The document describes a lab exercise to build a circuit using an Arduino, light dependent resistor, and relay to generate musical tones based on light intensity. The circuit will map light sensor values to notes in a pentatonic scale. Students will write code to read the sensor, map values to frequencies, and use the relay to play the notes.

Uploaded by

modyalt002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

WLC Arduino Lab Exercise

Light Dependent Tone Generator


Introduction
The objective of this lab is to design an Arduino-based tone generator that generates musical notes
based on the input from a Light Dependent Resistor (LDR). The synthesized notes will be from a
pentatonic scale, and their frequencies will be mapped according to the LDR's input values.
The sound itself will be (crudely) generated by using a relais! Below are the circuit symbols for
these components:

LDR Top: Circuit symbol of a relais. Bottom: Inner


view of a relais. The coil vibrates with the
frequency of the digital pin, thus creating a
"tone".

An LDR, also known as a photoresistor, is a type of resistor whose resistance changes based on the
intensity of light falling on it. As light intensity increases, the resistance of the LDR decreases,
allowing more current to flow through it. LDRs are commonly used in light-sensing applications
such as automatic street lights, camera exposure control, and in this case, to provide input to an
Arduino for light-based control of electronic devices.

1
Lab Tasks
Circuit Setup
• Connect the LDR to analog input pin A0 using a voltage divider circuit. (560 Ohm,
green blue brown).
• Connect a pushbutton (or simply use a wire) connecting to GND to digital pin 2.
• Instead of the tinkercad-buzzer, connect the relais as described below.

Of course, you can try it in Tinkercad first, using a buzzer!

Connection of the relais


Using jumper cables, connect the relais as follows
• Black on relais cable to GND on Arduino.
• Red on relais cable to +5V on Arduino.
• Yellow on relais cable to digital pin 9.

Code Implementation
• Write Arduino code to read the input from the LDR on A0 using analogRead().
• Map the LDR values to five ranges using the map() function.
• Define the frequencies for the notes of the pentatonic scale (C, D, E, G, A) as
constants. See table below for the values.
• Use conditional statements to play corresponding notes based on the mapped LDR
values.
• The buzzer is turned on at a certain frequency with tone(pin, frequency)
• The buzzer is turned off with noTone(pin).
• Implement logic to only play notes when the pushbutton is pressed.

Pentatonic scale notes and frequencies:


C4 262, D4 294, E4 330, G4 392, A4 440

2
Starter Code
#define NOTE_C4 262 // Define frequencies for pentatonic scale
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_G4 392
#define NOTE_A4 440

#define BUZZER_PIN 9 // Buzzer connected to digital pin 9


#define BUTTON_PIN 2 // Button connected to digital pin 2
#define ANALOG_PIN A0 // Analog input pin A0

void setup() {
Serial.begin(9600);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);

//test the buzzer/relais once at startup


tone(BUZZER_PIN, NOTE_C4);
delay(500);
tone(BUZZER_PIN, NOTE_D4);
delay(500 );
tone(BUZZER_PIN, NOTE_E4);
delay(500 );
tone(BUZZER_PIN, NOTE_G4);
delay(500 );
tone(BUZZER_PIN, NOTE_A4);
delay(500 );
tone(BUZZER_PIN, NOTE_C4);
delay(2000);
noTone(BUZZER_PIN);
}

void loop() {
int sensorValue = analogRead(ANALOG_PIN); // Read analog input
int mappedValue = 3; //TODO Map a range of sensorValue to 5 ranges (use map(...))

Serial.print(sensorValue);
Serial.print(", ");
Serial.println(mappedValue);

//TODO: play note only when switch is pressed

//TODO: play note according to mappedValue

delay(250);
noTone(BUZZER_PIN);
}

3
Test
• Upload the code to the Arduino board.
• Verify that pressing the pushbutton plays the notes according to the LDR input.
• Test the volume control functionality (if implemented).

Experimentation
• Modify the code to change the mapping of LDR values to note frequencies. Observe
the range of values from analogRead and adapt the mapping.
• Try adding more notes to the scale and adjust the code accordingly.
• Explain the function of the voltage divider formed by the LDR and the 560 Ohm
resistor. What happens at full darkness (LDR is highest), what at full brightness
(LDR has approx. 500 Ohm)?

Report
1. Make sure your Tinkercad circuit is up to date (shows All changes saved in top right corner).
2. Make sure you've taken all required photographs and screenshots.
3. Write the report using the report template.
4. Hand in the report via Moodle as PDF.
5. Upload the sourcecode to Moodle as well!

You might also like