Pages: [1]   Go Down
Author Topic: Accelerometer and Transistor  (Read 401 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: December 06, 2012, 03:21:45 am by Coding Badly » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25849
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int volts = 170; // send signals of 3.3v
    analogWrite(outputPin1, volts); 
Is turning an accelerometer on and off at 490Hz a good idea?
Logged

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just revised my code like this:
-----------------------------------
Code:
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?


* 写真.JPG (1725.79 KB, 2592x1936 - viewed 8 times.)

* image.jpeg (1808 KB, 2592x1936 - viewed 8 times.)
« Last Edit: December 07, 2012, 06:31:43 am by redcomet782 » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25849
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 [code] [/code] tags when posting code.
« Last Edit: December 07, 2012, 06:45:18 am by AWOL » Logged

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your meters are not reading the right value because you are trying to measure rapidly changing voltages, meters don't read those correctly.

You can not use the analogue write to set a voltage on the pin, it just produces a 5V square wave.
I did say this before but I think you did not understand. DO NOT DO THIS.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Not only can you not send 3.3V to the accelerometer using analogWrite, you can't send 1.5V to the transistor either.

To power the accelerometer, just use the 3.3V supply pin on the Arduino - as has already been said.

As for the transistor, is it a bipolar transistor, a darlington transistor, or a mosfet? The first 2 of these are current-operated devices, so you should drive them direct from a 5V digital output pin via a series resistor. A mosfet is a voltage-operated device, but most discrete mosfets need more than 1.5V anyway. if you tell us the part number of the transistor and what device it is driving, we can offer advice.

Also, you are comparing the result of analogRead with 100, but the comment says you are testing for more than 2V. 2V on the pin will give you a reading of (2/5 * 1024) = 409.
« Last Edit: December 07, 2012, 03:06:29 pm by dc42 » Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i'm sorry that I didn't notice that there has a 3.3V output on the board.

When I use the 3.3V pin, instead of outPutPin1, Pin D3, how I should edit the code?:

Code:
analogWrite(outputPin1, 170);  // send signals of 3.3v to Accelerometer


And the transistor I'm using is the NPN transistor.

-----------------------
General-purpose silicon, high-speed, medium control switching transistors. Rated 600mW. Case Type TO-92. Includes 5 of 2N2222, 5 of 2N4401 and 5 of 2N3904.
-----------------------
General Features
Model      276-1617   
Product Type      Transistors   
Enclosure Color      Black   
Body Material      Multi
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25849
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When I use the 3.3V pin, instead of outPutPin1, Pin D3, how I should edit the code?:
The 3.3V is always there, irrespective of the code you run.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I got it.

And what's the good way to send the output signal, instead of using

Code:
analogWrite(outputPin2, 77);  // send signals of 1.5V to Transistor
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 603
Posts: 33408
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just set the digital output pin high. The resistor you put between the output pin and the transistor's base will keep the current down and switch the transistor on. There is no need to supply a specific voltage to the transistor.
Logged

Pages: [1]   Go Up
Jump to: