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 reading1 = analogRead (transistorPin1);
 int reading2 = analogRead (transistorPin2);
 int reading3 = analogRead (transistorPin3);
 int reading4 = analogRead (transistorPin4);
 int reading [] = {reading1, reading2, reading3, reading4}; //This is an array for the values:
 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:
     if (reading[i] > maxReading){  
     maxReading = reading[i];
     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.) Project Setup Illustration:
Connection Diagram:

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.

  • 1
    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.
    – 6v6gt
    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
  • 1
    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.
    – 6v6gt
    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

1 Answer 1


In principle, you only need two phototransistors, a left and a right one. The quotient of the values from the ad-convertor (Vleft/Vright) will tell you where to find the light source. If the quotient > 1 then the light source is at the left side, if the quotient < 1 then the light source is at the right side. Most likely there is no lineair relationship between the quotient and the angular position of the light source. This relationship depends on the housing and the positioning of the diodes in their housings. One possibility is to do a calibration beforehand and use a lookup table during operation. The calibration could be done by placing the breadboard with photodiodes on a rotating platform with the light source in front and then record Vleft and Vright at various angles (in 1 degree steps?) of the platform in relation to the light source. If two photoresistors don't give you the required precision and/or range, you can use more photoresistors and use the pair which is best 'looking' in the direction of the light source.

  • The value could also be subtracted, and the sign gives the direction. In any case both light sensors should have the same sensitivity.
    – datenheim
    Commented Jan 1, 2023 at 23:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.