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.

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

Image: Self-made potentiometer by Kyösti Blinnikka. The stripe drawn with a pencil acts as the resistive material, and the wire in the middle as the wiper!

Image: Self-made potentiometer by Kyösti Blinnikka. The stripe drawn with a pencil acts as the resistive material, and the wire in the middle as the wiper!


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

Transcript

Oh, hi, sorry! I'm a bit fed up with the instrument already. The only way to make it shut up is to unplug it. You already know a programming trick that will give you more control of the sound: the if statement. You can program a condition for the instrument with the if statement. Let's tell it to only make noise if the light is low. The first thing to do is to check again what values the sensors produce. I want the sound to start only when I block the light with my hand. When my hand is close to the sensor the values are below 500. Okay, so now I know what the condition should be. The commands that make noise must be inside these curly brackets. Those commands are executed only then the condition is true - that is, when the LDR value is below 500. And when light is brighter than 500, the condition is not true and Arduino will skip the commands inside the if statement. Then there is no sound. Here is a few ideas for adjusting the sounds. If you look at the actual values you get from the LDR, you might notice the value doesn't usually go below 150. In that case you can 150 as the lowest bound. And what about the upper bound? Think about it. What is the biggest value the map command will ever get? You can see it from the if statement. The map command will do something only when the LDR value is less than 500. If you use 500 as the upper bound in the map command, you will hear the whole range of sounds. Try to tweak the range of the incoming values and also the frequency range. What if you only play low pitches? Or if you flip the frequency range completely? Or - can you change the condition in the if statement so that the instrument shuts up when it's dark? There are many things you could simulate with this instrument. Next, experiment a bit with the code. You might come up with some very original ideas.

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 with potVal?
  • ...replace the second or third parameter of the tone command with potVal?
  • ...change the "smaller than" symbol ( < ) into the "greater than" symbol ( > ) in the if statement?
  • ...use ldrVal as the length of the delay?

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