Arduino Leonardo and HC 05 bluetooth

Hello,

I have a problem with communication between Arduino Leonardo and HC 05 (zs-040) bluetooth.
I want to control 2 digital outputs via Android phone with ''Arduino bluetooth controller'' application.

If I use an Arduino Uno instead of Leonardo, everything works perfectly.
But when I connect the circuit to Leonardo the bluetooth comunication doesn't work at all. I can see that my phone and HC 05 are connected, but when I type in a command nothing happens.

I couldn't find a useful advice how to solve my problem, so please help me find an error.

Kind regards.

int ledPin = 13;
int state = 0;
int flag = 0;
int LED = 8;
void setup ()
{
  pinMode (ledPin, OUTPUT);
  digitalWrite (ledPin, LOW);
  pinMode (LED, OUTPUT);
  digitalWrite(LED, LOW);
  Serial.begin (9600);
  while (!Serial);
  }
  
  void loop()
  {
    if (Serial.available()>0)
    {
      state = Serial.read();
      }
      
      if (state == '0')
      {
        digitalWrite(ledPin, LOW);
        Serial.println("LED: off");
        state = 0;
        }
          
          if (state == '1')
          {
            digitalWrite(ledPin, HIGH);
            Serial.println("LED: on");
            state = 0;
    }
    
     if (state == 'a')
      {
        digitalWrite(LED, LOW);
        Serial.println("LED 2: off");
        state = 0;
        }
          
         if (state == 'b')
          {
            digitalWrite(LED, HIGH);
            Serial.println("LED 2: on");
            state = 0;
    }
  }

Leonardo has different arrangements for serial. If you change all Serial. commands to

Serial1.

You might find everything is OK. You will see a note to this effect in the Leonardo data sheets, where they allude to the "Serial1 class".

I think this link below will be useful for you:
http://www.martyncurrey.com/connecting-2-arduinos-by-bluetooth-using-a-hc-05-and-a-hc-06-easy-method-using-cmode/

Nick_Pyner thank you for your answer. Now everything works as it should.

See reply #1. For Leonardo, the code is different but the pins are the same. There is no need to programme the HC-05. You may never need to, but you do need to connect it properly, and that may be your problem.

You might find the following background notes useful.

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

This is your posted image of the connections and it shows the HC05 connected to pins 10 and 11.

The Arduino Leonardo board uses Serial1 to communicate via TTL (5V) serial on pins 0 (RX) and 1 (TX). Serial is reserved for USB CDC communication. For more information, refer to the Leonardo getting started page and hardware page.

You need to connect the bluetooth TX to pin 0 and Bluetooth RX to pin 1. The TX and RX designations are from the point of view of the Leonardo. You need to cross connect with Bluetooth module RX to Leonardo TX and Bluetooth module TX to Leonardo RX.

juancdrg5:
not works, i also connected rx and tx to pin 0 and 1 respectively

What I have given you works. Read reply #3.

If you are using pins 0,1, I suggest you stick with that.

If you change the pins, you must change the code. Hardware serial(just plain Serial.print) on pins 0,1, Software Serial on other pins. This applies to Leonardo, just like all the others. Also make sure you connected them the right way round Tx>Rx Rx>Tx. What you have said above is the wrong way round.

Further, the diagram you show is not a good idea, but probably not fatal. It is a good idea to use a voltage divider, as shown in the notes.

Also with HC 06 ...Nick_Pyner is correct and also....the Pro Micro board, which is a clone and based on the Arduino Leonardo, but much smaller than Leonardo and about $4.50 off Ebay from China ; this programs the same for bluetooth as the Leonardo. You switch "Serial" to "Serial1" in all cases in your code. Note: I am referring to the HC 06 (4 pins) bluetooth module. Here is my code and info.BlueToothHC06ProMicro.ino - Google Drive

#include "Arduino.h"
HardwareSerial& bthc05(Serial1);

void setup()
{
bthc05.begin(9600);
bthc05.println("Bluetooth On....");

}
void loop()
{

while (bthc05.available()>0)
{
char c = bthc05.read();

if(c == '1')
Serial.println(F("Now Testing HC - 05 Bluetooth Serial Module"));
else
{
Serial.println(F("illegal input!"));
return 0;
}

}
}