Electronic – arduino – Accelerometer and Transistor

accelerometerarduinomicrocontrollerprogrammingtransistors

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.


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 Digital Multi Meter, it keeps showing 1.5V even it doesn't change the position of the sensor.

I guess there's some problem in the if, else line.

Best Answer

You are not using the reading variable in the if statement. Your if statement is checking volts, and this never gets changed, it will always be at 170 (it will change to 77 for a few clock cycles, then back to 170.)

Also you shouldn't declare your variables in the loop. And your delay is set to one second not 5. You should add a delay after analogWrite(outputPin1, volts); so the Accelerometer has time to power up.

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

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

void loop()    
{   
    analogWrite(outputPin1, volts);  // send signals of 3.3v to Accelerometer   
    delay(200);    // delay .2 sec

    reading = analogRead(analogPin);  //  receive signal
    delay(1000);    // delay 1sec.

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

Here is what the code is doing now (assuming your voltage math is correct):

  1. outputPin1 powers to ~3.3v
  2. reading gets the value of analogPin
  3. 1 second pause
  4. if reading >= 100 power outputPin2 to ~1.5v else 0v
  5. repeat

To be more efficient, you don't need the variable volts and reading. outputPin1 doesn't need to be in the loop. I also put the delay before you analogRead(analogPin), so your if statement executes immediately after the reading, and on power up it allows your accelerometer time to power up. I also removed the Serial.begin(9600); since you weren't using it. Your entire code could look like this:

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

For future reference, you should use the serial Serial.println("helpfull info"); to help you debug your code. Take a look at this example. Every time it takes a reading it transmits the reading back to the computer so you can see what is going on.

Edit

Upload this and use a serial terminal to see what happens when you move the accelerometer.

int analogPin = 0;
int value = 0;

void setup()
{
  Serial.begin(9600);          //  setup serial
}

void loop()
{
  value = analogRead(analogPin);    // read the input pin
  Serial.println(value);             // debug value
  delay(1000);
}