Sunday, November 29, 2015

Password Access with Arduino

This post will show you how to make a pass code lock system using the Arduino Mega Board.
When you type the right code, an LED lights up an the servo moves to open a lock.


The materials that are needed for this project are:

one Arduino Mega (the arduino uno or duemilianove does not have enough digital pins for this project)
one LCD module
one Keypad
one Battery pack (or you can use the USB cable and PC power)
one 10K Ohm potentiometer
four 10K Ohm resistors
Breadboard
hookup wire
one servo

Step 1: Wire the LCD to the Arduino


The LCD module has 16 pins.
First of all, connect pins 1 and 16 of the LCD to the ground rail on the Breadboard
Then connect pins 2 and 15 of the LCD to the +5v rail on the breadboard
Now connect the ground rail(should be blue) of the breadboard to a ground pin on the Arduino;
Connect the +5v rail of the breadboard(this one is red) to one of the +5v pins on the Arduino board.

Now comes the contrast potentiometer which has to be connected to pin 3 of the LCD.
The potentiometer will have 3 pins. Take the middle pin and connect it to pin 3 of the arduino with hookup wire. Connect the othere two pins one to +5v and the other to GND(ground). The order does not matter.

Now let's do a test: power up the arduino. The LCD should light up. If it does then Great! If the LCD does not light up then turn off the power and check the wires.

Never change, move, or take out wires from the circuit board when the Arduino is powered up. You may permanently damage the Arduino.

If the light works rotate the potentiometer all the way to the right and all the way to the left until you see 2 rows of black squares. That's the contrast.

Now take out the power and let's hook up the LCD to the Arduino with the signal wires so we can display something on it.
Ready? Let's go!

Connect the pins as follows:

LCD Pin 4 --> Arduino Pin 2
LCD Pin 5 --> Arduino Pin 3
LCD Pin 6 --> Arduino Pin 4
LCD Pin 11 --> Arduino Pin 9
LCD Pin 12 --> Arduino Pin 10
LCD Pin 13 --> Arduino Pin 11
LCD Pin 14 --> Arduino Pin 12

And that should do it for the LCD circuit.

A test code for the the LCD: temporary.

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2,3,4,9,10,11,12);

void setup() 
{
    // set up the LCD's number of columns and rows:
    lcd.begin(16, 2);
    // Print a message to the LCD.
    lcd.print("hello, world!");
}

void loop()
{
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
     lcd.setCursor(0, 1);
   // print the number of seconds since reset:
   lcd.print(millis()/1000);
}

Copy and paste it in an arduino environment window, make sure you have the board and serial port set correct and click UPLOAD after you plug in the usb with the arduino.
You will see the TX and RX led's blinking, that means the code is going to the arduino.

Push the reset botton once on the arduino, tune the contrast, and you should see Hello World displayed.

Step 2: Wire the Keypad to the Arduino



Now that we're done with the LCD and we got it working, it's time to connect the keypad to the arduino. In my case, I used a 3x4 keypad that I had for some time.
If you have a keyboard that is made especially for connecting to an arduino, then it's easy. You just look at the datasheet for it and it tells you exactly how to hook it up.
If you have a keypad and you have no datasheet for it then hang on cause I was in the same situation.
Mine had on the back a diagram that shows you which pins are connected together when you press a certain key.
If you don't have that, you will have to use a multimeter and figure out which pins are connected together when you press each key.
To do that, take your multimeter and set it on continuity(the diode symbol).
Then put the test leads on pins 1 and 2 of the keypad. Now press every key until you get continuity.
Take paper and a pen and write down the key(ex:1, 2, #) and the two pins(ex: 6[1;2]).
Do so for every key until you get all of them figured out.

Make a table:
1=1+5
2=1+6
3=1+7
4=2+5
5=2+6
6=2+7
7=3+5
8=3+6
9=3+7
*=4+5
0=4+6
#=4+7

That is what I got.
Whatever you get, if you write down the keys in that order you will see the logic in it.
From my table I can see that the row pins are 1,2,3,4; and the column pins are 5,6,7.

Now plug the pins of the keypad in a breadboard and let's start connecting it.
Connect the pins for rows 2 and 3( in my case pins 2 and 3) to +5v through 10K Ohm resistors. Do the same with the pins for column 1 and 3 pins( in my case pins 5 and 7).

If you have an arduino mega, connect the keypad as follows:

Keypad pin row1--> arduino pin 25
Keypad pin row2--> arduino pin 24
Keypad pin row3--> arduino pin 23
Keypad pin row4--> arduino pin 22
Keypad pin column1 --> arduino pin 28
Keypad pin column2 --> arduino pin 27
Keypad pin column3 --> arduino pin 26

(The arduino uno does not have enough digital pins so it does not fit this project.)

That should do it for the keypad.

Step 3: Connecting the servo

It has 3 wires: Red, Yellow(or white or orange), and black.
Connect the red wire to +5v, the black wire to GND, and the middle wire to digital pin 8.
That's it for the servo.

Step 4: Preparations for coding

Before we put in the final code we have to make some modifications.


G o to the link above and download the libraries: keypad and password.
They are two files. take the files and put them in /Arduino/Libraries.

if the download does not work for bizarre reasons, go to:



Step 5:The code

Make sure you have all the wires in place and connect the USB cable.
Upload the following code to the arduino. Copy and paste it in the arduino window just like last time.

#include <Password.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>

Servo myservo;
int pos = 0;
LiquidCrystal lcd(2,3,4,9,10,11,12);
Password password = Password( "4321" );
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns

// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3',},
{'4','5','6',},
{'7','8','9',},
{'*','0',' ',}
};

// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {25, 24, 23, 22}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {28, 27, 26}; //connect to the column pinouts of the keypad
const int buttonPin = 7;
int buttonState = 0;

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledPin 13

void setup()
{
   myservo.attach(8);
   pinMode(buttonPin, INPUT);
   lcd.begin(16, 2);
   digitalWrite(ledPin, LOW); // sets the LED on
   Serial.begin(9600);
   keypad.addEventListener(keypadEvent); //add an event listener    for this keypad
   keypad.setDebounceTime(250);
}

void loop()
{
   keypad.getKey();
   buttonState = digitalRead(buttonPin);
   if (buttonState == HIGH)
   {
      lcd.clear();
   }
}

//take care of some special events

void keypadEvent(KeypadEvent eKey)
{
  switch (keypad.getState())
  {
    case PRESSED:
    lcd.print(eKey);
    switch (eKey)
    {
      case ' ': guessPassword(); break;
      default:
      password.append(eKey);
    }
  }
}

void guessPassword()
{
   if (password.evaluate())
   {
       digitalWrite(ledPin,HIGH); //activates garaged door relay
       delay(500);
       for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
       { // in steps of 1 degree
          myservo.write(pos); // tell servo to go to position in variable 'pos'
          delay(3); // waits 15ms for the servo to reach the position
       }
       for(pos = 180; pos>=50; pos-=1) // goes from 180 degrees to 0 degrees
       {
  myservo.write(pos); // tell servo to go to position in variable 'pos'
  delay(3); // waits 15ms for the servo to reach the position
        }

digitalWrite(ledPin,LOW); //turns off door relay after .5 sec
lcd.print("VALID PASSWORD "); //
password.reset(); //resets password after correct entry
delay(600);
lcd.print("Welcome");
delay(2000);
lcd.clear();
    }
    else
    {
digitalWrite(ledPin,LOW);
lcd.print("INVALID PASSWORD ");
password.reset(); //resets password after INCORRECT entry
delay(600);
lcd.clear();
    }
}


Give it a test: type 4321 then press #.
You should see the message VALID PASSWORD Welcome

After that the LED on the arduino board will light up for a short time and the servo will move to open the lock. 

And that's it, you've got yourself a password access system.
Put it on your door, or make a safe, or make a..... whatever you want. Put it on your bird cage so no one can steal your expensive exotic talking parrots.
If you have any problems or questions regarding this, feel free to post a comment.I will answer as soon as I can.




Saturday, November 28, 2015

Arduino Bluetooth Car

Hello Everyone,

This is my another post in which I am going to illustrate you about the Arduino Bluetooth Car.Here,I will show you the video which will completely illustrate you about this so that you can easily understand the functioning of the Arduino Bluetooth Car.





The Code:

int motorPin1 = 10;
int motorPin2 = 11;
int motorPin3 = 8;
int motorPin4 = 9;

void setup()
{
   Serial.begin(9600);

   pinMode(motorPin1, OUTPUT);
   pinMode(motorPin2, OUTPUT);
   pinMode(motorPin3, OUTPUT);
   pinMode(motorPin4, OUTPUT);

   digitalWrite(motorPin1, 0);
   digitalWrite(motorPin2, 0);
   digitalWrite(motorPin3, 0);
   digitalWrite(motorPin4, 0);
}

void loop()
{
   char vDados = Serial.read();
   if (vDados == '1')
   {
      fFrente();
   }
   elseif (vDados == '2')
   {
      fFrente();
      fDireita();
   }
   elseif (vDados == '3')
   {
      fDireita();
   }
   elseif (vDados == '4')
   {
      fRe();
      fDireita();
   }
   elseif (vDados == '5')
   {
      fRe();
   }
   elseif (vDados == '6')
   {
      fRe();
      fEsquerda();
   }
   elseif (vDados == '7')
   {
     fEsquerda();
   }
   elseif (vDados == '8')
   {
     fFrente();
     fEsquerda();
   }
   delay(111);

   fParar();
}
void fParar()
{
   digitalWrite(motorPin1, 0);
   digitalWrite(motorPin2, 0);
   digitalWrite(motorPin3, 0);
   digitalWrite(motorPin4, 0);
}
void fFrente()
{
   digitalWrite(motorPin1, 1);
}
void fDireita()
{
  digitalWrite (motorPin3, 1);
}
void fRe()
{
  digitalWrite(motorPin2, 1);
}
void fEsquerda()
{
  digitalWrite (motorPin4, 1);
}

If you have any query regarding my post,you can comment your questions and suggestions in comment section.

Arduino Bluetooth and Ultrasonic Sensor

Hello again,

