Lesson 11 Light Alarm

Share for us


This experiment is a very interesting one – a DIY phototransistor. DIY phototransistors use the glow effect and photoelectric effect of LEDs. That is, LEDs 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.


– 1 * SunFounder Uno board

– 1 * Breadboard

– 1 * USB cable

– Jumper wires

– 1 * Active buzzer

– 1 * Resistor (10KΩ)

– 1 * LED

– 1 * NPN Transistor S8050


LEDs not only have a glow effect, but also a photoelectric effect. They will generate weak currents when exposed to light waves.

NPN consists of a layer of P-doped semiconductor (the “base”) between two N-doped layers (see the picture above). 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.

There are three poles for the regions: base (b), emitter (e) and collector (c). They form two P-N junctions, namely the base-emitter junction and collector-base junction. The arrows in the NPN symbol (see the figure below) indicates the direction of the base-emitter junction.

The symbol of NPN is shown here. We can see the two PN junctions with unilateral conductivity inside, which enables it a switch component.

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.

Experimental 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, you can hear that the buzzer make sounds when the LED is shined.


//Simple Creation – Light Alarm
//Now, you can hear that the buzzer make sounds when the LED is shined.
void setup()
Serial.begin(9600); // start serial port at 9600 bps:
void loop()
int n=analogRead(A0); //read the value from analog pin AO
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 of the frequency of 10000 Hz (and 50% duty cycle) on a pin 5
pinMode(5,INPUT); //set the pin 5 as an input