# Accelerometer voltage to simulate potentiometer

Hey guys,

Long time reader, but first post... i need some help. I have a problem that i cant seem to get my head around.

I want to interface with a device that has a joystick (2 potentiometers)

the max reading of the variable resistor is 5.0v, and the min reading is 0v

if i think of the joysticks position as a x,y graph the origin (0,0) is (2.5v, 2.5v)

i want to substitute the joystick with an accelerometer how would i (in arduino sketch terms) map the output of the accelerometer to the original output of the joystick? I think (0,0) is about (1.65v,1.65v) for the accelerometer. (ill know for sure when my new multimeter comes in the mail, that number is based on a data sheet for now)

would i set a conditional, reading the input of the acc. and based on the reading set an output on the arduino to the correct voltage? or would i do it with hardware (capacitors and such), if the last, then i definitely need some of your help...

how would i (in arduino sketch terms) map the output of the accelerometer

Well, you could use the handy "map" function http://arduino.cc/en/Reference/Map

map(acc_val, 0, 3.2, 0, 5);

Where 0 - 3.2 is the acc. range and 0-5 is the pot?

the pin will know its a voltage?

There are no real analog outputs on Arduino.

Digital pins are either 0V or 5V never in between.

The “Analog” pins are PWM pins.

You can put a smoothing filter on an analog pin to make it look like a real analog voltage. But this can not always be used as a real analog voltage.

So you would need to map your values to the 0 - 255 PWM range for the analog pins and then use analogWrite() to output a value.

http://arduino.cc/en/Reference/AnalogWrite

ok.

I guess my question is how would i do it? So, if analog inputs dont work i can set the acc. to digitalRead?

i have two outputs that need values from 0v-5v. 2.5v is neutral. I have a 3 axis accelerometer. (5dof -ImU)

I can get the vout of the x,y,z out of the accelerometer and print a value similar to: 1.65V / 5V * 1023 = 337.59

where do i go from there?

News to me. Where did you get that from?

"map" won't work on floating point values, but then voltages don't get into your Arduino as floating-point, they come in as integers from the A/D converter.

ok.

The question remains...how is it done?

OK, assume your accelerometer is a ratiometric device, with a supply voltage of 3.3V, The 0g point will be at 1.65V. If your Arduino has a Aref of 5V, the 0g point will read as roughly 1.65/1024 * 5 = 337. I don't know what the sensitivity of the device is, let's say 800mV/g. So, -1g will be 1.65-.8 = 0.85V, which your converter will see as about 174, and so +1g will be around 500.

Plug these values into "map", and off you go.

Right right... i see

I'll have to check the data sheet for the sensitivity, but you might be spot on.

I really thank you for your help!

That's a great starting point!

ok the acc. outputs a range of 3700 and 5500 when i print the raw data... am i on the right track with this code? I dont know how to translate what its giving me into voltage...

`````` valx = (Xraw);            // reads the value of the accelerometer (value between 3700 and 6300)
valx = map(valx, 3700, 6300, 0, 255);     // scale it to use it with the interface (value between 0 and 255)
valy = (Yraw);            // reads the value of the accelerometer (value between 3700 and 6300)
valy = map(valy, 3700, 6300, 0, 255);     // scale it to use it with the interface (value between 0 and 255)

Serial.print(valx);
Serial.print(",");
Serial.print(valy);
Serial.println("");

// Delay for number of measurments per second needed.

delay(15);
``````

Where did the Xraw and Yraw values come from? The analogRead function didn't return values in that range.

here is my whole sketch...so far (using memsic 2125)

``````int valx;
int valy;

int xPin = 10;
int yPin = 9;
int Xraw, Yraw;

void setup(){
Serial.begin(9600);
pinMode(yPin, INPUT);
pinMode(xPin, INPUT);
}

void loop(){

// Due to erroneous output of pulseIn();
// when x and y are calculated together
// pulseIn(); must be used twice.

Xraw = pulseIn (xPin, HIGH);
Xraw = pulseIn (xPin, HIGH);
Yraw = pulseIn (yPin, HIGH);
Yraw = pulseIn (yPin, HIGH);

valx = (Xraw);            // reads the value of the accelerometer (value between 3000 and 5500)
valx = map(valx, 3700, 6300, 0, 255);     // scale it to use it with the interface (value between 0 and 255)
valy = (Yraw);            // reads the value of the accelerometer (value between 3000 and 5500)
valy = map(valy, 3700, 6300, 0, 255);     // scale it to use it with the interface (value between 0 and 255)

Serial.print(valx);
Serial.print(",");
Serial.print(valy);
Serial.println("");

// Delay for number of measurments per second needed.

delay(15);

}
``````
``````    valx = (Xraw);            // reads the value of the accelerometer (value between 3000 and 5500)
valx = map(valx, 3700, 6300, 0, 255);     // scale it to use it with the interface (value between 0 and 255)
``````

In the first statement, the parentheses are not necessary. Since you then overwrite the value in valx anyway, those two statements can be combined

`  valx = map(Xraw, 3700, 6300, 0, 255);     // scale it to use it with the interface (value between 0 and 255)`

If, as your comment implies, pulseIn returns a value between 3000 and 5500, why are you using 3700 and 6300 as the from range in the map function call?

the comment is a typo...its really 3700-6300

Wait a minute.

If you substitute an accelerometer for a potentiometer, you are an integral squared away from the same thing.

The joystick is measuring position.

The accelerometer is measuring acceleration

You can calculate speed from acceleration, and you can calculate position from speed, but you have to do that.

yes, but the interface the joystick is attached to is reading the voltage its supplied (im replacing this), so i just need translate the acc. rotation around a given axis (x or y) into the correct voltage that the interface can understand.

my main confusion is supplying the interface with the right voltage at any given moment…I’m having a hard time understanding this…

I dont see how the map function does this when you can only get 0 or 5v in a square wave; in varying frequencies…

neutral: 2.5v, 2.5v
full left: 0v,2.5v
full right: 5v, 2.5v
full up: 2.5v, 0v
full down 2.5v, 5v

sorry for my stupidity, but im trying to get this… what am i missing?

I dont see how the map function does this when you can only get 0 or 5v in a square wave; in varying frequencies

PWM frequency is fixed; it is the duty cycle that varies.

You need a low-pass filter to average the PWM into a DC level.

Sorry for the delay, i've been off this project for a while...coming back to it...

I've been reading the forum and i need to be clear on something. I've taken apart a crappy GWS 4ch transmitter (GWT-4a V1.0) that used to go to my electric helicopter. (see pictures)

So, its been brought to my attention that the board may be reading the resistance of the joystickand not the Vout of each Pot. How can i figure this out, and finally make my project work

Again, substituting the joystic with a memsic 2125 accelerometer...

thanks again guys, sorry for the annoyance, but im still learning