This is my another post in which I am going to cover Bluetooth and Ultrasonic sensor(HC-SR04).
Now the question arises,Why I have used these two.I made an RC 2 wheel car for my school project. It is  controlled with a free app via bluetooth. To make it "half robot", it has distance sensor, that measures distance. If the distance is (in my case) equal or lower than 10cm, the car stops and after 1.5s the person can control it again.

I will add code at the end of this post.You can go through that code.If you have any problem regarding this,you can comment on the comment section.

Step 1: THE HC-SR04 ULTRASONIC SENSOR

First of all,I am going to tell you about the HC_SR04 ultrasonic sensor.



This component is very simple, as it only has 4 pins.
First  and the last are power pins Vcc and GND. The middle ones are Trigger pin and Echo pin.
If you want to understand what these pins do, you must understand how this thing works.
So,this module is sending ultrasonic waves then these waves reflect from a surface and come back to the module.Arduino is triggering those waves on the trigger pin and than listening for the “echo”. Once it receives it, it calculates the distance based on the time spend waiting for the wave to come back. In the code, we are also going to change that value to cm, as it it easier to read.
The module will be connected to any I/O pins of the arduino.The trigger pin will be output and the echo pin will be input.

I will also describe how the code works in the programming step.

Step 2: PROGRAMMING THE ULTRASONIC SENSOR


int triggerPin = 7; //triggering on pin 7
int echoPin = 8;    //echo on pin 8
void setup()
{

  Serial.begin(9600);  //we'll start serial comunication, so we can see the distance on the serial monitor
  pinMode(triggerPin, OUTPUT); //defining pins
  pinMode(echoPin, INPUT);
}
void loop()
{
  int duration, distance;    //Adding duration and distance
  digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED)
  delay(10);
  digitalWrite(triggerPin, LOW);
  duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave
  distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number
  Serial.print(distance);    //printing the numbers
  Serial.print("cm");       //and the unit
  Serial.println(" ");      //just printing to a new line

  //IF YOU WANT THE PORGRAM SPITTING OUT INFORMATION SLOWER, JUST UNCOMMENT(DELETE THE 2 //) THE NEXT LINE AND CHANGE THE NUMBER
  //delay(500);

}

Step 3: THE BLUETOOTH MODULE

First of all,I am going to tell you the Bluetooth module.Probably all Bluetooth modules have the same pin-out.
It has power pins Vcc and GND and communication pins RX and TX.
Now you can probably see, that this module connects to the RX and TX pins of a micro controller such as Arduino or a USB to serial converter.
This module works fully with 5V, so there is no problem connecting it to Arduino. All the pins are labeled on the back of the module.


You can see the connections in the images.

Step 4: PROGRAMMING THE BLUETOOTH MODULE

The code is made for turning LED on and OFF, but with the same method of "else if" statements seen in the code, you can do enything.

Another thing is that  When you have your bluetooth module connected to the TX and RX pins to the arduino, remember to pull them out every time you' re uploading the sketch or it will display an error like on the picture 2, than disconnect the RX and TX pins from the bluetooth module and upload the code again. Once the code is uploaded,connect the pins again.
After all that, you need to connect your phone to the bluetooth module. Power the arduino(and at the same time the bluetooth module) and find it in. Once you added the device, download this app for Android.I don't know if this works with Apple devices.

The app used is:https://play.google.com/store/apps/details?id=eu.jahnestacado.arduinorc, so it's called Arduino RC

int LED = 13; //led pin
int info = 0;//variable for the information comming from the bluetooth module
int state = 0;//simple variable for displaying the state
void setup()
{
  Serial.begin(9600); //making serial connection
  pinMode(LED, OUTPUT);    //defining LED pin
  digitalWrite(LED, LOW);  //once the programm starts, it's going to turn of the led, as it can be missleading.
}
void loop()
{
    if(Serial.available() > 0){  //if there is any information comming from the serial lines...
    info = Serial.read();
    state = 0;   //...than store it into the "info" variable
}
   if(info == '1')
   {                //if it gets the number 1(stored in the info variable...
    digitalWrite(LED, HIGH);    //it's gonna turn the led on(the on board one)
      if(state == 0)
      {              //if the flag is 0, than display that the LED is on and than set that value to 1
         Serial.println("LED ON");  //^^that will prevent the arduino sending words LED ON all the time, only when you change the state
         state = 1;
       }
    }
    else if(info == '0')
    {
       digitalWrite(LED, LOW);      //else, it's going to turn it off
       if(state == 0)
       {
         Serial.println("LED OFF");//display that the LED is off
          state = 1;
       }
     }
}

Step 5: TESTING THE BLUETOOTH!





Once you downloaded the app, run it and click connect to device. Choose your bluetooth module(my is the first one) and you will be taken to "the lobby". If you have noticed, this app is for RC(remote control) which is perfect for controllng a car. It has buttons or gyro sensoring. But what makes it not only for RC is the "TERMINAL MODE".There you can type in anything that will be sent to the arduino. In our case, If you send 1, the LED will come on, or if you type 0, the LED will go off.

Step 6: COMBINING DEVICES

Now, I've combine both of the codes, so you can turn on the LED remote and turn it off for 2s when the distance is less than 15cm.


