# sel balancing single wheel code

I am currently experimenting with an arduino uno with a gyro breakout board l3g4200d. I have been looking everywhere for code that would work as a starting point to play around with. Does anyone have any suggestions specifically a +/- voltage to send to the dc motor to correct balancing angles. Any help would be greatly appreciated

Hello fozz,

I think that the first task for you is understanding how to interface with the gyro, seems that fortunately guys were doing it before http://arduino.cc/forum/index.php?topic=95022.0 Sparkfun sell it, take a look there for code examples etc.

The reaction to the gyro readings is really dependent on your setup, the mechanical part, weights and everything that interacts with the equilibrium.So there's no way to tell how much voltage you need. Before discovering how much voltage you must decide what you should do to "correct" forces and keep equilibrium. If you can detail how this all is working maybe we can try clues/suggestions, maybe some pictures.

Good luck

well I am just starting with a small 5v motor to see if the balancing code would work, I have the gyro working using the code below
I plan on using the balancing code through a motor controller to step up the voltage to 24 volts at up to 40amps but don"t want to start up that part of the program until I have some working code to work with. I was looking for a clean single motor setup with out dead man switches or any other extras yet. The gyro is L3G4200D mounted in a breadboard using the wiring diagram listed here (bildr L3G4200D Tripple Axis Gyroscope + Arduino - bildr)
//Arduino 1.0+ only

#include <Wire.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24

int x;
int y;
int z;

void setup(){

Wire.begin();
Serial.begin(9600);

Serial.println(“starting up L3G4200D”);
setupL3G4200D(2000); // Configure L3G4200 - 250, 500 or 2000 deg/sec

delay(1500); //wait for the sensor to be ready
}

void loop(){
getGyroValues(); // This will update x, y, and z with new values

Serial.print(“X:”);
Serial.print(x);

Serial.print(" Y:");
Serial.print(y);

Serial.print(" Z:");
Serial.println(z);

delay(100); //Just here to slow down the serial to make it more readable
}

void getGyroValues(){

x = ((xMSB << 8) | xLSB);

y = ((yMSB << 8) | yLSB);

z = ((zMSB << 8) | zLSB);
}

int setupL3G4200D(int scale){
//From Jim Lindblom of Sparkfun’s code

// Enable x, y, z and turn off power down:

// If you’d like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:

// Configure CTRL_REG3 to generate data ready interrupt on INT2
// No interrupts used on INT1, if you’d like to configure INT1
// or INT2 otherwise, consult the datasheet:

// CTRL_REG4 controls the full-scale range, among other things:

if(scale == 250){
}else if(scale == 500){
}else{
}

// CTRL_REG5 controls high-pass filtering of outputs, use it
// if you’d like:
}

Wire.beginTransmission(deviceAddress); // start transmission to device
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}

int v;
Wire.endTransmission();

while(!Wire.available()) {
// waiting
}