Thursday, November 19, 2015

Arduino Bluetooth Controller

Hello All!

This is my another blog in which I am going to show you how to connect an Arduino to your android phone via Bluetooth and then use voice commands to control high voltage devices using relays.
Electricity is dangerous so you have to be very careful in this…

Step 1: Materials that are required 

The various materials that are required in this project are : 
•Relay Module
•Wires
•LED's
•A Lamp
•Android Phone with Bluetooth
•Electrical Tape
•Wire Strippers (or knife)

Materials Required
Step 2: Relay Test

First of all we have to check if the relay is switching or not.
You can Wire up the relay as shown in the diagram, make sure that the JD-VCC and VCC pins are bridged if you are powering the relay from your arduino. If they are not bridged you will see the LED turning on and off every 2 seconds but there will not be the clicking sound of the relay switching.



Code:

#define relay 2    //attaches the relay to pin 2
void setup()
{
  pinMode(relay, OUTPUT);    //sets the relay as an output
}
void loop()
{
  digitalWrite(relay, HIGH);     //relay open
  delay(2000);                           //wait 2 seconds
  digitalWrite(relay, LOW);     //relay closed
  delay(2000);                          //wait 2 seconds
}

Step 3: Bluetooth Test

First of all wire up the circuit as shown in the images.Here,I have used a breadboard and made one rail positive and one negative.One thing that I found in this that the TXD and RXD pins on the Bluetooth module don’t work when connected to the same pins on the arduino itself.


The TXD pin on the Bluetooth module I have connected to the RXD pin on the arduino (pin 0), and the RXD pin on the Bluetooth module is connected to the TXD pin on the arduino (pin 1). The Bluetooth Module will run off 3.3v but the relay needs 5v to work, hence I have used 5 volts on the arduino.
Now,I am showing you the code that I have written for this 2 switch relay. 

/*
------------------------------------------------------------------------
                            InfidelFish
------------------------------------------------------------------------
*/

String voice;
#define relay1 2    //Connect relay1 to pin 2
#define relay2 3    //Connect relay2 to pin 3
void setup()
{
  Serial.begin(9600);            //Set rate for communicating with phone
  pinMode(relay1, OUTPUT);       //Set relay1 as an output
  pinMode(relay2, OUTPUT);       //Set relay2 as an output
  digitalWrite(relay1, LOW);     //Switch relay1 off
  digitalWrite(relay2, LOW);     //Swtich relay2 off
}
void loop()
{
  while(Serial.available())    //Check if there are available bytes to read
  {
    delay(10);                 //Delay to make it stable
    char c = Serial.read();    //Conduct a serial read
    if (c == '#'){
      break;                   //Stop the loop once # is detected after a word
    }
    voice += c;                //Means voice = voice + c
  }
    if (voice.length() >0)
    {
      Serial.println(voice);
      if(voice == "*switch on"){
        switchon();
      }               //Initiate function switchon if voice is switch on
      else if(voice == "*switch off"){
        switchoff();
      }               //Initiate function switchoff if voice is switch off
      else if(voice == "*lamp on"){   
//You can replace 'lamp on' with anything you want...same applies to others
        digitalWrite(relay1, HIGH);
      }
      else if(voice == "*lamp off"){
        digitalWrite(relay1, LOW);
      }
      else if(voice == "*kettle on"){
        digitalWrite(relay2, HIGH);
      }
      else if(voice == "*kettle off"){
        digitalWrite(relay2, LOW);
      }
      voice="";
    }
}
void switchon()               //Function for turning on relays
{
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
}
void switchoff()              //Function for turning on relays
{
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
}

/*

You can add any function you want depending on how many devices you have hooked up.
For example you could have a function called 'cinema' which would dim the lights and
turn the TV on. You can have as many as you have pins on your arduino.
For my relay 'LOW' turns off and 'HIGH' turns on
The outline to follow is this:
   
   void ......()
   {
     digitalWrite(...., LOW/HIGH);
     digitalWrite(...., LOW/HIGH);
   }
   
*/

When you will upload the code to your Arduino,you have to be make sure that you unplug the pins 0 and 1 otherwise you will probably get an error like this:

avrdude: stk500_getsync(): not in sync: resp=0x00

Another thing that you have to do is to download the app on your android phone.You can download it from Google Playstore

https://play.google.com/store/apps/details?id=robotspace.simplelabs.amr_voice&hl=en

Connect it to the Bluetooth module it will probably be called something like ‘HC-06’.
The first time it will ask you for a password.Most of the times password is 1234.

Once you have connected say the commands you have chosen in the code and hopefully the relay will switch on and off!

Step 4: Adding the Appliances

The first thing you have to do is to find an old lamp or any other appliances as we have to cut the lead in this.
Now,remove the outer rubber sleeving but just make sure not to cut the sleeving of the wires inside,we don’t want any live wire visible.

Next cut the positive wire (the red one) and remove a few millimetres of sleeving on each end. Insert the exposed wire in the correct ports of the relay as shown in the diagram and pictures above. MAKE SURE THERE IS NO WIRE VISIBLE! For safety wrap the wires in electrical tape, and cover any of the places that could be live on the relay, such as the screws.

Once this is done we are ready to power on the lamp and test the system.



Step 5: Conclusion

I just hope my blog will be informative and you will be are able to make that.

The possibilities for this I feel are endless you can add loads of devices and then create functions in order to control several at once.
Here is a video of the lamp working:










Tuesday, November 17, 2015

Rover 5 using Xbee and Arduino Mega 2560

Hello everyone, In this post you are going to learn how to work with Dagu Rover 5.I am a big enthusiast for the sharing of knowledge and get lot of information from the internet,so here is me sharing my  collective knowledge to all of you.

I have shown you the image of Dagu rover 5 arduino uno mega 2560 Xbee by which you can get some sort of idea of it.
The various items needed for this project are
Dagu Rover 5  in which I have the 4 motors/ 4 encoder model
rover 5 motor driver Board
arduino uno mega 2560
Xbees ,here I use XBee Series 1
XBee Explorer Dongle v2
XBee Explorer Regulated - plus pins and soldering stuff
Batteries - 6AA and 5v usb bank.
male to female jumper wire
Pololu RP5/Rover 5 Expansion Plate RRC07B (Wide) Transparent Clear.
Step 1: Cut a hole for the motor wire to come through
To attach the Pololu RP5/Rover 5 Expansion Plate to rover you have to cut out a hole in the center for the wires from the motors and encoders to come through. After that, cut some to plastic bit with a pointed knife. then pull the wires through, this will allow for the plate to be mounted flat.

Step 2: Wiring Part A

First of all,you have to attach the motor and encoder wires to the rover 5 motor driver Board. I have shown the image above of how i wired it but it doesn't show how I attach the motor and encoder wires. To wire these, draw them back to the motor and then plug them into the right port. For the encoder wires you have to plug them into bottom row of the pins label "INT ENC1 ENC2" . From left to right it is Red - Yellow - White - Black.

Step 3: Wiring Part B


Above shown image shows you how I wire the rover 5 motor driver Board to the arduino uno mega 2560.
Now, I am showing you some arduino codes of where to wires go.

#define encoderA 2
#define encoderB 3           // Only use one interrupt in this example
#define encoderC 19
#define encoderD 18        // Only use one interrupt in this example

volatile int Lcount;
volatile int Rcount;

int Lcountsaved;
int Rcountsaved;

int speedch1 = 9; // left front
int speedch2 = 10; // left back
int speedch3 = 11; // right front
int speedch4 = 12; // right back

// direction pins 

int dirch1 = 44; // left front
int dirch2 = 45; // left back
int dirch3 = 42; // right front
int dirch4 = 43; // right back
int curch1 = A1; // left front
int curch2 = A2; // left back
int curch3 = A3; // right front

int curch4 = A4; // right back

Here, I have included the cur(current) pins and I have used a breadboard for a common ground, this may be wrong but it works best for me.

Step 4: Platform to hold batteries


After wiring the lower platform, there is no room for batteries. Then I created one for mine out of paper mache and wire.That is not very good, possible alternatives 3d print or shop bought box that fits.Then we have to attach the batteries via velcro.


Step 5: Coding basic

Now, I am presenting you the basic code for all four motor to spin for 5 seconds, and here,there are no encoder or xbee in this code.

//ROBOT CAR CODE 001

int speedch1 = 9;
int speedch2 = 10;
int speedch3 = 11;
int speedch4 = 12;
int dirch1 = 44;
int dirch2 = 45;
int dirch3 = 42;
int dirch4 = 43;
int curch1 = A1;
int curch2 = A2;
int curch3 = A3;
int curch4 = A4;
 void setup() {                
  // initialize the digital pin as an output.
pinMode(dirch1,OUTPUT);
pinMode(dirch2,OUTPUT);
pinMode(dirch3,OUTPUT);
pinMode(dirch4,OUTPUT);
pinMode(speedch1,OUTPUT);
pinMode(speedch2,OUTPUT);
pinMode(speedch3,OUTPUT);
pinMode(speedch4,OUTPUT);
 }
 
// the loop routine runs over and over again forever:
void loop() {
   delay(2000);
   digitalWrite(dirch1,LOW); // direction motor spins 
   digitalWrite(dirch2,HIGH); // 1 and 2 are on the same side but fight against each other.
   digitalWrite(dirch3,LOW);
   digitalWrite(dirch4,HIGH);
   analogWrite(speedch1,200); // speed PWM 0 - 255 
   analogWrite(speedch2,200);
   analogWrite(speedch3,200);
   analogWrite(speedch4,200);
   
   delay(5000);//how long the motors run
   
   analogWrite(speedch1,0); // stop
   analogWrite(speedch2,0);
   analogWrite(speedch3,0);
   analogWrite(speedch4,0);
}

Step 6: Encoders code
Now, I am presenting a easy code for the left encoder.After working for many hours, I       found a linear encoder code that works best for our project.This code will spin the left      motors until the encoder(count) reaches 500.It also print it to the serial monitor.It is same for the other side just have to change the pins and names.
// Interrupt information // pins may be different on uno<br>   // 0 on pin 2
// 1 on pin 3


#define encoderA 2
#define encoderB 3 // Only use one interrupt in this example
volatile int count;


int speedch1 = 9; // left front
int speedch2 = 10; // left back
int speedch3 = 11; // right front
int speedch4 = 12; // right back
// direction pins 
int dirch1 = 44; // left front
int dirch2 = 45; // left back
int dirch3 = 42; // right front
int dirch4 = 43; // right back


int curch1 = A1; // left front
int curch2 = A2; // left back
int curch3 = A3; // right front
int curch4 = A4; // right back
int TIME; //time the command is for
int SPEED;// 0 - 255 motor speed


void setup()
{
Serial.begin(9600);
count=0;
pinMode(dirch1,OUTPUT);
pinMode(dirch2,OUTPUT);
pinMode(dirch3,OUTPUT);
pinMode(dirch4,OUTPUT);


pinMode(speedch1,OUTPUT);
pinMode(speedch2,OUTPUT);
pinMode(speedch3,OUTPUT);
pinMode(speedch4,OUTPUT);
pinMode(encoderA, INPUT);
pinMode(encoderB, INPUT); attachInterrupt(0, handleEncoder, CHANGE);

}

void loop()
{
 delay(1000);
  (SPEED = 200);
   forward();
  if (count > 10) Serial.println(count); 
}

void handleEncoder()
{
if(digitalRead(encoderA) == digitalRead(encoderB))
{ count++;
}
else
{ count--;
}


}
void forward()
{  
     
   digitalWrite(dirch1,LOW);
   digitalWrite(dirch2,HIGH); 
   analogWrite(speedch1,SPEED);
   analogWrite(speedch2,SPEED);
   
   if (count > 500){ 
   
   analogWrite(speedch1,0);
   analogWrite(speedch2,0);
  }
}
Step 7: Xbee
We will need Xbees to solder some pins into it. First of all,take Xbees of your choice and program them to take to each other on the same channel using XBee Explorer Dongle v2 and XCTU.Choose the serial port for the Xbee on the Arduino 2 or 3 and after that you have to run these easy receiving code.This code will turn led 13 on the board on when it  receives processing in the next step.
int incomingByte = 0;   // for incoming serial data

int led = 13;

  void setup() {
  Serial2.begin(9600);  // change to the serial port /2/3
  pinMode(led, OUTPUT);        
}