All I did is that:
  • I combined all the int variables above the setups
  • I combine both setups into one
  • I named each loop of each code "bluetooth" and "sensor"
  • In the main loop I added those 2 loops
  • I added stopping function in the sensor part.

int triggerPin = 7; //triggering on pin 7
int echoPin = 8;    //echo on pin 8
int LED = 13; //led pin
int info = 0;//variable for the information comming from the bluetooth module
int state = 0;//simple variable for displaying the state

void setup()
{ //we will be combinig both setups from the codes
  
   Serial.begin(9600);  //we'll start serial comunication, so we can see the distance on the serial monitor
  pinMode(triggerPin, OUTPUT); //defining pins
  pinMode(echoPin, INPUT);
  pinMode(LED, OUTPUT);    //defining LED pin
  digitalWrite(LED, LOW);  //once the programm starts, it's going to turn of the led, as it can be missleading.
}
void loop()
{ //here we combine both codes to work in the loop
  bluetooth();
  sensor();
}
void bluetooth()
{ //loop from the bluetooth code is renamed to "bluetooth" void
  if(Serial.available() > 0)
  {  //if there is any information comming from the serial lines...
    info = Serial.read();   
    state = 0;   //...than store it into the "info" variable
  }
  if(info == '1')
  {                //if it gets the number 1(stored in the info variable...
    digitalWrite(LED, HIGH);    //it's gonna turn the led on(the on board one)
    if(state == 0)
    {              //if the flag is 0, than display that the LED is on and than set that value to 1
      Serial.println("LED ON");  //^^that will prevent the arduino sending words LED ON all the time, only when you change the state
      state = 1;
    }
   }
   else if(info == '0')
   {
    digitalWrite(LED, LOW);      //else, it's going to turn it off
    if(state == 0)
    {
      Serial.println("LED OFF");//display that the LED is off
      state = 1;
    }
  }
}

void sensor()
{ //loop from the sensor code is renamed to the "sensor" void
  
  int duration, distance;    //Adding duration and distance
  digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED)
  delay(10);
  digitalWrite(triggerPin, LOW);
  duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave
  distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number
  Serial.print(distance);    //printing the numbers
  Serial.print("cm");       //and the unit
  Serial.println(" ");      //just printing to a new line
  
  //adding for mesuring distance where the led will turn off, even if we tell it to turn off when we chose so in the app
  
  if(distance <= 15)
  {  //if we get too close, the LED will turn off, that's the method with my RC car, if it gets too close, it turns off, but that will be in the next instructable :)
    digitalWrite(LED, LOW);
    Serial.println("TOO CLOSE!!!");
    delay(2000); //so the stopping is visabl
  }
}






Friday, November 27, 2015

Bluetooth Enabled Door Lock using Arduino

This is my new post which will explain a simple way to make a password protected door lock using your Arduino, and this lock can be unlocked by sending a four digit pin from your Android phone.


 

Step 1: Materials Required

  • Arduino 
  • Electric Door Strike
  • Bluetooth Module 
  • Power Supply (Required voltage and amperage differs among different door strikes/locks)
  • TIP120 Transistor
  • 1N4001 Diode
  • Hookup Wire
  • Solderless Breadboard
  • An Android phone (optional, considering that there are lots of devices you could use to send serial data to our bluetooth modem including Iphones, computers, and other Bluetooth devices).

Step 2: Description of Transistor:

First of all,we will start by focusing on one of the main components of the circuit which is named as transistor.
Our transistor will allow us to control a device that requires more current than our Arduino can supply,by sending the transistor different values. The type of transistor we are using (the TIP120) has a base, collector,and an emitter which are labeled here. We will send the signal from pin 9 on the Arduino to the base of the transistor,and depending on the value sent, current will increase or decrease.

Step 3: Assemble The Circuit

In the diagram,I have shown how the transistor is wired up in our circuit.
As you can see,we have a diode pointed away from ground that is connected to the collector of the transistor as well as the ground of the lock itself.This diode will protect our electronics from any back voltage that might be created when our lock is turned off.At this point you could set pin 9 to high or low to control the lock.

Step 4: Adding Bluetooth

Now,the another step that we will follow in this is to add a Bluetooth module to our project.Simply connect RX on the Bluetooth module to TX on our Arduino board,TX on the module is then connected to RX on the Arduino, GND is obviously connected to ground, and lastly VCC is connected to 3.3 volts or 5 volts depending on your Bluetooth module.

Step 5: Final Product (without the code)

Here is a look at what everything should look like. Its a bit difficult to see everything, but this is just the completed circuit on the breadboard.

Step 6: The Code

Basically the Arduino will check to see if anything is being received through serial. If it is, it will read those chars into an array and from that point verify that what was received matches the password we defined.Here,the password defined is ABCD.

int lock = 9;          //pin 9 on Arduino
char final[4];         //Characters the Arduino will receive
char correct[4] = {'A','B','C','D'};    //User-Defined Password
int pass_correct = 0;          //Does Password match, 0=false 1=true

void setup()
{
   pinMode(lock, OUTPUT);
   Serial.begin(9600);
   digitalWrite(lock, HIGH);  //By default,lock is active(locked)
}
void loop()
{
   while(Serial.available()) 
   {
      for(int i=0; i<4; i++)   //While data is available read 4 bytes
      {
         final[i] = Serial.read();  //Read 4 bytes into the array labled "final"
      }

     for(int i=0; i<4; i++)
     {
       if(final[i]==correct[i]) //Compare each char received to each car in our password in order
       {
         pass_correct = 1;   //If we compare two chars and they match, set the pass_correct variable to true(1)
       }
       else
       {
          pass_correct = 0;  //if the two compared chars do NOT match, set pass_correct variable to false(0)
          break;   //End loop and stop comparing chars
       }
     }
   }
   if(pass_correct==1)  //If all chars compared match, deactivate(unlock) the lock for 5 seconds
   {
      Serial.println("Unlocked");
      digitalWrite(lock, LOW);
      delay(5000);
      Serial.println("Locked");
      pass_correct = 0;
   }
   else
   {
      digitalWrite(lock, HIGH); //Else if there was not a complete match, keep the lock high(locked)
   }

  /* FOR TESTING

    Serial.print(final[0]);
    Serial.print(final[1]);
    Serial.print(final[2]);
    Serial.print(final[3]);
    Serial.print(" | ");
    Serial.print(correct[0]);
    Serial.print(correct[1]);
    Serial.print(correct[2]);
    Serial.print(correct[3]);
    Serial.print(" ");
    Serial.print(pass_correct);
    Serial.println("");
  */
  delay(500);
}

Step 7: Implementing Your Android Phone

On the app market, you can search for Bluetooth SPP (Serial Port Profile). There are many apps that have been made to send serial data through Bluetooth on your phone. Mine is called Bluetooth SPP just like 4 or 5 other apps that are similar and can be found by going to the link below. After you install one of these Bluetooth SPP apps, you can pair with your bluetooth modem connected to the Arduino. If it asks for a key, it will usually be by default "1234" (without quotes of course). After that, you should beable to send ABCD and the door strike will unlock for around 5 and 1/2 seconds, and then lock itself again. This concludes my tutorial on making a Bluetooth enabled door lock. If you have any questions or corrections for my project let me know!








Arduino Bluetooth Remote using LCD Display

Hello everyone,This is my another blog in which I made a simple but useful remote LCD.

Now you have question in your mind that what is basically all about?

In this,you can connect your phone to the Blutooth module of your Arduino,after that enter a text message  and it will appear on the Arduino’s LCD.


Step 1: Requirement of Materials: 

The various materials that are required to make this are: 

•A breadbord.
•A LCD display and potmeter
•Some wires.
•A mobile phone Android, Windows or Apple.


Step 2: Stick the parts in the breadboard.

In this step,you don’t have to do much. Just put the parts in the breadboard.


Step 3: Connect the Bluetooth module.

After putting the parts in the breadboard,you will need to connect the Bluetooth module to the Arduino. 

Module: Arduino
TX ====>pin1
RX ====>pin2
VCC ====>+5v
GND ===>GND


Step 4: Connect the LCD

Now you'll need to connect the LCD screen to the Arduino.
For this,you can see the image.
 


Turn on your Arduno and test if the LCD is working properly. You will see some black filled pixels on the screen.

Step 5: Upload the code.

Now you will need to upload the code to the Arduino.
Start up your Arduino Software and select the correct COM port and board.

Open the file added (Bluetooth Commander.ino) and press "upload"!

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
String inData;
void setup()
{
    Serial.begin(9600);       
    lcd.begin(16, 2);
    lcd.print("Welcome!");
    delay(3000);
    lcd.clear();
}
void loop()
{
  int i=0;
  char commandbuffer[100];
  if(Serial.available())
  {
     delay(100);
     while( Serial.available() && i< 99)
     {
        commandbuffer[i++] = Serial.read();
     }
     commandbuffer[i++]='\0';
  }

  if(i>0)
     Serial.println((char*)commandbuffer);
     lcd.print((char*)commandbuffer);
     delay(1000);
     lcd.clear();
}

Step 6: Set up your phone.

First you will need to pair to the Bluetooth network called HC-05 you can do this with password 1234.

Download from the phone store some app where you can send serial commands to the module.



I used Bluetooth Terminal for Android. When you have done that open the app click on connect to a device and tap on HC-05 it should connect now.

Step 7: Test it!

Now test it!
Just type some text into the commander and then the text will appear on the Arduino!



When you have a problem uploading the Arduino sketch try unplugging the Bluetooth module's TX and RX pins, upload then. 

Post your comments and suggestions in the comments section Below!!!








Monday, November 23, 2015

Arduino Bluetooth RC-Car

This project is about a car that is controlled via Bluetooth.


Step 1: Materials Requirement

Arduino UNO
•Arduino Motor Shield
HC-05 Bluetooth module
•Jumper Wires
•Robot Wheels
•Omni Wheel
•Base to mount the parts (Here,I have used wood plank 10.5cmx14cm)
•Relay sheild or my relay circuit
•Power source


Step 2: Assembling the components






Now that we have all the components we connect the motor shield to the Arduino.I did a small trick to connect the RX and TX pin to the motor shield because mine didn't have female pin headers. I took two small female pin headers and soldered to my motor shield.

Next we have to mount the Arduino to the base of the car then mount the motor sheild on top of it. Next mount the Bluetooth module on the base remembering the Vcc,GND,RX and TX pins by noting it down.

Next mount the omni wheel onto the base and the dc motor robot wheels too.
Then connect the dc motors to the M1 and M2 ports of the motor shield.

Thereafter connect the RX pin in the module to the TX pin of Arduino and TX pin of module to RX pin of Arduino.

Lastly connect the relay circuit i made to digital pin 6 so that we can switch on the front light via Bluetooth.

Step 3: Coding and the android apk
Before uploading the code remove the 5v or RX and TX pins from the arduino which is connected to the Bluetooth module because there would be problems in uploading the code if not.

Go to play store and download the "Arduino Bluetooth RC Car" apk or scan the Qr code to download easily and install it switch on the car and connect the module using 1234 as the password

//Arduino Bluetooth car
//Author: Manu Goswami
//Function:Controls car via Bluetooth

#include <AFMotor.h>

//creates two objects to control the terminal 1 and 2 of motor shield
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
char command;
const int FrontLed = 6;
void setup()
{    
  Serial.begin(38400);  //Set the baud rate to your Bluetooth module.
  // Front LEDs
  pinMode(FrontLed, OUTPUT);  // declare FrontRightLed as output
}
void loop(){
  if(Serial.available() > 0){
    command = Serial.read();
    Stop(); //initialize with motors stoped
    //Change pin mode only if new command is different from previous.
    //Serial.println(command);
    switch(command){
    case 'F':
      forward();
      break;
    case 'B':
       back();
      break;
    case 'L':
      left();
      break;
    case 'R':
      right();
      break;
      case 'W':
      digitalWrite(6,HIGH);
      break;
      case 'w':
      digitalWrite(6,LOW);
      break;
     }
  }
}
void forward()
{
  motor1.setSpeed(90); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(90); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
}
void back()
{
  motor1.setSpeed(90);
  motor1.run(BACKWARD); //rotate the motor counterclockwise
  motor2.setSpeed(90);
  motor2.run(BACKWARD); //rotate the motor counterclockwise
}
void left()
{
  motor1.setSpeed(90); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}
void right()
{
  motor1.setSpeed(0);
  motor1.run(RELEASE); //turn motor1 off
  motor2.setSpeed(90); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
}
void Stop()
{
  motor1.setSpeed(0);
  motor2.run(RELEASE); //turn motor1 off
  motor2.setSpeed(0);
  motor2.run(RELEASE); //turn motor2 off
}

Saturday, November 21, 2015

Arduino Bluetooth Panzer

We have used remote control cars during our childhood and they are fun to drive the room or wandering the house. What could be more fun of these remote control cars. Suppose you have CCTV cameras around the house or office but there are some blind spots that are not covered by them like corner areas,then this project can be your Patrol Cam.

You can make this project very easily.For this,you can buy any rover on the market that has gorgeous futuristic cases.


Step 1: Materials Required

• An Arduino Uno R3 compatible.
• L298N Dual DC Motor Driver.
• A wireless Arduino IP camera.
Arduino Bluetooth Module (the cheapest HC-05 bluetooth module on www.robomart.com).
• Some mini cables or 1-pin project cables. Battery pack, here I use 7.4V Li-Po 
• Mini on/off switch for battery.
• Dual Motor Gearbox complete with two standard DC Motors.
• Tank Treads.
• Universal Plate Set.
• Cable ties.

Materials Required


Step 2: Chassis

We can use several modes of chassis that can be made by Universal Plate and Track and Wheel Set.You can try several combination so that you can get what mode you want.There are some configuration that are only suitable for flat tracks and other may be good for All Terrain Forward.

Here,I have picked Tank mode because it is best for All Terrain Forward and Backward moving. After assembling the motor gear set ,now get two pieces of female to female jumper cables with different colors, cut them into two in the middle with same lengths.Solder them to the DC motors terminals.

Solder each motor with two colors on its terminals.In my case,I use grey wire for left terminal and purple wire for right terminal.
Chassis

Step 3: How to do Wiring : Bluetooth Module to Arduino

Here,I have used HC-05 bluetooth module and you can more about this product by visting this link or you can just google keyword “HC-05”.

Wiring Bluetooth module on Arduino Uno R3 is very easy as it has only four cables. This JY-MCU Bluetooth Module comes with four pins female to female cables mark with red dashes and dots.I change this cables, because I need female headers on bluetooth module and male headers on Arduino.

Here,I have used rainbow-color cables so that it will be easy to guide you and now I am explaining you which color I use.

1.Red wire from HC-06 VCC to Arduino 5V.
2.Brown wire from HC-06 GND to Arduino GND.
3.Black wire from HC-06 TXD to Arduino Digital Pin 0 (Rx).
4.White wire from HC-06 RXD to Arduino Digital Pin 1 (Tx).


Step 4: Wiring : L298N Motor Driver to Arduino

