Servo/Photocell Project help

First off, I'm new here, so I apologize in advance if this is the wrong place to post this.

Secondly, I'm also almost completely new to arduino programming, so in your explanations, please try and talk as if I am a beginner.

I'm trying to get a servo to turn in to a certain spot depending on when a light source points to one of three photocells. (First photocell to the left, Second photocell to the center, Third photocell to the right.)

I used a schematic and code from this one site

to get it to move the servo, but only with one photocell, and it's when the photocell is lacking light that the servo will move, I'd like the opposite.

First thing I wanted to try was see if I could get it to work with two photocells, so I plugged in another photocell+resistor combo and put one of the photocell legs to Analog Pin 2. I added a section into the void loop code to let the board pick up analog pin 2 as well.

Here is the new code:

#include <Servo.h>

Servo myservo;
int val;

void setup()
{
myservo.attach(12);
}
void loop()
{
val = analogRead(0);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(15);

val = analogRead(2);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(15);
}

While it does work, the servo starts shaking really badly, and I'm not sure if it's a power problem, a cheap servo problem, or if I did something incorrectly. The servo is one that came with my kit, and it says "VILROS MicroServe 9g SG90" on it. I know absolutely nothing about servos, but I think the 90 means it can only turn 90 degrees?

Anyways, my questions are:

  1. How can I get it to stop shaking?
  2. Am I doing the right thing by adding another photocell, or is there another way to do it?
  3. How can I get it to move the servo to a certain spot if there's a bright light source detected on it?

I would greatly appreciate any help at all. :slight_smile:

According to this datasheet it's 180 degrees, 90 each side of centre.

It may well be a power problem, if you are powering the servos from the Arduino 5V which is not a good idea. You need to give them their own power, hooking the grounds together as shown attached.

PS.... please don't link to code. Put it in the post (in code tags) or attach it by following the options link below the typing area.

servo power.png

I wasn't sure how to attach photos, when I hit edit the option wasn't there , sorry! And I think you misunderstood, I'd like all 3 photocells to trigger 1 servo to move to different angles, not 1 servo for each photocell.

Well it should still have its own power, and not be powered from Arduino... many servo problems (like random shaking) go away when they get decent power, so it's always the first thing to correct. It might not fix the problem, of course, but it must be eliminated as a potential problem. It's not good practice to use the Arduino as a power supply....

Well thanks for that, that's something new I learned today. I attached it to an old 9v battery that's kinda old, and it's shaking a lot less. Can someone explain what this part of the code means though

val = map(val, 0, 1023, 0, 179);
myservo.write(val);

Yep... that says read in the value of val, which will be between 0 and 1023. Then map that value to 0 as an output if the input is 0, and 1023 input to 179 output, and linearise inbetween. (So, 512 in would be about 90 out, eg) Then put that new value back into the variable val, and write that to the servo.

Most likely, the input val of 0-1023 is an analogRead, like from a potentiometer; the output 0-179 is clearly the degrees for a servo.

It might make more intuitive sense if there were two variable names like valIn and valOut:

valOut = map(valIn, 0, 1023, 0, 179);

Reference page

I attached it to an old 9v battery that's kinda old, and it's shaking a lot less.

If "it" is the servo, then you need to be careful. Most hobby servos are rated for voltages from 4.8v to 6v, not 9v.