Photo sensor and servo motor

hi all,

I am trying to create a device that is controlled by servo motors and reads input from photosensors.

There are two photosensors A and B

There is one Servo (Continuous rotation) Motor

There are 4 Positions for the motor to go to (N,W,S,E)

I am having troubles writing this code. SO far I have been able to tell the motor to turn from the starting position (North) when Photosensor A is shadowed to go 90degree clockwise to East. Then if Photosensor A is triggered again it will no longer move Clockwise. If Photosensor B is triggered it will turn 90degree counterclockwise.

My problem is expanding this code any further than this basic code. Essentially, Sensor A tells the motor to turn CLOCKWISE and Sensor B tells it to go CounterCLockwise but it can never move beyond a 360degree turn. So, for instance, if the motor is in South position, I want it to turn clockwise or counterclockwise depending on the sensor triggered. How can I acheive this?

ANy help would be appreciated


Below is what I have so far:

int averageA;
int averageB;
int PointACovered;
int PointBCovered;
int previousA;
int previousB;
int pos=1;

void setup(){
for(int i=0;i<10;i++) //Calibrate the first sensor
for(int j=0;j<10;j++) //Calibrate the second sensor
Serial.println(“System Ready”);
void loop(){
int A = analogRead(5);
int B = analogRead(4);

if (A<(averageA-75)) //if point A is Covered
if (pos==1) // Initial Start Point
{for(int a=0; a<200; a++){
digitalWrite(2,HIGH); // turn on servomotor to 180 degrees
delayMicroseconds(1600); // length of pulse sets the motor position
digitalWrite(2,LOW); // turn off motor

if (B<(averageB-75)) //if point B is Covered
if (pos==2)
{for(int a=0; a<100; a++){
digitalWrite(2,HIGH); // turn on servomotor to 180 degrees
delayMicroseconds (800); // length of pulse sets the motor position
digitalWrite(2,LOW); // turn off motor



A continuous rotation servo does not provide feedback on position. Could you say more about how you intend to determine if the vehicle (or servo) is facing N,E,S or W ?

Do you want the photocells determine this? If so how do they know about direction? If not what is their purpose?

I calculated the amount of Pulse required to send the Servo Motor 90degrees clockwise and 90degrees counterclockwise. So, By sending the motor 270degrees clockwise the motor would be in the West Position.

I only want the photosensors to trigger which direction the Servo can go, clockwise or counterclockwise at intervals of 90 degrees but the servo can not go beyond 360degrees if one sensor is triggered continuously.

hope that makes more sense…


I doubt that will be consistent enough over time, have you actually tested pulsing the servo to send it 90 degrees clockwise and 90 degrees counterclockwise repeatedly over many minutes?

The distance a servo moves is not proportional to the number of pulses. This is because the servo moves less distance for the initial pulses when its starting up than when its moving at full speed. This will also vary with the load on the servo and voltage that is providing servo power.

Even if these errors in your servo were relatively small over a few rotations, they would be enough without any mechanical feedback to generate significant error over time.

If you haven't tried a test, you may want to do so using a simpler sketch without the photocells. Just cycle the number of pulses you calculated to send the servo to the four directions with a pause of a second between each direction. See if over time the servo arm 'creeps' away from the required directions. Try it alternating with 90 degree and 180 degree movements. And if you can, try to simulate the actual load you will have on the servo. If its steering then this load will be fluctuating so try to simulate that.

If your servo is consistent enough for your needs then great. If not then you could add some physical feedback to indicate the four stopping points or perhaps use a conventional servo that has physical feedback built-in and gear it up so when the servo turns 180 degrees an output gear turns 360 degrees.

Have Fun