Now,again I am going to explain you the connections in my colors:-

1.Red wire from Motor Driver +5V to Arduino VIN.
2.Brown wire from Motor Driver GND to Arduino GND.
3.Orange wire from Motor Driver ENB to Arduino Digital Pin 6.
4.Yellow wire from Motor Driver IN4 to Arduino Digital Pin 7.
5.Green wire from Motor Driver IN3 to Arduino Digital Pin 5.
6.Blue wire from Motor Driver IN2 to Arduino Digital Pin 4.
7.Purple wire from Motor Driver IN1 to Arduino Digital Pin 2.
8.Grey wire from Motor Driver ENA to Arduino Digital Pin 3.

If you are using another Motor Driver, maybe you need to adjust the connection to Arduino refer to your board's manual. If you have any question, you can post your comment,I will try my best to help you.


Step 5: Wiring : Battery and Motors

1.Purple and Blue wires from Motor Driver MOTOR-A to Right Motor terminals.
2.Green and Yellow wires from Motor Driver MOTOR-B to Left Motor terminals.
3.Red wire from Motor Driver VMS to Mini Switch bottom terminal.
4.Another Red wire from Mini Switch middle terminal to Battery 5V (Red wire).
5.Black wire from Motor Driver GND to Battery GND (Black wire).

For points 1 and 2,after testing when the wheels move the opposite way than the button you pressed, you need to interchange the terminal wires. That is why, here,I solder female headers on the motors' terminals, so that we can easily swap them.



Step 7: Adding a WiFi Camera

Now we put an eye on the tank. The most important thing is : it runs at 5V DC which is perfect match for Arduino project. Can we just set it up on the tank? Well, you can do so, but I think it weigh too much for my little tank with standard DC motors, and also too bulky. The camera base will block the tank treads movement. You can put something to set it up higher, but I think it is still too heavy and bulky.
Actually all we need is the main board which handle the video streaming wirelessly and the camera module itself.  which I can put it vertically and the camera case about the size of a golf ball.

The camera comes with an AC adapter. I cut the wire off and as you see in my photos, I solder it to a switch. You don't need to do this, you can simply screw the Black wire to Motor Driver GND and the Red wire to Motor Driver 5V.The switch I use was preventing power consumption while I was testing the tank movement.

Below the DC power jack there is a curve on the board. I put the antenna there. But before that,I put some tape on the board to prevent short circuit, because there lay some solder pads. I put a double tape below the camera and then tie it to its mainboard and the tank's plate.



Step 8: Optional Camera

I have to tell you that my OpAmp chip on my camera mainboard smokes.
While I am testing my camera alone without the tank. I was carelessly grab a 7.4V battery fully charged to 8.2V. Either that caused the burnt or my setting in higher resolution that draw too much current to the chip.

While I am searching for a replacement chip, I found my old android phone which I use for backup, lying in my drawer. I found a software streaming the phone's camera through wifi. I will go on with this application on the next step 10.
I use a tweeter (speaker) stand to hold my phone. I put some foam and cable ties. I use double tape to stick it on the motor gear box. It is done. Now we move to softwares.



Step 9: Arduino Sketch

/*
  Chienline @ 2015
  Controlling an Arduino car/tank using an Android phone over Bluetooth connection. 
  Android Software : BlueCam 2 by Johnny Visser [in PlayStore].
*/
char dataIn  = 'S';        //Character/Data coming from the phone. S=Stop;

int IN1Pin = 2; // Motor A IN1
int ENAPin = 3; // Motor A Enable
int IN2Pin = 4; // Motor A IN2
int IN3Pin = 5; // Motor B IN3
int ENBPin = 6; // Motor B Enable
int IN4Pin = 7; // Motor B IN4

int delayTime = 100;

void setup() 
{
  Serial.begin(9600);  //Initialize serial communication with Bluetooth module at 9600 baud rate.  
  pinMode(IN1Pin, OUTPUT);
  pinMode(ENAPin, OUTPUT);
  pinMode(IN2Pin, OUTPUT);
  pinMode(IN3Pin, OUTPUT);
  pinMode(ENBPin, OUTPUT);
  pinMode(IN4Pin, OUTPUT);
  
  //Stop both motors on power up.  
  stopMotors();
}

void loop()
{
  if (Serial.available() > 0)    //Check for data on the serial lines.
  { 
    dataIn = Serial.read();  //Get the character sent by the phone and store it in 'dataIn'.
    if (dataIn == 'F'){   //if incoming data is a F, move forward
        moveForward();
        delay(delayTime);
    }  
    else if (dataIn == 'B'){   //if incoming data is a B, move backward
        moveBackward();
        delay(delayTime);
    }  
    else if (dataIn == 'L'){   //if incoming data is a L, move wheels left  
        moveLeft();
        delay(delayTime);
    }  
    else if (dataIn == 'R'){   //if incoming data is a R, move wheels right
        moveRight();
        delay(delayTime);
    }
    else {
        stopMotors();    
    }
    stopMotors();    
  }
}

