Friday, March 6, 2015

PROJECT 1: Accelerometer based Robot Control using ARDUINO

Let get down to some real fun.
This one is a wired robot & can be made wireless using rf433mhz, Xbee or other communication modules. which will feature in future tutorials. 


Below I have provided the source code & circuit for making an accelerometer based robot project.
i.e, you can make your own bot & steer it /control it using an accelerometer attached to the back of your hand.

For the same , i have used an MMA7361 (3axis accelerometer IC) module (cheap n easily available) attached to the analog pins of my arduino/avr. & 2 DC motors 150rpm which are attached to motor driver H-bridge IC L293D. which receives input to drive the motors via the PWM pins of my arduino.

You can either use a readymade motor driver shield or you can make your own L293D circuit on pcb & use it.

The controller needs 5v & can be powered by a 9v battery (preferably rechargeable)
but the motor driver circuit needs a supply of 12v dc 1amp. make sure you have an adapter or a Li-ion bat. for that too.

So, by now , i am sure you've had enough experiments with arduino & avr & you very well understand the code below & the circuit diagram.


Circuit:









Source Code:


int xaxis;
int yaxis;
int sensorx = A0;  // accelerometer x axis data pin
int sensory = A1;  // accelerometer y axis data pin
int motor1p = 11;  //connect motordriverboard's 1st motor +ve pin to digital pin 11
int motor1n = 10;  //connect motordriverboard's 1st motor -ve pin to digital pin 12
int motor2p = 5;   //connect motordriverboard's 2nd motor +ve pin to digital pin 5
int motor2n = 6;   //connect motordriverboard's 2nd motor -ve pin to digital pin 6


void setup()
{
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);//used to communicate arduino with computer
  pinMode(motor1n,OUTPUT);        // set motor pins as output pins
  pinMode(motor2n,OUTPUT);
  pinMode(motor1p,OUTPUT);
  pinMode(motor2p,OUTPUT);
  pinMode(sensorx,INPUT);        // set accelerometer pins as input pins
  pinMode(sensory,INPUT);
}



void loop()
{
                               
  int xaxis = analogRead(sensorx); //analogRead(A0) can also be used to read the accelerometer x axis data
  int yaxis = analogRead(sensory); //Go to Tools and click on Serial Monitor & print out the value you read:
  Serial.print("x axis:");
  Serial.println(xaxis);   //see the output of sensor on computer using serial monitor
  Serial.print("y-axis:");
  Serial.println(yaxis);
  delay(100);
if(yaxis<425 && yaxis>275 && xaxis<=425 && xaxis>=275 )
{
  digitalWrite(motor1n,LOW);
  digitalWrite(motor1p,LOW);
  digitalWrite(motor2n,LOW);
  digitalWrite(motor2p,LOW);
}
 else if (yaxis>=425 && xaxis<=425 && xaxis>=275)
 {
   Serial.write("Forward");
  digitalWrite(motor1n,LOW);     // give reference pin 0V
  analogWrite(motor1p,100);       // give motor speed(voltage) ranging from 0-255
  digitalWrite(motor2n,LOW);
  analogWrite(motor2p,100);       //voltage is given as (50/255)*5 V
 }

 else if (yaxis<=275 && xaxis<=425 && xaxis>=275)
 {
   Serial.println("Backward");
  analogWrite(motor1n,100);       //give speed as 50 in reverse direction
  digitalWrite(motor1p,LOW);  
  analogWrite(motor2n,100);       //voltage is still given as (50/255)*5 V
  digitalWrite(motor2p,LOW);
 }


else if (xaxis<=275 && yaxis<=425 && yaxis>=275)
 {
  Serial.println("LEFT");
  digitalWrite(motor1n,LOW);
  analogWrite(motor1p,100);
  digitalWrite(motor2n,LOW);
  digitalWrite(motor2p,LOW);
 }



else if (xaxis>=425 && yaxis<=425 && yaxis>=275)
 {
   Serial.write("RIGHT");
  digitalWrite(motor1n,LOW);
  digitalWrite(motor1p,LOW);
  digitalWrite(motor2n,LOW);
  analogWrite(motor2p,100);
 }
}


Copy the above source code & burn it onto your arduino board , & make connections according to the circuit diagram. Power it n run 
Thats all. 

Have Fun.
Will be posting the same project for AVR & PIC too... keep reading.

If you have any doubts or suggestions regarding any part of the project  , feel free to ask in the comments.






-Abhirup