Lesson 19 Simple Creation – Light Alarm Introduction

Share for us

Introduction

This experiment is really interesting – to apply a DIY phototransistor. DIY phototransistors use the glow effect and photoelectric effect of LEDs – they will generate weak currents when some light is shined on it. And we use a transistor to amplify the currents generated, so the SunFounder Uno board can detect them.

Components

– 1 * SunFounder Uno board

– 1 * Breadboard

– 1 * USB cable

– 1 * Passive buzzer

– 1 * Resistor (10KΩ)

– 1 * LED

– 1 * NPN Transistor S8050

– Jumper wires

Principle

The schematic diagram

With the photoelectric effect, LEDs generate weak currents when exposed to light waves.

NPN consists of a layer of P-doped semiconductor (the “base”) between two N-doped layers. A small current entering the base is amplified to produce a large collector and emitter current. That is, when there is a positive potential difference measured from the emitter of an NPN transistor to its base (i.e., when the base is high relative to the emitter) as well as positive potential difference measured from the base to the collector, the transistor becomes active. In this “on” state, current flows between the collector and emitter of the transistor. The value of A0 will be larger than 0. By programming, we make the buzzer beep when A0 is larger than 0.

A 10kΩ pull-down resistor is attached to the transistor output stage in order to avoid analog port suspending to interfere with signals and cause misjudgment.

Note: In this experiment, the LED will not brighten until you shine strong light on it thus energizing the transistor. So this experiment should better be done in a dark environment.

Experimental Procedures

Step 1: Build the circuit

Step 2: Open the code file

Step 3: Select correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board

Now, shine a flashlight on the LED and you can hear the buzzer beep.

Code

//Simple Creation – Light Alarm //Now, you can hear that the buzzer make sounds when the LED is shined. //Email:support@sunfounder.com //Website:www.sunfounder.com //2015.5.7 void setup() { Serial.begin(9600); // start serial port at 9600 bps: } void loop() { int n=analogRead(A0); //read the value from analog pin AO Serial.println(n); if(n>0) //If there is a voltage { pinMode(5,OUTPUT); //set the digital pin 5 as an output tone(5,10000); //Generates a square wave(10000 Hz frequency,50% duty cycle) on pin 5 pinMode(5,INPUT); //set the pin 5 as an input } }