Solved Sending bluetooth AT commands

I'm currently using an Ardunio Uno attached to a MLT-BT05 module (I believe, it took some time to figure this out as it was labelled differently where I bought it).
The code that is shown below mostly works, the main bit being when I type AT commands into the serial monitor they come back as expected, for example typing "AT" returns "OK".
My current set-up involves pushing a button which causes the program to write "AT" which should return "OK" (as when I type it in the serial monitor myself). The problem is, it returns "ERROR" instead (the same message it would give me back if I were to type an invalid command such as having a typo).
I've tried using various commands and inputting different text, such as using BTSerial.write as well as BTSerial.print as well as different input such as typing AT as a single string or multiple characters, all to no effect.

The goal of this code is for me to push a button, have the Arduino send "AT" to the Bluetooth module and receive "OK" back.

Any help would be greatly appreciated and thank you for your time.

#include <AltSoftSerial.h>
AltSoftSerial BTSerial;
char c = ' ';
boolean NL = true;
int LEDPin = 10;
int buttonPin = 12;
int buttonVal;
int buttonCheck = 0;


void setup()

{
  pinMode(LEDPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.print("Sketch:   ");   Serial.println(__FILE__);
  Serial.print("Uploaded: ");   Serial.println(__DATE__);
  Serial.println(" ");

  BTSerial.begin(9600);
  Serial.println("BTserial started at 9600");
  Serial.println("");

}

void loop()
{
  while ((Serial.available() > 0) || (BTSerial.available() > 0))    {
    // Read from the Bluetooth module and send to the Arduino Serial Monitor

    if (BTSerial.available())
    {
      c = BTSerial.read();
      Serial.write(c);
    }


    // Read from the Serial Monitor and send to the Bluetooth module
    if (Serial.available())
    {
      c = Serial.read();
      BTSerial.write(c);

      // Echo the user input to the main window. The ">" character indicates the user entered text.
      if (NL) {
        Serial.print(">");
        NL = false;
      }
      Serial.write(c);
      if (c == 10) {
        NL = true;
      }
    }
  }

  buttonVal = digitalRead(buttonPin);


  //write to the BT serial that should return a responce, but it doesnt
  if ((buttonVal == 0) && (buttonCheck == 0)) {
    Serial.println("AT");
    BTSerial.write("AT");
    buttonCheck = 1;
  }
  if ((buttonVal == 1) && (buttonCheck == 1)) {
    buttonCheck = 0;
  }
}

How is your Serial monitor configured? Are you sending CR/LF? What does your module expects as a end of command marker?

try

BTSerial.write("AT\r\n");
1 Like

Ahh okay, that fixed the problem. Thank you :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.