Step 1: Key Points
Photosensitive diode is a very common electrical component that changes resistance according to the amount of light it received. More light means lower resistance.
Maybe we all had thought about making a smart light which can turn itself on in darkness. In this lesson, we will learn some knowledge about on of this kind of sensor>>photosensitive diode
1. Get to know the usage and principle of the photosensitive diode.
2. Get to be well-executed in Arduino IDE and the usage of function in code
3. Program to let the buzzer ring to indicate the direction of light
Step 2: Example of How to Use the Photosensitive Diode
There are two photosensitive diodes which are installed in the front of the robot, one of them faces the left side and the other faces right. So it can be used for making a light-hunter robot. The example shows how to use a buzzer to indicate the direction of light.
Open folder “light” >> light.ino:
1. Code of the sample
Connect your robot with your computer, then upload the code.
After the first step, take a flashlight to irradiate each sensor, then listen the sound from the buzzer。You can also let the sensor face toward outside and it will be the same phenomenon。
You can also use the serial port to observe the change of analog value changed by light：
2. the value returned with the different direction of the light
The number returned is not the voltage value. When it measures a 5V input signal, the value will be 1023 and 0V to 0. So when the return is 620, the real value of voltage is (620/1024)*5=3.03V.
Step 5: Code Analysis
Use “BUZZER” as the number of the pin connect to the buzzer
#define BUZZER 16
Configure the mode of pin and communication speed
pinMode(BUZZER,OUTPUT);//set the pin to output
Read the value
i=analogRead(5); //read from analog pin 5
Print the value in computer：
Serial.println(i); //print the value of “i” through the serial port
Judge the direction of light
if(i<400) //if the light is on the left
for(i=0;i<80;i++) //buzzer rings at an exact frequent
digitalWrite(BUZZER,HIGH);//Pin 16 output a high voltage
delay(1); // delay 1ms
digitalWrite(BUZZER,LOW);//Pin 16 output a low voltage
delay(1); //delay 1ms
for(i=0;i<80;i++) //Buzzer rings at an exact frequent
delay(3); // delay 3ms
delay(3); //delay 3ms
Step 6: Knowing the Hardware
Photosensitive diode is actually a kind of photosensitive resistance, it is very sensitive to the light. Inside the diode is a PN junction，electricity can only flows uni-directionally，thus the changing light changes the electricity in the circuit. It means, the stronger the light is, the less the resistance becomes.
Step 7: Analysis of Circuit
The schematic diagram and sketch diagram：
3. Schematic Diagram of Photosensitive Diode on the Robot
In the circuit, R1 is used to limited the electricity because when the light is very bright, the resistance of D1 and D2 is very small, if there is no R1, the electricity in the circuit will be very big, it is harmful to the battery. If D1 receives lighting, the resistance will be smaller, and the value read by Arduino will be bigger. And if D2 receives lighting, Arduino can read a smaller value.
The priciple is easy to understand, let’s see the circuit below:
4. Partial pressure circuit
The input voltage Vin（in this case is 5V） is connected to two resistances, Vout is the voltage of on R2, use this formula to calculate it：
5. Partial pressure formula
So，if R1 is a resistacne of 10K，R2 is a photosensitive diode. When R2 is in the dark, the resistance will be very big, the output will be nearly equal to Vin(5V). And once the lighting comes, its resistance will be smaller, so the Vout will be smaller at the same time, and just check the formular, you will have a deep impression. By another side, R1 shouldn’t be too little, it should be about 1K~10K, or we can not see a obvious change. Now, try to design your own circiut.
If the schematic diagram is difficult to distinguish：
6. Connect diagram
Step 12: More to Know
This is the circuit of buttons：
7. Schematic diagram of buttons
In the diagram，D1,D2,D3 are general diodes, they are used for protecting Arduino. And for the buttons, if S1is pressed, D4 will be turned on, Arduino pin will get nearly 0V(now D1 can be seen as a conductor and notice the current direction), if S2 is pressed, D5 will be turned on, the current flows through R1>R2>D2>S2>GND, there will be nearly no current flows through R3, and now Arduino reads the voltage between R1 and R2. For S3, I think you can reads yourselves.
To the code we write, we need to detect the value continuously，and once we detected the pressing, do not believe that the button is pressed, just wait for about 100ms to ignore the error judgment and detect again, the second time we can be sure it is pressed.
8. Sinle button schematic
Open the folder “key”>key.ino, upload the code, and press buttons, see what will happen?
Last Arduino MiniQ 2WD Tutorial: MiniQ 2WD Complete Robot Kit v2.0- Lesson 2(Make some Noise)