Arduino Light Sensor With Buzzer and Blinking LEDs



Introduction: Arduino Light Sensor With Buzzer and Blinking LEDs

This is a pretty cool project that I created with my daughter. The final product is fun and gives you something cool to take to school for a show-and-tell or show your relatives when they come to visit!

It's a combination of three other small Arduino projects:

- a light sensor using a photocell

- using a piezo buzzer to create a beeper with variable tone and speed and

- blinking a sequence of LEDs with variable speed

The final product is a light sensor with a series of LEDs that (a) blink faster with more light (and slower with less) and (b) beeps faster and at a higher pitch with more light (and slower and lower pitch with less.

When I created the breadboard diagram using Fritzing I made some adjustments from the original breadboard so that it's clear to visualize the wires etc.

What you will need:

- breadboard

- Arduino Uno

- 5x LEDs

- photovoltaic cell

- piezo speaker

- 5x 220 ohm resistor (for LEDs)

- 500 ohm resistor (for photovoltaic cell)

- 100 ohm resistor (for piezo buzzer)

To be completely honest, I've selected those resistors based on the work done by other people and other projects I've found online. I'm sure there is science and formulas behind it but you will not find it in this Instructable, sorry.

Step 1: Step 1 - Wire the First LED

Start with the basics: connect the board to GND and the 5V pins and make sure you have jumpers connecting the positive and negative rails to the other side of the breadboard, making them "active" and helping you build your circuits without crossing over the breadboard too much.

We want to create a sequence of LEDs to be operated independently.

Let's connect the first LED. The positive side of the LED connects to pin 13. The negative side of the LED you will connect with a 220 ohm resistor, that is then connected to GND.

Step 2: Step 2 - Wire the Other 4 LEDs

Now wire the other four LEDs following the same plan: positive sides each to pins 12, 11, 10 and 9 respectively, and negative sides to ground, using each a 220 ohm resistor.

The LEDs are ready: you will be able to control them independently, each one through a separate PIN.

Step 3: Step 3 - Wire the Piezo

We want our sensor to beep. For that we'll use a Piezo buzzer, so let's wire it.

The negative wire connects to GND and the positive wire connects first to a 100 ohm resistor, than to pin 7. As I said before, the 100 ohm resistor is suggested in other projects that I found online.

Step 4: Step 4 - Wire the Photovoltaic Cell

The photovoltaic cell is a simple resistor that becomes more conductive when exposed to light. So, with zero light it blocks 100% of current, and with full light it allows current to flow through. The way Arduino "reads" this is that zero light returns a value of 0 and full light returns a value of 1024.

The photovoltaic cell doesn't have a negative and positive side. So you will wire one side to the positive rail of the board. The wiring of the negative side is slightly complicated: you will connect it both (a) to the negative rail using a 500 ohm resistor and (b) directly to pin A0 .

The circuit is ready. Let's look at the code.

Step 5: Step 5 - the Code

You will find the full code below to cut and paste. It has //comments so that you can understand what is going on where.

This is what the code is doing:

1 - The sensor reads the light level, measuring it from 0 to 1024

2 - We "translate" this reading into an instruction for the buzzer to beep and the LEDs to blink.

3 - For the LEDs, we translate the reading of the light into milliseconds of blinking. The less light, the slow it blinks. Doing some tests, even when the light is strong, it doesn't read more than 700 or 800 (hard to get to 1024) so I used 700 as my "top" light level. Because of the way the MAP function works, if light reaches above 700, it would turn the blinking time into a negative number - and the whole thing crashes. So I created a rule that Blink_Time cannot be shorter than 20 milliseconds.

4 - The LEDs light in sequence (that is, the first one turns on, then when it turns off the next one turns on etc)

5 - For the buzzer, we translate the reading of the light (0 - 1024) to hertz (120 to 1500), so the more light, the higher the pitch.

6 - The buzzer beeps at the same time as the first, third and fifth LED, (and for the sale length of time) then pauses as the LEDs pause. This creates a pulsating effect, light and sound in the same rhythm.

This is it. Enjoy it!


// Light sensors with beeps and sequence of leds like an airport

// ints for blinking

int Blink_Time = 20; // creates this variable to be used for the length of the blinks and intervals

int Light_Level = 0; //creates this variable to be used for the level of light

int Light_Pin = A0; // pin 0 will be used for the photocell

// ints for buzzer

int Buzz_Tone = 300; // creates this variable for the tone of the buzzer

int Buzz_Tone_Max = 1500; // max herz for buzz tone

int Buzz_Tone_Min = 120; // min herz for buzz tone

void setup() {

pinMode(9, OUTPUT); // initialize pint 9 - 13 as as outputs for the leds

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

pinMode(12, OUTPUT);

pinMode(13, OUTPUT);

pinMode(7, OUTPUT); // Set buzzer - pin 7 as an output for the buzzer

Serial.begin(9600); Serial.println("Ready"); // Open the serial port at 9600 baud to monitor the behavior of the variables


void loop() {

Light_Level = analogRead(Light_Pin); // reads light level

Blink_Time = map (Light_Level, 0, 700, 300, 1); // sets blink time according to light level (more light, more speed)

if (Blink_Time <= 20) {Blink_Time = 20;} // sets a minimum limit for the blink time. Because the light level can go above 700, the mapping function can cause blink time to become negative, in which case the program freezes.

// sets buzz tone according to light level (more light, more herz, higher pitch)

Buzz_Tone = map (Light_Level, 0, 700, Buzz_Tone_Min, Buzz_Tone_Max);

// prints all variables in the serial monitor for you to see what is happening

Serial.print("Light level = ");


Serial.print(" Blink time = ");


Serial.print(" Buzz_Tone = ");


Serial.println(" ");

// first LED

tone(7, Buzz_Tone); // starts the beep at the same time that the first led turns on

digitalWrite(9, HIGH); // turn the LED on (HIGH is the voltage level)

delay(Blink_Time); // wait for [blink time]

digitalWrite(9, LOW); // turn the LED off by making the voltage LOW

noTone(7); // stops the beep

// second LED

// no beep here, I want only three beeps so I put them on the first, third and fifth LEDs

digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)

delay(Blink_Time); // wait for [blink time]

digitalWrite(10, LOW); // turn the LED off by making the voltage LOW

// third LED

tone(7, Buzz_Tone); //beep

digitalWrite(11, HIGH); // turn the LED on (HIGH is the voltage level)

delay(Blink_Time); // wait for [blink time]

digitalWrite(11, LOW); // turn the LED off by making the voltage LOW


// fourth LED

digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)

delay(Blink_Time); // wait for [blink time]

digitalWrite(12, LOW); // turn the LED off by making the voltage LOW

// fifth LED

tone(7, Buzz_Tone);

digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)

delay(Blink_Time); // wait for [blink time]

digitalWrite(13, LOW); // turn the LED off by making the voltage LOW


delay (5*Blink_Time); // pause between series of blinking LEDs + Beeps


Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Reclaimed Materials Contest

      Reclaimed Materials Contest
    • Unusual Uses Contest

      Unusual Uses Contest