void loop() {

      // send data only when you receive data:
        if (Serial2.available() > 0)
        {
         // read the incoming byte:
            incomingByte = Serial2.read();

            if(incomingByte == 49) digitalWrite(led, HIGH);
            else if(incomingByte == 50) digitalWrite(led,LOW);
         }
}

Step 8: Processing
This is the another step.Firstly,import processing.serial. plug you other xbee to the XBee Explorer Dongle v2 and into your computer,here I found a USB extensionlead.Then,you  have to button it now should turn on led 13 on the board.I have written this code in pick    the port to which you are attach too and Clicking and holding the mouse v2.1.1.
import processing.serial.*;

Serial myPort;  // Create object from Serial class

void setup(){
  size(640, 360);
  println(Serial.list());// This will list serial ports
  String portName = Serial.list()[5];// port number
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if (mousePressed) {
    myPort.write(49);
  } else {
    myPort.write(50);
  }
}
Step 9: Final code

Now,I am presenting you the final code for the arduino uno mega2560.Here,I have          created the subcommands so that it will be easy for all of you to understand.
//ROBOT CAR CODE 004
// commands
// forward()
// backward()
// rotate_left()
// rotate_right()

//notes 
//   FULL 360 rotate
//  (TIME = 12000);   //delay time
//  (SPEED = 200);   //speed 0-255
//  rotate_right(); 
int incomingByte = 0; //  for incoming serial data 
//speed pins 0 - 255 

#define encoderA 2
#define encoderB 3 // Only use one interrupt in this example
#define encoderC 19
#define encoderD 18 // Only use one interrupt in this example
volatile int Lcount;
volatile int Rcount;
int Lcountsaved;
int Rcountsaved;

int speedch1 = 9; // left front
int speedch2 = 10; // left back
int speedch3 = 11; // right front
int speedch4 = 12; // right back
// direction pins 
int dirch1 = 44; // left front
int dirch2 = 45; // left back
int dirch3 = 42; // right front
int dirch4 = 43; // right back

int curch1 = A1; // left front
int curch2 = A2; // left back
int curch3 = A3; // right front
int curch4 = A4; // right back
int TIME; //time the command is for
int SPEED;// 0 - 255 motor speed

int count;
int check1; //see if subcomand has run
int check2; //see if subcomand has run
int check3; //see if subcomand has run
int check4; //see if subcomand has run
int check5; //see if subcomand has run

void setup() 
{                
Lcount=0;
Rcount=0;

// initialize the digital pin as an output.
pinMode(dirch1,OUTPUT);
pinMode(dirch2,OUTPUT);
pinMode(dirch3,OUTPUT);
pinMode(dirch4,OUTPUT);

pinMode(speedch1,OUTPUT);
pinMode(speedch2,OUTPUT);
pinMode(speedch3,OUTPUT);
pinMode(speedch4,OUTPUT);

pinMode(encoderA, INPUT);
pinMode(encoderB, INPUT); attachInterrupt(0, LhandleEncoder, CHANGE);
pinMode(encoderC, INPUT);
pinMode(encoderD, INPUT); attachInterrupt(5, RhandleEncoder, CHANGE);

Serial2.begin(9600);
Serial.begin(9600);

analogWrite(speedch1,0);// stop all
analogWrite(speedch2,0);// stop all
analogWrite(speedch3,0);// stop all
analogWrite(speedch4,0);// stop all

count = 0;
(SPEED = 200);

}

// the loop routine runs over and over again forever:
void loop()
{
   if (Serial2.available() > 0) {
    // read the incoming byte:
   incomingByte = Serial2.read();               
  }
  
  if(incomingByte == 10 && check1 == 0) forward();
  else if(incomingByte == 11 && check2 == 0) rotate_right();
  else if(incomingByte == 12 && check3 == 0) backward();
  else if(incomingByte == 13 && check4 == 0) rotate_left();
  else if(incomingByte == 14 && check5 == 0) pause();

// if(incomingByte == 16){
// if (Lcount != Lcountsaved && Rcount != Rcountsaved)
 Serial2.print(Lcount,DEC);//send encoder data
 Serial2.print(",");
 Serial2.print(Rcount,DEC);
 Serial2.print(",");
 Serial2.println();
 
 incomingByte = 1;
// Lcountsaved = Lcount;
 //Rcountsaved = Rcount;
// }
 }
 
  void LhandleEncoder()
{
if(digitalRead(encoderA) == digitalRead(encoderB))
{ Lcount++;
}
else
{ Lcount--;
}
}

