martes, 30 de agosto de 2011

Connect Arduino to an Android Device using Bluetooth SPP

In this tutorial we’ll check out how to make a really quick and effective connection via bluetooth (using one Bluesmirf module) with one Arduino and one Android cellphone/tablet.
Bluesmirf is one Bluetooth device specially designed for use with microcontrollers via a UART connection, it’s a Serial device. It uses Bluetooth SPP(Serial Port Protocole) for communication, which is a Serial Port emulation via a Bluetooth Connection.
Programming in Arduino is one thing, but programming in Android is another... So, what do we do if we want to control our Arduino using one Android device, but without programming this device?, I found a solution: Blueterm, this Android app is a download free app, that emulates a serial port terminal via a Bluetooth connection, really simple to use, it only requires that our Android device is Bluetooth SPP capable. So... if we have one Bluetooth SPP device(Bluesmirf preferable), one Arduino and one Android Device we are ready to make a connection!
We’ll be using the next stuff:


Bluesmirf and Bluetooth Mate have different pinouts...

Blueterm is a terminal app, this means it will display characters sent and received via a bluetooth serial port connection. So... let’s download and install Blueterm on our android device, Blueterm can be downloaded freely via Android Market, no further settings required, should work on most Android devices, if encountering problems, please refer to manufacturer’s datasheet for bluetooth profiles installed.
Now, we have to prepare our hardware, we’ll make a really simple test program to see if Arduino is receiving data via a bluetooth connection. First, we have to connect our Rx/Tx pins to our bluetooth module, if using a Bluesmirf module make sure to power it with 5V. You can use a breadboard or male/female header cables for connections.
Here’s our Arduino test code:
void setup(){
  //make sure your bluesmirf module is
  //configured at 115200 baudrate setting
  Serial.begin(115200);
  //let's test arduino led
  pinMode(13, OUTPUT);
}
void loop(){
  if(Serial.available()){
    //read first characer received
    unsigned char charreceived = Serial.read();
    
    switch(charreceived){
      case 'a':
        digitalWrite(13, HIGH);
        Serial.println("Arduino Led On");
        break;
      case 'b':
        digitalWrite(13, LOW);
        Serial.println("Arduino Led Off");
        break;
      default:
        break;
    }
    
    //flush remaining characters
    //we only want first character
    Serial.flush();
  }
  delay(10);
}
After uploading our code to our Arduino, we are ready to proceed with the bluetooth connection. In our Android device, we must activate bluetooth connections and search for our bluesmirf and pair it, depending on our bluesmirf, it’s name will vary, usually it will be findable with the initials: RN-41 or RN-42, pair code is usually “1234”. I strongly recommend setting our bluetooth module to defaults, check here how to do so. Pairing should present no further problems after a reset.
Now it’s time to truly test it, after pairing we have to open our Blueterm app, in our Blueterm app we have to press an option button, and we’ll see one option called “Connect device”, press it, it should display all our bluetooth paired devices, click on the bluesmirf option (RN-41 or RN-42 usually) and after a couple of seconds we should see in our upper  right corner a message saying “Connected”, now we are able to send characters via our terminal, remember: if we press ‘a’ we’ll turn on Arduino’s led, and if we press ‘b’ we’ll turn it off. Note: remember that not all android devices are Bluetooth SPP compatible, please refer to manufacturer’s product specifications.
Once we are connected to our Bluetooth module, you'll see the green led turned on




Main View of Blueterm App (I used Asus EePad Transformer)

So, that’s it, without much effort, we have our Arduino+Bluesmirf paired and connected with our Android device, now it’s up to us to make more complex code for Arduino, for example: maybe adding more cases in our switch sentence for more leds control, maybe using characters for motor speed control, it’s up to our imagination. It’s pretty easy, we didn’t have to kill ourselves programming in android. Please refer to this page for further bluetooth programming stuff in android.

lunes, 8 de agosto de 2011

Sensor Ultrasónico Maxbotix LM-EZ1 con Arduino

Hoy veremos cómo usar el sensor ultrasónico Maxbotix LV-EZ1 con Arduino. Primero debemos ver el datasheet, para ver que pin corresponde a cada cosa:

Usaremos unos cables hembra-machos para conectar nuestro sensor con nuestro Arduino. El sensor posee varias formas de enviar datos:
  • Salida Serial - 9600bps
  • Salida Analógica - ~9.6mV/inch
  • Salida PWM - 147uS/inch
La manera más sencilla es usando la salida analógica, ya que con sólo conectar el pin AN del sensor a un pin analógico del Arduino y usar la función analogRead() podemos tener una lectura rápida del sensor. El Arduino tiene una resolución analógica de 10 bits(2^10), que es igual a 1023 posibles valores, de manera que como el Arduino trabaja a 5V tendremos que 5V/1023 da a 0.0048V(4.8mV) por unidad.
La salida analógica del sensor tiene una resolución de 9.6mV, entonces si el arduino tiene una resolución de 4.8mV podemos fácilmente inferir que por cada 9.6mV tendremos dos unidades en la lectura analógica, dos unidades equivaldrán a una pulgada. Un ejemplo rápido, hagamos de cuenta que logramos una lectura de 209, para convertir esta lectura a una medida en pulgadas sólo tendremos que dividirla entre dos. El sensor tiene un rango mínimo de 6 pulgadas, estos quiere decir que no detectará nada a menos de 6 pulgadas y siempre el valor mínimo será de 6 pulgadas. Recordemos que una pulgada es igual a 2.54 cm.
No hay que olvidarnos de alimentar al sensor con 5V. Aquí está el código:

//Creado por Henry Serrano 9/8/2011
//Taller Arduino FIUADY

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

void loop(){
  //conectamos el pin AN del sensor
  //al pin A0 del Arduino
  int a = analogRead(0);
  //dividimos entre 2 y nos aseguramos
  //que siga siendo un valor entero
  a = (int)(a/2);
 
  //para imprimir en pulgadas
  Serial.println(a);
 
  //para imprimir en centímetros
  //Serial.println(a*2.54);
 
  delay(80);
}