Servos don't move in response to x,y,z from adxl345

I have 3 servos on a breadboard which is connected to a 6f22 9v battery, the servos are grounded to the GND pin near 13.

I am using an adxl345 accelerometer with scl and sda connected to a5 and a4 respectively.
the vcc is connected to 5v and GND to GND near 5v.

I have mapped the accelerometer's values to be within 0 to 180.

My issue:
initially only my x axis sensor moved in respone to moving the adxl345.
Now none of them move.

My end goal is to make a servo driven arm (built with icecream sticks) that move in response to an accelerometer.

Code:

#include <Wire.h>
#include <ADXL345.h>
#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;

ADXL345 adxl;

int x,y,z;
int rawX, rawY, rawZ;
int mappedRawX, mappedRawY, mappedRawZ;

void setup(){
  Serial.begin(9600);
  adxl.powerOn();
  servo1.attach(3);
  servo2.attach(5);
  servo3.attach(6);
}

void loop(){
  adxl.readAccel(&x, &y, &z);

  rawX = x-7;
  rawY = y-6;
  rawZ = z+10;

  if (rawX < -255) rawX = -255; else if (rawX > 255) rawX = 255;
  if (rawY < -255) rawY = -255; else if (rawY > 255) rawY = 255;
  if (rawZ < -255) rawZ = 255; else if (rawZ > 255) rawZ = 255;
   
  mappedRawX = map(rawX, -255, 255, 0, 180);
  mappedRawY = map(rawY, -255, 255, 0, 180);
  mappedRawZ = map(rawZ, -255, 255, 0, 180);
  
  servo1.write(mappedRawX);
  delay(20);
  servo2.write(180 - mappedRawY);
  delay(20);
  servo3.write(mappedRawZ);
  delay(20);
  
  Serial.print("MappedRawX:");Serial.print(mappedRawX);
  Serial.print(" MappedRawY:");Serial.print(mappedRawY);
  Serial.print(" MappedRawZ:");Serial.print(mappedRawZ);
  Serial.println();
}

Please Help!

Schematic?

If they are not 9V servos then you should not be using a 9V battery.
Which servos are you using?
You will probably need a 5V 5A supply.

i am using tower pro's 9g micro servo - SG90

You shouldn't be using a smoke alarm battery to power motors.

thank you for replying!
what battery would you recommend for this project?

thanks for the reply!
i'll try to find a 5v battery

Those servos run on 5V.
You should have a 5V power supply rated at 2.5A or more.

thanks for the help!
is my code for the operation of the servos correct?

We can't say because we don't know what the specification is.

It will certainly make the servos move but if it does what you want it to do, I have no way of knowing.

1 Like

Thanks for all the help guys!

If you must have batteries then you could use 4 AA batteries.
If you want the batteries to last longer you could use 4 D batteries

i found this in my local eletronics store and i just connected it to my circuit.
IT WORKS!
thank you so much for your time you guys saved me!

Now have fun!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.