void RhandleEncoder()
{
if(digitalRead(encoderC) == digitalRead(encoderD))
{ Rcount++;
}
else
{ Rcount--;
}
}

void forward()
{             
   check2 = 0;
   check3 = 0;
   check4 = 0;
   check5 = 0;
   
   analogWrite(speedch1,0);
   analogWrite(speedch2,0);
   analogWrite(speedch3,0);
   analogWrite(speedch4,0);
 
   delay(100);
 
   digitalWrite(dirch1,LOW);
   digitalWrite(dirch2,HIGH); 
   digitalWrite(dirch3,LOW);
   digitalWrite(dirch4,HIGH);
   analogWrite(speedch1,SPEED);
   analogWrite(speedch2,SPEED);
   analogWrite(speedch3,SPEED);
   analogWrite(speedch4,SPEED);
   
   check1 = 1;
}

void backward()
{  
   check1 = 0; 
   check2 = 0; 
   check4 = 0;
   check5 = 0;
   
   analogWrite(speedch1,0);
   analogWrite(speedch2,0);
   analogWrite(speedch3,0);
   analogWrite(speedch4,0);
   
   delay(100);
 
   digitalWrite(dirch1,HIGH);
   digitalWrite(dirch2,LOW); 
   digitalWrite(dirch3,HIGH);
   digitalWrite(dirch4,LOW);
   analogWrite(speedch1,SPEED);
   analogWrite(speedch2,SPEED);
   analogWrite(speedch3,SPEED);
   analogWrite(speedch4,SPEED);

   check3 = 1;
  
}
void rotate_right()
{  
   check1 = 0;
   check3 = 0;
   check4 = 0;
   check5 = 0;
   
   analogWrite(speedch1,0);
   analogWrite(speedch2,0);
   analogWrite(speedch3,0);
   analogWrite(speedch4,0);
   
   delay(100); //wait to stop
   
   digitalWrite(dirch1,LOW);
   digitalWrite(dirch2,HIGH); 
   digitalWrite(dirch3,HIGH);
   digitalWrite(dirch4,LOW);
   analogWrite(speedch1,SPEED);
   analogWrite(speedch2,SPEED);
   analogWrite(speedch3,SPEED);
   analogWrite(speedch4,SPEED);
   
   check2 = 1;
}
void rotate_left()
{  
   check1 = 0;
   check2 = 0;
   check3 = 0;
   check5 = 0;  
   
   analogWrite(speedch1,0);
   analogWrite(speedch2,0);
   analogWrite(speedch3,0);
   analogWrite(speedch4,0);
   
   delay(100);
   
   digitalWrite(dirch1,HIGH);
   digitalWrite(dirch2,LOW); 
   digitalWrite(dirch3,LOW);
   digitalWrite(dirch4,HIGH);
   analogWrite(speedch1,SPEED);
   analogWrite(speedch2,SPEED);
   analogWrite(speedch3,SPEED);
   analogWrite(speedch4,SPEED);
  
  check4 = 1;
}
void pause()
{ 
   check1 = 0;
   check2 = 0;
   check3 = 0;
   check4 = 0;  
  
   analogWrite(speedch1,0);
   analogWrite(speedch2,0);
   analogWrite(speedch3,0);
   analogWrite(speedch4,0);
  
   check5 = 1; 
}
Step 10: Final Processing code
Now,I am presenting you the processing code which is used to run the Xbee.I have used arrow keys for the direction and ctrl key for stop.I have included the data in channel to receive the encoders.Sometimes,it crashes when it only receives 1 Encoder Code is written in step 2.
 import processing.serial.*;
 Serial myPort;  // Create object from Serial class
 int lf = 10;    // Linefeed in ASCII
 int source1; 
 int source2;
 String myString = null;

