Publicidad

sábado, 17 de junio de 2017

Módulo Bluetooth

En este primer ejemplo vamos a encender y apagar un motor y a controlar el sentido de giro. Para ello descargamos en Google Play la app BlueTerm de pydasde.es con la que vamos a introducir los comandos de control.

El módulo bluetooth utilizado es el HC-05, lo podeís encontrar en dealxtreme por unos 6-7 euros. La diferencia con el HC-06 es que el primero es maestro-esclavo, mientras que el HC-06 es esclavo.

En DIYmakers hay un estupendo tutorial acerca de estos módulos con un ejemplo sencillo donde se explica como controlar tres leds desde el móvil con la app BlueTerm.


Esquema

Como veis el circuito es muy sencillo, solo hace falta un L293D y el módulo HC-05.


Sketch

El código está basado en un tutorial de Rui Santos y adaptado al circuito anterior.

Antes de cargar el código en el arduino, desconectad los pines para TX y RX ya que son los terminales de comunicación con el PC y si están en uso por el módulo bluetooth dará error al cargar el sketch. Conectar los pines TX y RX del módulo al arduino una vez finalizada la carga del programa.

El funcionamiento es muy sencillo. Conectar el móvil con el módulo HC-05, la contraseña es 1234 y al pulsar el comando correspondiente en el teclado de la app, el motor obrará en consecuencia.


/*
 * Control DC motor with Smartphone via bluetooth
 * created by Rui Santos, http://randomnerdtutorials.com
 * Modificado por angmuz, https://sites.google.com/site/angmuz/
*/

int motorPin1 = 10; // pin 2 on L293D IC
int motorPin2 = 9; // pin 7 on L293D IC
int enablePin = 5; // pin 1 on L293D IC
int state;
int flag=0;        //makes sure that the serial only prints once the state
 
void setup() {
    // sets the pins as outputs:
    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);
    pinMode(enablePin, OUTPUT);
    // sets enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH);
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}
 
void loop() {
    //if some date is sent, reads it and saves in state
    if(Serial.available() > 0){     
      state = Serial.read();   
      flag=0;
    }   
    // if the state is '0' the DC motor will turn off
    if (state == '0') {
        digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
        digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
        if(flag == 0){
          Serial.println("Motor: off");
          flag=1;
        }
    }
    // if the state is '1' the motor will turn right
    else if (state == '1') {
        digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
        digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
        if(flag == 0){
          Serial.println("Motor: right");
          flag=1;
        }
    }
    // if the state is '2' the motor will turn left
    else if (state == '2') {
        digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
        digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
        if(flag == 0){
          Serial.println("Motor: left");
          flag=1;
        }
    }
}




Proyecto 19b - Control motores CC. Parte 2

En este ejemplo vamos a añadir un segundo motor.


Esquema

Sketch

/*
 * Control DC motor with Smartphone via bluetooth
 * created by Rui Santos, http://randomnerdtutorials.com
 * Modificado por angmuz, https://sites.google.com/site/angmuz/
*/

int motor1Pin1 = 10; // pin 2 on L293D IC
int motor1Pin2 = 9; // pin 7 on L293D IC
int enableM1Pin = 5; // pin 1 on L293D IC
int motor2Pin1 = 4; // pin 10 on L293D IC
int motor2Pin2 = 13; // pin 15 on L293D IC
int enableM2Pin = 6; // pin 9 on L293D IC
int state;
int flag=0;        //makes sure that the serial only prints once the state
 
