Accelerometer problem with servo

Hello,
i’m making a gyroscopic stabiliser for a school project and I’m having a problem.
I’m using an Adafruit ADXL335 accelerometer and a Hitec HS-485HB servo. I started programming and I can perfectly get the values from the accelerometer that I also remapped from 0 to 180.

The problem occurs when I try to give the servo the value the accelerometer gives me, for example when I add servo1.write(degx); with degx being the value in degrees, the values go from 90, 89, 90, 90, … (when everything works fine) to 291 90 246 290 90 256 for example as soon as I add this line telling the servo to take the value.

I really don’t get it why it’s doing that.
Here’s the code:

#include <Servo.h>

int xpin = A1; // X Axis

Servo servo1; // Declares servo 1

int axex; // Raw values X

int degx; // Remapped X values

void setup()
{
servo1.attach(9); // Connects servo 1 to port 9
servo1.write(degx);
Serial.begin(9600); // Start com
}

void loop()
{
axex = analogRead(xpin); // Gives axex its value
degx = map(axex, 269, 408, 0, 180);
//servo1.write(degx); // PROBLEM

Serial.print(degx);
Serial.print("\t");
delay(20);

}

Here are some pictures:


If you guys could help me it would be great :open_mouth:
Thanks,
Alex

Most servos can do a maximum of 180 degrees. Does yours do 360?

Nope, it's 180 degrees

Are you powering the servo from the Arduino 5v pin? That almost always causes crazy problems - and may damage your Arduino. Give the servo a separate power supply and connect the servo GND to the Arduino GND.

...R

Print out your accelerometer reading with xdeg so you can see what's causing the over 200 values. For this setup, did you actually have a plate to place your accelerometer on or are you just tilting your breadboard to simulate tilt?

Yes i'm powering both the accelerometer and the servo with the arduino 5v pin, i'll try to power it externally.

liudr > What do you mean exactly? The values of degx which are sent to the servo are normal when I dont add the servo1.write(degx); line and when I add it, the values of degx are like 291 90 246 290 90 256.

And I just tilt my breadboard for now. :-)

And also, what about the accelerometer, is it okay to power it using the 5v pin?

Dewyy: And also, what about the accelerometer, is it okay to power it using the 5v pin?

Probably. How much current does it require?

...R

Exactly 5V :)

Would powering the arduino with a 9V battery and the accelerometer on the 5V pin, and powering the servos with a 9V battery connected to a 5V Voltage regulator do the job? It's supposed to be a portable system though. :S

Dewyy: Exactly 5V

I asked you how much current (amps) it requires. Not what voltage. I had assumed it needed 5v or you would not be silly enough to connect it to the 5v pin.

The small PP3 9v batteries are quite useless. They might get things going for 20 or 30 seconds. Use battery holders with 6 AA NiMh batteries if you need simple portable power. And give the servos their own power supply.

...R

Hi, I think your problem could be caused due to the map, function.

degx = map(axex, 269, 408, 0, 180);

if axex is less than 269, the degx will be less than 0, if axex is more than 408 then degx will be greater than 180. These values do no compute in the servowrite function.

The map function simply works out a y=mx +c equation and churns through the maths with each new value.

You may have to use the constrain function on axex before using it in a map function.

Tom.... :)

Robin2: I asked you how much current (amps) it requires. Not what voltage. I had assumed it needed 5v or you would not be silly enough to connect it to the 5v pin.

The small PP3 9v batteries are quite useless. They might get things going for 20 or 30 seconds. Use battery holders with 6 AA NiMh batteries if you need simple portable power. And give the servos their own power supply.

...R

Oh sorry, i'm really new to arduino and electronics in general. Well, the datasheet of the accelerometer says it requires - 350 μA.

Okay so basically I'd have two 6*AA battery stacks, with one powering the arduino through i guess the barrel plug and another battery stack powering the servos with a regulator for example?

TomGeorge: Hi, I think your problem could be caused due to the map, function.

degx = map(axex, 269, 408, 0, 180);

if axex is less than 269, the degx will be less than 0, if axex is more than 408 then degx will be greater than 180. These values do no compute in the servowrite function.

The map function simply works out a y=mx +c equation and churns through the maths with each new value.

You may have to use the constrain function on axex before using it in a map function.

Tom.... :)

I thought about that but I monitored the values of degx and they all stay between 0 and 180 at any time. It sometimes went to -1 or -2 instead of stopping at 0 somehow so I just added abs() like this:

finalx = abs(degx); // Makes degx values absolute

( It works perfectly but only when I dont put the servo1.write command of course. )

Dewyy: Oh sorry, i'm really new to arduino and electronics in general. Well, the datasheet of the accelerometer says it requires - 350 μA.

Okay so basically I'd have two 6*AA battery stacks, with one powering the arduino through i guess the barrel plug and another battery stack powering the servos with a regulator for example?

I suspected the current required by the accelerometer would be very low. There will be no problem powering that from the 5V pin.

A pair of battery packs should be reliable and simple and not too expensive. But it would probably make more sense to use a pack of 4 to power the servos. That's the nice thing about using AA batteries - it is easy to select the number to match the required voltage.

...R

Okay great! Would I have to use a regulator though? The HS485HB specifications say "Operating Voltage: 4.8-6.0 Volts" so i guess I wouldn't even have to use a regulator right?

Here's how it would look is it all good? :open_mouth:

If you just use 4 AA cells you won't need a regulator for the servos.

The wiring picture for the servos looks right but I never trust those pictures. A photo of a crude pencil drawing will be much better. I am not able to comment on the wiring for the accelerometer.

...R

Robin2: If you just use 4 AA cells you won't need a regulator for the servos.

The wiring picture for the servos looks right but I never trust those pictures. A photo of a crude pencil drawing will be much better. I am not able to comment on the wiring for the accelerometer.

...R

Okay thanks :p I'll try to draw a schematic of the system.