Only pin 2 working

I'm using this code where the degree of tilt from the mpu causes two servos to move

#include <Wire.h>

#include <MPU6050.h>

#include <Servo.h>   

Servo sg90X;          
Servo sg90Y;
int servoX_pin = 8;
int servoY_pin = 2;


MPU6050 sensor ;

int16_t ax, ay, az ;

int16_t gx, gy, gz ;

void setup ( )

{ 

sg90X.attach ( servoX_pin );
sg90Y.attach ( servoY_pin );

Wire.begin ( );

Serial.begin  (9600); 

Serial.println  ( "Initializing the sensor" ); 

sensor.initialize ( ); 

Serial.println (sensor.testConnection ( ) ? "Successfully Connected" : "Connection failed"); 

delay (10); 

Serial.println ( "Taking Values from the sensor" );

delay (1000);

}




void loop ( ) 

{ 

sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);


ax = map (ax, 0,1024 , 0, 10) ;
ay = map (ay, 0,1024 , 0, 10) ;

Serial.println (ax);
Serial.println (ay);
sg90X.write (ax); 
sg90Y.write (ay);

delay (10);

}

and so I need to connect one servo motor to pin 2 on the arduino while the other servo to pin 8. however only pin 2 seems to work and i tried changing the pin combination to 3,8 (didnt work) servo 1 to pin 8 and servo 2 to pin 2 (only pin 2 works)

can somebody tell me why?

the wiring: Imgur: The magic of the Internet

Strip the code down to the bare minimum with one servo on pin 8 and see if that works:

#include <Servo.h> 

Servo myservo;

void setup() 
{ 
  myservo.attach(8);
  myservo.write(90);  // set servo to mid-point
} 

void loop() {} 

What board are you using ?

Hello illumin8
I see not clear on this points:

  • ax and ay: do they have only possible values? I think also negativ are possible as defined as int16_t but the map() will only check 0 to 1024

As I understood the ay on pin 2 is working fine. Can you try that way to steer both servo for test with ay.

sg90X.write (ay); 
sg90Y.write (ay);

What Arduino board is been used?
Can you provide a hand drawn wiring diagramm from your project?
BR Markus

Hi, @illumin8
Welcome to the forum.

Can I suggest you leave your code for the moment and just run the SERVO SWEEP code example in the IDE.
This will make it possible to test your servos and the pins you are using?

Can you please post a circuit diagram of your project, including power supplies, use a pen(cil) and paper and show an image of it would be fine.
Please do not use a Fritzy picture

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

im using the arduino uno

yup ill try what you said right now
i have posted the picture of the wiring (imgur cuz im a new user)

ill be honest, that another problem that ive been trying to solve
i want the servo to only move 10 degrees in either direction from when the gear is in line with the actual motor and nothing i found online is consistent about setting a range
i have uploaded a wiring diagram to imgur with the link in the original post
the board is arduino uno

i have just tryed changing both to ay and the problem still exists

i just tried the code and this allows the servo connected to pin 8 to work
on a sidenote, it says set servo to midpoint but the angle is more tilted to the right (but the code helps the servo at pin 8 to move so thats fine ig)

Hi,

What still exists, does pin 2 or 3 or 8 work with just the sweep code?

Tom... :smiley: :+1: :coffee: :australia:

sry mb it was a bad drawing i meant to write 8 instead of 3 but the same problem still exists
only the servo connected to 2 would work but with the sweep program works for both servos altho i havnt tried doing it with both servos at the same time

Hi,
Okay so the pin 2 and 3 do work?
Just not with your code with the IMU am I correct?
You need to use the IDE monitor and Serial.begin, Serial.print to help see what your variables are doing.

Tom... :smiley: :+1: :coffee: :australia:

yes, individually they work
but not with the code i put originally which doesnt make sense (is my coding wrong?)

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