Hi, for the project I’m working on, I’m required to use a bluetooth module with a Seeeduino XIAO.
I’m testing the components by sending “A” and “B” by keyboard (through CoolTerm) with the RN42 so that a simple vibromotor connected to the Seeeduino gets activated.
At the beginning I tried this code on a Arduino Uno and it worked perfectly:
//REMEMBER AT FIRST YOU LOAD THE SKETCH WITHOUT TX AND RX THEN YOU CONNECT RX AND TX
void setup() {
pinMode(9, OUTPUT);
// set baudrate to match BT module:
Serial.begin(115200);
}
void loop() {
String t;// String to hold data from BT module:
while(Serial.available()){// keep reading bytes while they are still more in the buffer
t+=(char)Serial.read(); // read byte, convert to char, and append it to string
}
if(t.length()){ //if the string is not empty do the following
if(t == "A\r\n"){
Serial.print("Brrrr!\n");
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
}
else if(t == "B\r\n"){
Serial.print("Brr Brr\n");
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
delay(500);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9, LOW);
}
delay(1000);
}
delay(20);
}
I simply connected the module to 3.3 V, GND, RX and TX of Arduino Uno board, I used the following options on CoolTerm and everthing worked.
I tried to repeat the same procedure on the Seeeduino, but when I type “A” or “B” on CoolTerm nothing happens.
Someone told me that probably by connecting the Seeeduino with the USB- type C cable the port gets busy, so I tried using a software serial:
//REMEMBER AT FIRST YOU LOAD THE SKETCH WITHOUT TX AND RX THEN YOU CONNECT RX AND TX
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10); // RX, TX
void setup() {
pinMode(6, OUTPUT);
// set baudrate to match BT module:
mySerial.begin(115200);
}
//void loop()
//{
// if (mySerial.available())
// Serial.write(mySerial.read());
// if (Serial.available())
// mySerial.write(Serial.read());
//}
void loop() {
String t;// String to hold data from BT module:
while(mySerial.available()){// keep reading bytes while they are still more in the buffer
t+=(char)mySerial.read(); // read byte, convert to char, and append it to string
}
if(t.length()){ //if the string is not empty do the following
if(t == "A\r\n"){
mySerial.print("Brrrr! \n");
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
}
else if(t == "B\r\n"){
mySerial.print("Brr Brr\n");
digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, LOW);
delay(500);
digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, LOW);
}
delay(1000);
}
//else{
// Serial.print("Syntax Error\n");
// }
delay(20);
}
but nothing changes.
I also tried to avoid using the USB cable and I gave power to the Seeeduino through Arduino Uno by connecting the 5 V pins and GND with the first code I posted, but it still doesn’t work.
What is the problem? Did I use the sofware serial correctly?