Lesson 10 Controlling Voice by Light

Introduction

Since you have learnt how to use a photoresistor, in this lesson, you will learn how to control a buzzer to make it beep in different frequencies by the photoresistor.

 

Compoment

1* SunFounder Uno board

- 1 * USB data cable

- 1 * Photoresistor

- 1 * Buzzer (Active)

- 1 * Resistor (10KΩ)

- Several jumper wires

-1 * Breadboard

 

Experiment Principle

When you shine some light on the photoresistor, if the incident light gets stronger, the resistance of the photoresistor will decrease; if the incident light becomes weaker, the resistance will increase.

In this experiment, the output of the photoresistor is sent to pin A0 on the SunFounder Uno board and then processed by the ADC on the board to output a digital signal. We use this digital signal as the parameter of the delay() function in the sketch to make the buzzer beep.

When the incident light is strong, the output value gets greater, thus the buzzer will beep slowly; when incident light is weak, the output value is smaller, thus the buzzer will beep sharply.

 

 

Experiment Procedures

Step 1: Build the circuit

 

The schematic diagram

  

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorials on our website)

Step 3: Compile the code

Step 4: Upload the sketch to the SunFounder Uno board

Now, if you place the photoresistor in a dark environment, the buzzer will beep sharply; if you shine a flashlight on the photoresistor, the buzzer beeping will slow down.

 

 

Code

/**********************************************************
* name:Controlling Buzzer by Photoresistor
* function: if you place the photoresistor in a dark environment, the buzzer will beep sharply;

* if you shine a flashlight on the photoresistor, the buzzer beeping will slow down.
**********************************************************/
//Email:support@sunfounder.com
//Website:www.sunfounder.com

/*******************************/
const int photocellPin = A0; //photocell attach to
int sensorValue = 0; // value read from the sensor
const int buzzerPin = 9; //buzzer attach to

void setup()
{
  pinMode(buzzerPin, OUTPUT); //initialize buzzer as an output
}
void loop()
{
  sensorValue = analogRead(photocellPin); //read the value of A0
  digitalWrite(buzzerPin, HIGH); //
  delay(sensorValue); //wait for a while,and the delay time depend on the sensorValue
  digitalWrite(buzzerPin, LOW);
  delay(sensorValue);
}

 

Copyright © 2012 - 2016 SunFounder. All Rights Reserved.