//These direction functions are designed for Keyes L298N Motor Driver.
//You need to change them to suit your other motor drivers.
void moveForward(){
    digitalWrite(IN1Pin, LOW);
    digitalWrite(IN2Pin, HIGH); //L-Forward
    digitalWrite(IN3Pin, LOW);
    digitalWrite(IN4Pin, HIGH); //R-Forward
    digitalWrite(ENAPin, HIGH);
    digitalWrite(ENBPin, HIGH);
}

void moveBackward(){
    digitalWrite(IN1Pin, HIGH);
    digitalWrite(IN2Pin, LOW); //L-Backward
    digitalWrite(IN3Pin, HIGH);
    digitalWrite(IN4Pin, LOW); //R-Backward
    digitalWrite(ENAPin, HIGH);
    digitalWrite(ENBPin, HIGH);
}

void stopMotors(){
    digitalWrite(ENAPin, LOW);
    digitalWrite(ENBPin, LOW);
}

void moveLeft(){
    digitalWrite(IN1Pin, HIGH);
    digitalWrite(IN2Pin, LOW); //L-Backward
    digitalWrite(IN3Pin, LOW);
    digitalWrite(IN4Pin, HIGH); //R-Forward
    digitalWrite(ENAPin, HIGH);
    digitalWrite(ENBPin, HIGH);
}

void moveRight(){
    digitalWrite(IN1Pin, LOW);
    digitalWrite(IN2Pin, HIGH); //L-Forward
    digitalWrite(IN3Pin, HIGH);
    digitalWrite(IN4Pin, LOW); //R-Backward
    digitalWrite(ENAPin, HIGH);
    digitalWrite(ENBPin, HIGH);
}


Step 10: Android Apps

Video Streaming :

You have to download this on your camera phone if you are using an android phone as the camera.This app will stream your camera through wifi and you can open it in any web browser.Then you can also view it within BlueCam 2 which we use as the tank controller.

Firstly, you need to setup your phone and connect it to your wireless router. After that open your IP Webcam then you will be taken to the settings page. If you attach your phone to stand vertical on the tank,go to “Video preferences”  
First, you need to setup your phone connect to your wireless router. Open IP Webcam then you are taken to the setting page. If you attach your phone to stand vertical on the tank, go to "Video preferences" (the first one in settings), set "Video orientation" to portrait. If you attach your phone in landscape mode like mine, you can skip this one. Go back to Settings. Go to the last item in settings that is "Start server". Then you will see what your camera sees on your screen. At the bottom center you will see something like : "http://your.ip.address.here:port". 

This is how you connect to your streaming camera. If you are using a web browser then just type that in the address bar. I will tell you later about how to connect from BlueCam 2.
We don't need this screen on because it is on the tank and it will waste the phone's battery. So you click on the "Actions..." button on upper right corner, then choose "Run in background". It will tell you that you can just press the "Home Key" on your phone and this app will still run in background. Click "Ok, I get it!". You will see the IP Webcam icon is on upper left corner, means that it is streaming the video in background, then you can turn off your screen.



Tank Controller :

Download this one on your controller phone. Before you run this apps make sure you have paired your JY-MCU module to your phone. Turn on your tank. Open bluetooth settings on your phone, search for devices. It will shows up as "LINVOR" or "HC-06" with default password "1234" (without quotes). Make sure your phone's wifi is connectinig to the same router as the phone you use as the tank's camera. Turning on bluetooth first before running the app is important, otherwise you won't see the bluetooth paired devices list and you need to exit the app and then open it again. Okay, now let's run the BlueCam 2.
This app is working in landscape mode only and it is good, we will have a wide camera view on the screen. Press the menu (Three Dots) button then choose "Configure buttons". If you don't see the menu buttons, you need to show it permanently in your navigation key. I haven't told Johnny about this one because I have my menu key "always visible" in navigation pane. Go to your Android Phone Settings > Navigation > Navigation bar > Menu visibility > Always show. This might be different a little bit depends on your phone models, but all you have to search is Menu visibility set to always show and it is under navigation bar setting.
Let's move on ... now we configure the buttons to match the sketch we uploaded to Arduino, that is :

•F for FORWARD button.
•B for BACKWARD button.
•L for LEFT button.
•R for RIGHT button.


Then turn on both Burstmode buttons next to Forward-Backward and Left-Right buttons. This Burstmode is sending the character every 250 ms. So when our finger is still pressing on the arrow, it will keep sending the character to Arduino. If it is off, it will only send the character once even if your finger is pressing the button, and the tank will move once then stop. Special thanks to Johnny who fixed it for us :) The "+" and "-" buttons are not in use. I configure them to send "S" which I called Brake buttons. When I was in testing, the motor won't stop even when I have released my finger from the buttons. Then I need this button to stop the tank. Something on my mind is to put a servo on the camera so that we can move the camera up and down. We don't need the left and right as we can move the wheels :) Now click on the Diskette button to save the buttons configuration.
On bottom left there is a list of paired bluetooth devices. My bluetooth module is already renamed to "PANZER", find your tank either "LINVOR" or "HC-06". Then click on the bluetooth icon, it will change to orange means connecting. If the connection is successful then the icon will change to red. Now you can move your tank with arrow keys.

Step 11: Result

Below,I am providing you the link which will show you how will it work.