Pages: [1] 2   Go Down
Author Topic: Accelerometer voltage to simulate potentiometer  (Read 2232 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...

thanks for your time
Keith
Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Per Arduino ad Astra

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 5
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?



Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So, if analog inputs dont work i can set the acc. to digitalRead
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.
Logged

Per Arduino ad Astra

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok.

The question remains...how is it done?

« Last Edit: March 05, 2010, 10:39:12 am by rcpilotp51 » Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Per Arduino ad Astra

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
« Last Edit: March 05, 2010, 12:02:55 pm by rcpilotp51 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46291
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);

   }
 
« Last Edit: March 05, 2010, 12:11:40 pm by rcpilotp51 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46291
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   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:
  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?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 139
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Pages: [1] 2   Go Up
Jump to: