# Detecting an approximate angle/location of a light source using phototransistors

Note: [I am approximately two weeks into learning Arduino and hence have little experience with utilizing Phototransistors.]

My plan revolves around placing four identical Phototransisors which are spread approximately 10cm apart on a breadboard in a V configuration. Each Phototransistor is connected to one of the "analog In" pins respectively (I am using the Arduino Uno), with the long leg attached to 5V while the short leg is connected to ground via a 220-ohm resistor. In the center of this setup, I plan to place a servo that will face the direction of the light beam emitted from a flashlight.

(I want this project to be allocated in a dark room with any external lighting calibrated),The idea is to identify the phototransistor which has the highest analog value (closest to the light source). With this information, I want to be able to compute an approximate angle of the light source to then write it on my servo.

You can find the attached datasheets below (All the components are from the Arduino starter kit :) ).

[Datasheet for the Phototransistor]

[Datasheet for the Servo motor]

[Datasheet for the 220 ohm Resistor]

Here is what I have so far for the code:

``````#include <Servo.h>
Servo myServo;
const int transistorPin1 = A0;
const int transistorPin2 = A1;
const int transistorPin3 = A2;
const int transistorPin4 = A3;
void setup() {
myServo.attach (10);
Serial.begin (9600);
}
void loop() {
int maxReading = reading [0], maxSensor = 1; // I am setting the maximum reading and the Phototransistor that corresponds to it:
for (int i = 1; i <= 3; i++){ //Setting this for the other three phototransistors:
maxSensor = i + 1;
}
}
}
``````

So my question is: What is the most efficient way to compute the angle of the lighsource? And is my method even relevant for this application?

Thank you for taking you time to read this! Your support will be very much appreciated!

Edit 1:

Explanation of my hardware setup:

This project is allocated on two connected breadboards which means that there are no moving parts. The only moving component is the one single servo that is fixed in position.

The phototransistors, (as mentioned above), are placed in a V configuration which too are stationary. The servo is allocated behind the phototransistors and can only rotate 180 degrees. (You can see the attached illustrations below, I apologize in advance for any technical errors or my horrible drawings.)

As you can visualize on the first image, I do not plan on a complete 360 degree rotation which probably complicates the issue further. Any suggestions on how I can code this would be greatly appreciated! Once again thank you for your time.

• See this instructables.com/Arduino-Sunflower-an-Electronic-Sundancer or google for Arduino Sunflower or solar tracker for more examples. I believe the most effective ones have the light sensors mounted on the panel which the servos moves. The servos continues to position the panel until all photosensors deliver exactly the same measurement. Can you say something about the physical construction of your project. Commented Dec 25, 2022 at 10:53
• Thank you for your reply, the link you sent me was quite helpful however I have noticed that the project mentioned utilizes two servo motors for a full 360 degrees rotation (covering all three x, y, and z coordinates). I, unfortunately, have to resort to utilizing one servo motor which only provides me with 180 degrees. Another issue I have noticed is that the light sensors are not stationary and are rotated by a robot "chassis". Due to my project being entirely based on breadboards the method used will not work for my application. I hope my edit will clarify the idea a little bit better. Commented Dec 25, 2022 at 22:27
• Your efforts are well thought out, but make use of others before you, e.g., arduino.stackexchange.com/questions/36597/… (yes, I know they are LDRs, but the task is similar). Also, make some effort to calibrate your phototransistors, even if it is just to check the levels when a light source is 6 inches away - they need to be as identical as possible and fixed 220s may not be good enough. Finally, remember you only have 1 ADC and are switching channels, check that you are obeying speed limits, so to speak. // end two cents.
– DrG
Commented Dec 25, 2022 at 22:46
• Just to get started, I'd probably look at the photo transistor which gives the strongest light value and use that as the current starting point (0,60,120 or 180 degrees assuming that is how the transistors have been distributed around the servo) then look at the adjacent transistors to work out how much, and in which direction, to bias the result away from the starting point. The result is sent to the servo. Repeat every X ms. Your hardware configuration is unusual in that all the sensors are static. Most tracking type projects move the sensors towards the light source. Commented Dec 26, 2022 at 0:23
• This really helped! It took me a while but I managed to get it to kind of work. The servo is jittery but I am satisfied. I will attach a youtube URL later today to show my results. Thank you guys for replying! Commented Dec 26, 2022 at 11:11