void setup() {
    // sets the pins as outputs:
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    pinMode(enableM1Pin, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(enableM2Pin, OUTPUT);
    // sets enablePin high so that motor can turn on:
    digitalWrite(enableM1Pin, HIGH);
    digitalWrite(enableM2Pin, HIGH);
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}
 
void loop() {
    //if some date is sent, reads it and saves in state
    if(Serial.available() > 0){     
      state = Serial.read();   
      flag=0;
    }   
    // if the state is '0' the DC motor will turn off
    if (state == '0') {
        digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low
        digitalWrite(motor1Pin2, LOW); // set pin 7 on L293D low
        digitalWrite(motor2Pin1, LOW); // set pin 10 on L293D low
        digitalWrite(motor2Pin2, LOW); // set pin 15 on L293D low
        if(flag == 0){
          Serial.println("Motors: off");
          flag=1;
        }
    }
    // if the state is '1' the motors will turn right
    else if (state == '1') {
        digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low
        digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D high
        digitalWrite(motor2Pin1, LOW); // set pin 10 on L293D low
        digitalWrite(motor2Pin2, HIGH); // set pin 15 on L293D high
        if(flag == 0){
          Serial.println("Motors: right");
          flag=1;
        }
    }
    // if the state is '2' the motor will turn left
    else if (state == '2') {
        digitalWrite(motor1Pin1, HIGH); // set pin 2 on L293D high
        digitalWrite(motor1Pin2, LOW); // set pin 7 on L293D low
        digitalWrite(motor2Pin1, HIGH); // set pin 10 on L293D low
        digitalWrite(motor2Pin2, LOW); // set pin 15 on L293D high
        if(flag == 0){
          Serial.println("Motors: left");
          flag=1;
        }
    }
}



Proyecto 19c - Control motores CC y Leds

Ahora añadimos Leds al circuito.


Esquema

Sketch

/*
 * Control DC motor with Smartphone via bluetooth
 * created by Rui Santos, http://randomnerdtutorials.com
 * Modificado por angmuz, https://sites.google.com/site/angmuz/
*/

int motor1Pin1 = 10; // pin 2 on L293D IC
int motor1Pin2 = 9; // pin 7 on L293D IC
int enableM1Pin = 5; // pin 1 on L293D IC
int motor2Pin1 = 4; // pin 10 on L293D IC
int motor2Pin2 = 13; // pin 15 on L293D IC
int enableM2Pin = 6; // pin 9 on L293D IC
int ledRojo = 2; // Led rojo al pin 2
int ledVerde = 3; // Led verde al pin 3
int state;
int flag=0;        //makes sure that the serial only prints once the state
 
void setup() {
    // sets the pins as outputs:
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    pinMode(enableM1Pin, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(enableM2Pin, OUTPUT);
    pinMode(ledRojo, OUTPUT);
    pinMode(ledVerde, OUTPUT);
    // sets enablePin high so that motor can turn on:
    digitalWrite(enableM1Pin, HIGH);
    digitalWrite(enableM2Pin, HIGH);
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}
 
void loop() {
    //if some date is sent, reads it and saves in state
    if(Serial.available() > 0){     
      state = Serial.read();   
      flag=0;
    }   
    // if the state is '0' the DC motor will turn off
    if (state == '0') {
        digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low
        digitalWrite(motor1Pin2, LOW); // set pin 7 on L293D low
        digitalWrite(motor2Pin1, LOW); // set pin 10 on L293D low
        digitalWrite(motor2Pin2, LOW); // set pin 15 on L293D low
        if(flag == 0){
          Serial.println("Motors: off");
          flag=1;
        }
    }
    // if the state is '1' the motors will turn right
    else if (state == '1') {
        digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low
        digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D high
        digitalWrite(motor2Pin1, LOW); // set pin 10 on L293D low
        digitalWrite(motor2Pin2, HIGH); // set pin 15 on L293D high
        if(flag == 0){
          Serial.println("Motors: right");
          flag=1;
        }
    }
    // if the state is '2' the motor will turn left
    else if (state == '2') {
        digitalWrite(motor1Pin1, HIGH); // set pin 2 on L293D high
        digitalWrite(motor1Pin2, LOW); // set pin 7 on L293D low
        digitalWrite(motor2Pin1, HIGH); // set pin 10 on L293D low
        digitalWrite(motor2Pin2, LOW); // set pin 15 on L293D high
        if(flag == 0){
          Serial.println("Motors: left");
          flag=1;
        }
    }
    // if the state is '3' the leds will turn ON
    else if (state == '3') {
        digitalWrite(ledRojo, HIGH); // set Red led ON
        digitalWrite(ledVerde, HIGH); // set Green led ON
        if(flag == 0){
          Serial.println("Leds ON");
          flag=1;
        }
    }
    // if the state is '4' the leds will turn OFF
    else if (state == '4') {
        digitalWrite(ledRojo, LOW); // set Red led OFF
        digitalWrite(ledVerde, LOW); // set Green led OFF
        if(flag == 0){
          Serial.println("Leds OFF");
          flag=1;
        }
    }
}

No hay comentarios.:

Publicar un comentario

Publicidad