Go Down

### Topic: Accelerometer voltage to simulate potentiometer (Read 4365 times)previous topic - next topic

#### Keith Brown

##### Mar 05, 2010, 03:07 pm
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...

Keith

#### Groove

#1
##### Mar 05, 2010, 03:15 pm
Quote
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

#### Keith Brown

#2
##### Mar 05, 2010, 03:22 pm
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?

#### MikMo

#3
##### Mar 05, 2010, 03:48 pm
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

#### Keith Brown

#4
##### Mar 05, 2010, 04:14 pm
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?

#### Groove

#5
##### Mar 05, 2010, 04:34 pm
Quote

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.

#### Keith Brown

#6
##### Mar 05, 2010, 04:38 pmLast Edit: Mar 05, 2010, 04:39 pm by rcpilotp51 Reason: 1
ok.

The question remains...how is it done?

#### Groove

#7
##### Mar 05, 2010, 04:45 pm
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.

#### Keith Brown

#8
##### Mar 05, 2010, 04:53 pm
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!

#### Keith Brown

#9
##### Mar 05, 2010, 05:59 pmLast Edit: Mar 05, 2010, 06:02 pm by rcpilotp51 Reason: 1
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...

Code: [Select]
` 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);`

#### PaulS

#10
##### Mar 05, 2010, 06:05 pm
Where did the Xraw and Yraw values come from? The analogRead function didn't return values in that range.

#### Keith Brown

#11
##### Mar 05, 2010, 06:07 pmLast Edit: Mar 05, 2010, 06:11 pm by rcpilotp51 Reason: 1
here is my whole sketch...so far  (using memsic 2125)

Code: [Select]
`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);   } `

#### PaulS

#12
##### Mar 05, 2010, 06:12 pm
Code: [Select]
`    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

Code: [Select]
`   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?

#### Keith Brown

#13
##### Mar 05, 2010, 06:14 pm
the comment is a typo...its really 3700-6300

#### brtech

#14
##### Mar 05, 2010, 06:18 pm
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.

Go Up

Please enter a valid email to subscribe