Transcript
Noises From the Dark
Introduction
What is the name of the new component you'll be using next?
Parts
Do this
Collect these parts. In addition to these, you will need an Arduino UNO or Mehackit Board, a USB cable, a breadboard and a bunch of jumper wires.
Part | Image | Description |
---|---|---|
LDR (light-dependent resistor, photoresistor) | The resistance of an LDR changes with the amount of light. The brighter it gets, the less an LDR resists an electric current. | |
Resistor 10kΩ (10,000Ω ) | 10 kilo-ohm (10,000 ohm) resistor is used with an LDR. Stripe colors: brown, black, orange, gold | |
Potentiometer | A potentiometer is a variable resistor which is adjusted by turning the knob. | |
Piezo speaker | A piezo can transform a quickly alternating voltage into sound. Some piezos can be used as a knock sensor. You will need a so-called passive piezo for this task. Active piezo speakers are a bit more expensive and they are programmed differently (they also work with Arduino) |
Remember which pins you connect sensors like LDR and potentiometer to?
Circuit
Do this
Make a circuit according to the diagram below.
How to connect the potentiometer:
- connect one side to ground
- connect the other side to 5V
- connect the center pin to A1
Check out how to connect diferent potentiometers
What happens when you turn the knob of a potentiometer or when an LDR reacts to light?
Educator notes
Potentiometers also make use of the voltage divider.
A potentiometer contains a resistive strip and a conductive wiper which turns with the knob of the potentiometer. The resistance between the center pin and the side pins changes when the knob is turned.
If this potentiometer was connected to an Arduino and the wiper was turned all the way to GND, the voltage measured from the center pin would be 0 volts (value 0). If the wiper is in the 5V end, the measured voltage is 5V (value 1023).
It doesn't matter which of the side pins is connected to ground and which to 5V.
You could even make your own potentiometer with a pencil and a couple of wires:
Programming 1: Check the Sensors
Do this
Write a new program according to the example.
- Open the serial monitor, turn the knob of the potentiometer and cover the LDR. See what kind of values you get!
Code Example:
- If your sensors don't work, check the breadboard connections and your code!
Why do you need to use both Serial.print and Serial.println in this code?
Educator notes
Key points:
- practising the sensor workflow: always check the sensor values first to make sure everything works
- using variables to store sensor data
- reading and monitoring two sensors
Example code:
//variable for storing ldr values:
int ldrVal;
//variable for storing potentiometer values:
int potVal;
void setup()
{
//start serial communication:
Serial.begin(9600);
}
void loop() {
//store value from A0 to ldrVal:
ldrVal = analogRead(A0);
//store value from A1 to potVal:
potVal = analogRead(A1);
//print the sensor values
//with clarifying text:
Serial.print("ldr: ");
Serial.print(ldrVal);
Serial.print(", potentiometer: ");
Serial.println(potVal);
//delay of 20ms:
delay(20);
}
Programming 2: Noise!
Do this
Continue your code according to the example.
- When you upload this code to Arduino, you should hear sounds that react to the LDR and potentiometer!
Code Example
Check the video again if you have trouble with the code! Also remember the Arduino Reference.
Which part of this program defines the range of sound frequencies this instrument can play?
What does the potentiometer do to the sounds of the instrument?
Educator notes
At this point there are likely to be some noisy moments in the classroom. Hopefully not too noisy! There’s something immediately compelling and interactive about sound projects - many students enjoy them.
Key points:
- using the map command
- making sounds and rhythms that react to sensor data
Example code:
int ldrVal;
int potVal;
//create an integer variable called sound:
int sound;
void setup()
{
Serial.begin(9600);
//define pin 9 as an output:
pinMode(9, OUTPUT);
}
void loop() {
ldrVal = analogRead(A0);
potVal = analogRead(A1);
//give variable sound a value: map it from ldrVal:
sound = map(ldrVal, 0, 600, 50, 1300);
//play a note with tone (use variable sound as pitch):
tone(9, sound);
//add a delay (length: potVal):
delay(potVal);
Serial.print("ldr: ");
Serial.print(ldrVal);
Serial.print(", potentiometer: ");
Serial.println(potVal);
delay(20);
}
Programming 3: More Control
Do this
Write an if statement in the loop part. There should be sound only when the LDR is covered with a hand
- write the condition:
ldrVal
is smaller than 500 - put commands that make sound or rhythm inside the if statement
- remember the curly brackets
- look at the serial monitor: replace 500 with a better value if needed
EXTRA
Experiment with the commands! What happens if you...
- ...change the note range with the
map
command (last two numbers)? - ...replace some parameter inside the
map
command withpotVal
? - ...replace the second or third parameter of the
tone
command withpotVal
? - ...change the "smaller than" symbol (
<
) into the "greater than" symbol (>
) in the if statement? - ...use
ldrVal
as the length of thedelay
?
When you look at the serial monitor, the numbers run slower when there is sound. Why?
Educator notes
Key points:
- practising using if statements
- using sensor data as a threshold
Only the loop part changes:
void loop() {
ldrVal = analogRead(A0);
potVal = analogRead(A1);
if (ldrVal < 500) {
sound = map(ldrVal, 0, 600, 50, 1300);
tone(9, sound);
delay(potVal);
}
Serial.print("ldr: ");
Serial.print(ldrVal);
Serial.print(", potentiometer: ");
Serial.println(potVal);
delay(20);
}