void setup()
{
  size(640, 360);
  println(Serial.list());
  String portName = Serial.list()[5]; // pick the port connected
  myPort = new Serial(this, portName, 9600);
  myPort.clear();

  myString = myPort.readStringUntil(lf);
  myString = null;
  source1 = 0;
  source2 = 0;

}
 
 void draw()
{
   background(255,255,255);
   textSize(32);
   text(source1,20,60);
   text(source2,20,120);
   fill(0,0,0);
   while (myPort.available() > 0) 
   {
    myString = myPort.readStringUntil(lf);
    if (myString != null) 
    {
      println(myString);  

     String[] q = splitTokens(myString, ",");//split at ","
     println(q.length + " values found");  // Prints 2 values found"
     source1 = int(q[0]);
     source2 = int(q[1]);
     }
    }
}
 void keyPressed() 
{
  if (key == CODED)
  {
    if (keyCode == UP)
    {
     myPort.write(10);//Forward
    } 
    else if (keyCode == DOWN)
    {
      myPort.write(12);//Backward
    }
    else if(keyCode == LEFT)
    {
      myPort.write(13);//LEFT
    }
    else if(keyCode == RIGHT)
    {
      myPort.write(11);//Right
    }
    else if(keyCode == CONTROL)
    {
    myPort.write(14); //Pause
    }
   }
}
Step 11: How It works.
Below I have uploaded a video of the rover rolling over some obstacles.I just hope my blog will be helpful to you.











Monday, November 16, 2015

Flame Sensors Using Arduino

Today I am going to provide you the simple guide which gives an appropriate explanation about how to use and explore the Flame Sensor module with an Arduino.
Arduino Flame Sensor
Arduino Flame Sensor
The  various materials that are needed in this are :
·         Flame Sensor (model with an analog out)
·         Male to Female jumper wires
·         An Arduino, any flavor.
·         Lighter or another flame source for testing.
You can use any other flavors of Arduino as well  in your project like arduino yun, arduino unojoystick, mpu6050 arduino uno and you can easily get any type of arduino from robomart (http://www.robomart.com).

Step 1: Basic Idea About the Flame Sensor : 
 Flame Sensors
·         Usage:
We used these types of sensors for short range fire detection and they can be used to monitor the projects or as a safety precaution to cut devices on/off.
·         Range:
Most accurate unit for this is up to about 3 feet that I have found in my               experiment.
·         How it works:
The flame sensor that we have used in our project is very sensitive to Infra Red wavelength at 760 nm ~ 1100 nm light.
Analog output (A0): In this ,Real-time output voltage signal on the thermal resistance.
Digital output (D0): When the temperature reaches a certain threshold,the output high and low signal threshold will be adjusted via potentiometer.               
·         Pins:
VCC...... Positive voltage input: 5v for analog 3.3v for Digital.
A0.......... Analog output
D0......... Digital outputGND..... Ground
·         Dimensions:
 (3.0 cm x 1.5 cm x 0.5 cm) 1.18 in x 0.59 in x 0.20 in

·         Weight:
 (8 g) 0.28 oz

Step 2: Testing and Troubleshooting:

Arduino Uno Testing
Arduino Uno Testing

Testing:
To test the Flame Sensor and ensure that it is working correctly connect the VCC to a 5v power source and GND. Move a flame source with in a foot of the front of the Ir sensor and the D0-LED should light up.
Troubleshooting:
If the DO-LED does not light up,you have to check the following :
·         Is the power source 5v?
·         Is the ground hooked up?
·         Is the flame with in 1 foot and in Line of Sight?
When you found that If none of the previous makes the D0-LED light up, your sensor may be defective.

Step 3: Wiring to an Arduino:



To wire the Flame Sensor to the Arduino simply connect the following as shown:
Flame sensor ............... Arduino
VCC............................... 5v
GND...............................GND
A0.................................. Analog in 0

 

Step 4: Arduino Sketch Example:

The following code maps and reads the analog values given by the flame sensor (0-1024). The stock flame sensor will have the following reaction with this code:
·         If holding a flame within 1.5 feet in front of the sensor; "case 0" will be activated and " ** Close Fire ** " will be sent to the serial monitor.
·         If holding a flame between 1.5 feet and 3 feet in front of the sensor; "case 1" will be activated and " **Distant Fire** " will be sent to the serial monitor.
If no flame is detected in front of the sensor; "case 2" will be activated and "No Fire " will be sent to the serial monitor.Arduino Uno testing