Bluetooth communication from a Mac to Arduino UNO HC-05

I got an HC-05 bluetooth module from an Arduino kit on Amazon, from OddWires. Im trying to use it with my Arduino Uno board to control a LED at first. So I wired my BT-UNO like this:

BT_HC-05-------ARDUINO_UNO Tx------------------Rx Rx------------------Tx Vcc-----------------5V GND----------------GND

Then I use this sketch which I upload to the UNO:

int ledpin=13; // led on D13 will show blink on / off
int BluetoothData; // the data given from Computer

void setup() {
  Serial.begin(9600);
  Serial.println("Bluetooth On please press 1 or 0 blink LED ..");
  pinMode(ledpin,OUTPUT);
}

void loop() {
 if (Serial.available()>0){
   BluetoothData=Serial.read();
   Serial.println(BluetoothData);
   if(BluetoothData=='1'){   // if number 1 pressed ....
     digitalWrite(ledpin,1);
     Serial.println("LED  On D13 ON ! ");
   }
  if (BluetoothData=='0'){// if number 0 pressed ....
    digitalWrite(ledpin,0);
    Serial.println("LED  On D13 Off ! ");
  }
}
delay(100);// prepare for next data ...
}

After uploading the sketch (with the Tx/Rx cables disconnected from the UNO board), I disconnect the USB from the UNO and plug in the 9V battery pack to the UNO which powers up. Both BT and UNO light up, but nothing happens when entering 1 or 0 to the Serial Monitor.

I did connect to the BT module via my Mac, it asked me for the pairing code, I entered 1234, it then displayed "Connected" on my Mac but nothing happened after entering 1 or 0. Then all of a sudden the Mac displays that the HC-05 is Not Connected anymore.

Is this a known issue or am I doing something wrong?

I wrote to OddWires and asked them and they referred me to this tutorial:

which uses a Software Serial library. The tutorial actually states that the writer was unable to make the BT module work by connecting the wires to Tx and Rx as is customary. So he used this library which makes use of other digital pins as Tx and Rx pins. It uses pin 10 as Rx and 11 as Tx. This means it connects BT-Tx to Arduino Rx-10 and vv. But the MT module keeps getting disconnected after pairing to the mac. It remains connected to my Mac for about 5-9 seconds. In that time I send a 1 and or 0 thru the serial terminal but the LED doesn’t light up.

Ive attached pictures and a video link

Anybody experienced this before?

Thanks

IMG_5722.JPG

IMG_5723.JPG

IMG_5724.JPG

Got it working.

I guess it didnt work with the Serial Monitor on the Arduino IDE. I used screen on Mac terminal and it worked fine! :-)

One more thing.

How come the data logged in the terminal, is this:

Bluetooth On please press 1 or 0 blink LED .. 49 LED On D13 ON ! 48 LED On D13 Off ! 49 LED On D13 ON ! 48 LED On D13 Off !

I was logging the Serial.read() values. I was entering 0 and 1, where 1 caused the ON sentence to be logged while 0 caused the Off to be logged, but this is what was actually being logged as the value gotten from Serial.read(). Why 48 and 49?

Please help i don't know how to make the connection stick, it looses it every time

Marciokoko: One more thing.

How come the data logged in the terminal, is this:

Bluetooth On please press 1 or 0 blink LED .. 49 LED On D13 ON ! 48 LED On D13 Off ! 49 LED On D13 ON ! 48 LED On D13 Off !

I was logging the Serial.read() values. I was entering 0 and 1, where 1 caused the ON sentence to be logged while 0 caused the Off to be logged, but this is what was actually being logged as the value gotten from Serial.read(). Why 48 and 49?

I had a similar problem once, I am not sure if you are still having a problem with this or no. But I am going to post here for future references.

The 48 and 49 are the equivalent of 0 and 1 in the ASCII table. try sending 2 and you will seeing the number 50 pop up, same with letters (of course different numbers). For me I just moved from serial connection to I2C. I do not know if you can do that, but if you can it is very easy, there are library already inside of adruino IDE

I had this problem, check out this video:

https://www.youtube.com/watch?v=0TzYDOIaDYA&t=241s

fixed my connection issue