 # 21 – ARDUINO AND LDR PHOTOSENSORS

#### Objectives

• A nice component, LDR (Light-Dependent Resistor).
• Voltage dividers.
• A light-dependent Theremin.

#### BILL OF MATERIALS Arduino Uno or equivalent.We can use any other Arduino model in this chapter. A solderless Breadboard. Some Jumper wires A 330 Ohm resistor A photoresistor or LDR

#### PHOTOSENSORS

A photoresistor or LDR (Light Dependent Resistor) is a photoelectric device whose resistance varies depending on the incident light intensity. This resistance is very low, a few Ohms, with a strong incident light but it grows enormously as light decreases (up to several megaohms MΩ).

They are often used as light sensors, to boot lights automatically when darkness exceeds a certain threshold, or as motion sensors (when something gets interposed).

We will use a typical LDR in this chapter, which is quite easy to get and is sensitive to changes in ambient light. We will assemble a prototype with an LDR and a buzzer, that we saw in the last session, to build a rudimentary theremin, that I hope keep you entertained for a while.

A theremin is a rudimentary audio synthesizer that generates a continuous spectrum variable audio signal, analog, depending on a control signal (do not panic, it’s a nonsense).

The circuit uses an LDR to generate the control signal and we will calculate the frequency depending on the voltage drop read in our photoresistor.

• LDRs are not exactly quick to react to light, taking up to a few tenths of a second to react. This is not important for an alarm light, but makes it impossible to be used to send information using light

But before we go on we need to digress in order to know what is a voltage divider.

#### VOLTAGE DIVIDERS

We talked about Ohm’s law in previous chapters:

V = R * I

But we do not talk about how to combine resistors and it is time to do it, to the despair of those who hate Maths.
We can combine a pair of resistors R1 and R2 in two ways: in series and in parallel:

in series in parallel When we put two resistors in series, the equivalent resistance is the sum of both: And when we put them in parallel, we can calculate the equivalent resistance this way: Imagine now a circuit like this:

As the resistors are put in series the total value is R1 + R2. If Vin is 5V the current intensity that flows through the circuit will be: The million dollar question is now, which will be the value of Vout If we measure the voltage between the two resistors? Easy: If there is anyone awake, he will realize that, provided that R1 and R2 have the same value, Vout is exactly the half of Vin. But if R1 or R2 were a potentiometer (or LDR), any variation would cause a change in the output voltage, Vout.

This is what is known as a voltage divider and it is the most practical circuit to reduce the voltage of an input signal, I bet you will use it more than once.

For example, Arduino’s analog to digital converters accept a maximum of 5V, but many industrial signals range from 0 to 12V. If you connect them directly to an analog pin, A0 for example, you will burn the chip.
But using the trick of the voltage divider and calculating properly the resistors (yes, you can do it with the information on the previous page) you can adapt the voltage for your Arduino to use a signal that originally had burnt it down.

Voltage dividers are a very simple circuit and they ought to be part of your electronic arsenal. They solve many problems using only a resistor and a potentiometer and are ideal for processing signals that, due to excess of voltage, would be outside the scope of your Arduino.

In fact, in this chapter we will assemble a voltage divider with a resistor and LDR and we will read the voltage drop on it. It will serve us as an example of voltage dividers and the output voltage will be used as a control signal to calculate the frequency at which the buzzer will vibrate.

#### CIRCUIT SCHEMATIC DIAGRAM FOR AN OPTIC THEREMIN

Although the title is impressive, I must admit it, the circuit is not that terrible: Note that the LDR R1, together with R2, form a voltage divider. The reading of the pin A0 depends on the incident light on the LDR. The wiring schematic diagram is just as easy: #### The sketch

The program is very simple. We read the voltage drop in A0 and use it to map a frequency between 20 and 5.000 Hz that will be later passed in the call to the function tone(), and that’s all, folks.

```
const int pinBuzzer = 13 ;
void setup()
{
pinMode (pinBuzzer , OUTPUT) ;
}
void loop()
{
int n = map (p, 0,1024, 20, 5000) ;
tone ( pinBuzzer, n) ;
}```

• To test the circuit I suggest you to put a strong light bulb 50 cm above the LDR and move your hand, especially from top to bottom and vice versa.
• You will listen a more or less high-pitched continuous tone that varies its frequency depending on the light incident on the LDR
• It is said that the human ear ranges between 20Hz and 20KHz (although this is true for any teenager with a very fine ear), for gray hair people a range between 40Hz and 10Khz is already quite optimistic. Therefore, we have mapped the potentiometer values, ranging from 0 to 1024, to fall between 20 and 5Khz, but I suggest you to change these values and see what happens.

I think you’ll agree with me that it was so easy, I can’t let you go without suggesting any other problems. Let’s see.

The sound obtained is of continuous spectrum, i.e., reproduces continuous frequencies in the range that is capable. But our ears are accustomed to hearing the notes in tones and semitones of a given frequency, as we saw in the chapter 20. What would you do in order the theremin to produce the notes that we defined there?

The result could be something like that:

```
const int pinLDR = 0 ;
const int pinBuzzer = 13 ;

int tono[ ] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440,466, 494};
// mid C C# D D# E F F# G G# A
void setup()
{ pinMode (pinBuzzer , OUTPUT) ;
}

void loop()
{
int n = map (p, 500,1024, 0, 12) ; // Ell array solo tiene 12 notas
tone(pinBuzzer, tono[n]);
delay(300);
}```

We have used an array to store the temperament scale frequencies of an octave. Then we map the reading of the pin A0 to an integer between 0 and 12, because the array has only 12 notes, and we use its value to read the corresponding frequency.

In my case, moreover, A0 provided values only between 500 and 1024, so that’s why I corrected the scale. Finally the delay prevents the change between notes to be instantaneous, because otherwise we would not notice it very much.

#### SUMMARY

• We have introduced the photoresistors or LDRs.
• We have introduced voltage dividers.
• You will use them more than once in your prototypes.
• We use them to reduce voltage in input signals and as adapters to read voltage drops in variable resistors.
• We saw how to assemble an optic Theremin.