14. Where do we connect the Bluetooth module in Arduino UNO?
p9A0i_bluetooth_Serial.aia (2.9 KB)
-
Terminals 0 (RX) and 1 (TX) are used by Arduino as the default Serial RX/TX. Use it to upload sketch, Serial Monitor, Bluetooth. So when we are going to upload a sketch for Bluetooth we must disconnect the RX cable from the Arduino.
-
We can use other terminals to connect the Bluetooth module, for example 10 and 11, in this case we need the “SoftwareSerial” library. Now when we upload a sketch it is not necessary to remove the RX cable.
- CODE FOR MODULE IN default RX/TX pin 0 and 1 of Arduino.
// Juan A. Villalpando
// http://kio4.com/appinventor/9A0_Resumen_Bluetooth.htm
#define Pin13 13
char caracter;
void setup() {
Serial.begin(9600);
pinMode(Pin13, OUTPUT);
}
void loop() {
if(Serial.available()) {
caracter = Serial.read();
if(caracter == 'a'){ digitalWrite(Pin13, HIGH);}
if(caracter == 'b'){ digitalWrite(Pin13, LOW);}
Serial.println(caracter);
}
}
- CODE FOR MODULE IN pin10 and pin11 with SoftwareSerial library.
// Juan A. Villalpando
// http://kio4.com/appinventor/9A0_Resumen_Bluetooth.htm
#include <SoftwareSerial.h>
SoftwareSerial I2CBT(10,11);
// El TX del módulo BT va al pin10 del Arduino
// El RX del módulo BT va al pin11 del Arduino
#define Pin13 13
char caracter;
void setup() {
I2CBT.begin(9600); // To read and write Bluetooth
Serial.begin(9600); // To print in Serial Monitor
pinMode(Pin13, OUTPUT);
}
void loop() {
if(I2CBT.available()) {
caracter = I2CBT.read();
if(caracter == 'a'){ digitalWrite(Pin13, HIGH);}
if(caracter == 'b'){ digitalWrite(Pin13, LOW);}
Serial.println(caracter); // Serial Monitor
I2CBT.println(caracter); // return Bluetooth
// I2CBT.write(caracter); // return Bluetooth
}
}