I’m designing an arduino robot that’s objective is to find and drive towards a light using a photo resistor mounted on a servo. I want to write my program with PID control to sweep the photo-resistor while moving.

Currently my logic flow is:

  • powerMotors(0)
  • Continuously sweep servo 180
  • Continuously collect photoresistor
  • If photoresitor>photoresistorMax
  • photoresistorMax = photoresistor
  • Angle to light = servo position
  • Error = 90 - angle to light
  • powerMotors(Error*Kp)

So at the moment it is adjusted with P control. It wobbles and does not always make it precisely. I believe a major problem with it is that the sampling time essentially is limited by the time to sweep 180°.

Is there any adjustments you could recommend to improve sampling time and overall increase accuracy of my system?


  • $\begingroup$ Hi Nick, Welcome to Robotics Stack Exchange. Reading this problem at a high level it seems to be a relatively generic question about how to tune a PID which has been asked and there are whole courses on. And for any of the other details there's not enough encapsulating information to understand your problem enough to help you find your solution. A good way to think about it is that someone needs enough information to reproduce your problem with just what's described in the question to help you. You can edit your question to add more details but otherwise I'd likely recommend this for closing a $\endgroup$
    – Tully
    Commented Nov 12, 2021 at 6:38

2 Answers 2


To improve sampling time, you can usually avoid the full $180^o$ sweep, resorting to it only when the bot has no idea of where the light is. Otherwise the world has continuity you can use: the light is probably near where it was the last time you located it, and it's probably about as bright as it was then. So you can probably make a pretty good guess where to look.

For instance, if the bot just turned a little to the right, and the sensed light dropped off, the servo could look a little to the left and see if the light gets brighter again. It can stop the scan when it looks bright enough -- no need to hunt past the max every time just to make sure.

With all the "probably" and "assume" and "guess" of this approach, it will pay to be a little conservative about how to tell when it's confused. One sign is when you expect the light to be at one side but when you turn in that direction the detected level doesn't rise again. Even then, it could be smart about whether to turn toward full-left or full-right to begin the scan, and maybe even stop early if it sees the light bright enough again.

If it's necessary to cast it in terms of PID, I'd have to think about this harder.


I don't have full a solution for your problem, but I expect that this will improve performance...

I'll suggest that you decrease speed as the Absolute value of the Error increases.

something like...

speed = maxSpeed - abs(Error)

you would also need to ensure that speed never goes to zero, something like this...

speed = max(speed,minSpeed)


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.