Go Down

Topic: Accelerometer and Transistor (Read 479 times) previous topic - next topic

redcomet782

Dec 06, 2012, 09:19 am Last Edit: Dec 06, 2012, 09:21 am by Coding Badly Reason: 1
Here is my program, I want to

1) The microcontroller, Arduino UNO R3 sends 3.3V to the sensor, Accelerometer, MMA7361

2) The sensor turns on and send signal to the microcontroller.

3) When I move the sensor and if the voltage becomes more than 2V, the microcontroller sends 1.5V to the Transistor. Otherwise send 0V.

Code: [Select]
int outputPin1 = 3;   // Pin D3
int outputPin2 = 5;  // Pin D5
int analogPin = 0;  // Input Pin A0

void setup()
{
 pinMode (outputPin1, OUTPUT);
 pinMode (outputPin2, OUTPUT);
 Serial.begin(9600);
}

void loop()
{
   int volts = 170; // send signals of 3.3v
   analogWrite(outputPin1, volts);  // to Accelerometer

   int reading = analogRead(analogPin);  //  receive signal
   delay(1000);    // delay 5sec.
   
   if (volts >= 100)    // Input voltage is more than 2.0V
   {
   int volts = 77; // send signals of 1.5V
   analogWrite(outputPin2, volts);  // to Transistor
   }
   else
     {
     int volts = 0;
     analogWrite(outputPin2, volts);  // to Transistor
     }
}


When I measure with DM meter, it's keep showing 1.5V even it doesn't change the position of the sensor.
I guess there's some problem in the if, else.


Moderator edit: [code] [/code] tags added.

AWOL

Code: [Select]
int volts = 170; // send signals of 3.3v
    analogWrite(outputPin1, volts); 

Is turning an accelerometer on and off at 490Hz a good idea?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Grumpy_Mike


Code: [Select]
int volts = 170; // send signals of 3.3v
    analogWrite(outputPin1, volts); 

Is turning an accelerometer on and off at 490Hz a good idea?

Especially as you will be feeding 5V into a 3V3 device. You might have just fried it.
Dispute the name analogWrite noes not produce an analogue voltage.

redcomet782

#3
Dec 07, 2012, 12:25 pm Last Edit: Dec 07, 2012, 12:31 pm by redcomet782 Reason: 1
I just revised my code like this:
-----------------------------------
Code: [Select]

int outputPin1 = 3; // Pin D3
int outputPin2 = 5; // Pin D5
int analogPin = 0; // Input Pin A0

void setup()
{
   pinMode (outputPin1, OUTPUT);
   pinMode (outputPin2, OUTPUT);
   analogWrite(outputPin1, 170);  // send signals of 3.3v to Accelerometer
}

void loop()    
{
   delay(1000);    // delay 1sec.

   if (analogRead(analogPin) >= 100)    // Input voltage is more than 2.0V
   {
       analogWrite(outputPin2, 77);  // send signals of 1.5V to Transistor
   }
   else
   {
       analogWrite(outputPin2, 0);  // 0v to Transistor
   }
}


-----------------------------------------

It looks like Accelerometer is working properly which sends different voltage when I change the position, BUT when I measured the voltage of outputPin2, Pin D5, it's keep showing 1.5V even I change the direction of the sensor that has to show 0V.

Does it mean the microcontroller doesn't receive the signal from the accelerometer properly or some part is missing in the code?

AWOL

#4
Dec 07, 2012, 12:27 pm Last Edit: Dec 07, 2012, 12:45 pm by AWOL Reason: 1
Quote
analogWrite(outputPin1, 170);  // send signals of 3.3v to Accelerometer

Does it, really?
Or does it just send a 66% duty-cycle 5V, 490Hz signal?

If you want to power a 3.3V device, it is much simpler to use the 3.3V output provided on the board.

Please use [font=Verdana][[/font]code][font=Verdana] [[/font]/code] tags when posting code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up