Arduino + HC-05 + App Inventor

Hello guys,

I have some problems with my Android phone and HC-05. I am working on a arduino project and theirefor I need to connect my phone to my arduino (I am using an Arduino Uno). I found multiple tutorials on the web where they make a simple ON/OFF led controll with arduino using a HC-05 and App Inventor. Since it's exactly what I need, I tried it out.

I made the app with App Inventor. I wrote a program for the arduino. When I run the app, I can connect to my bluetooth HC-05 and I can press the button, but my led doesn't go OFF or ON. My led is even burning constantly.

What am I doing wrong? Hope you'll help me, Thank you.

Here is the arduino code:

int data;
int led = 13;

void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);   
}

void loop() {
  if(Serial.available()>0);{
    data = Serial.read();
    if(data = '1');{
      digitalWrite(led, HIGH);
    }
    if(data = '0');{
      digitalWrite(led, LOW);
    }
  }
}

if(data = '1');
if(data = '0');

Double error.

== not = for comparison

No semicolon after conditional statement

if(data == '1')
 if(data == '0')

Loose this semicolon as well

if(Serial.available()>0);

TorbenO:
When I run the app, I can connect to my bluetooth HC-05

This merely means that you have done what you have said you have done, and it guarantees nothing as far as Arduino is concerned - and not your app inventor stuff either.. Maybe the real intention of your project is the app inventor stuff but, either way, you don't actually need it, and you would be better off using an ordinary bluetooth terminal to start with, thereby removing a potential problem from the game.

If, after going through reply#1, you still have no joy, 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

Note that getting Bluetooth going is a much a matter of procedure as programming.

cattledog:
== not = for comparison
No semicolon after conditional statement
Loose this semicolon as well

Thank you for responding, I changed all you said, but when I try with the app, the led stays off.

Nick_Pyner:
Maybe the real intention of your project is theapp inventor stuff but, either way, you don't actually need it, and you would be better off using an ordinary bluetooth terminal to start with, thereby removing a potential problem from the game.
http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

I see what you mean, so I installed bluetooth terminal on my android phone. I took the program from the second link. When I connect with the bluetooth terminal to my HC-05, I can everything I write in the serial monitor on my computer see in the app. As far as I understand, I also need to be able to see the text from the serial monitor on the bluetooth terminal app. (in this case is it: OK then, you first, say something.....). But I don't see any text, only the text I type in the serial monitor.
When I type 1 or 0 in the serial monitor, the led goes on and off so then it works, but when I typ 1 or 0 in the bluetooth terminal, nothing happens.

but when I typ 1 or 0 in the bluetooth terminal, nothing happens.

Your code only looks at the Serial input. How have you wired up your Bluetooth adaptor?
The normal way is to use software serial, so you need to use that in the Arduino’s code.

TorbenO:
I took the program from the second link. When I connect with the bluetooth terminal to my HC-05, I can everything I write in the serial monitor on my computer see in the app. As far as I understand, I also need to be able to see the text from the serial monitor on the bluetooth terminal app. (in this case is it: OK then, you first, say something.....). But I don't see any text, only the text I type in the serial monitor.
When I type 1 or 0 in the serial monitor, the led goes on and off so then it works, but when I typ 1 or 0 in the bluetooth terminal, nothing happens.

I think you might be mixing code - or trying to think about it. IF you use the code in the second link, the objective is only to connect Arduino <> Android, and send text two-way. No amount of typing 0 or 1 will flash any LEDs. The serial monitor is just a passenger and need not be connected at all. You should see "OK, you first..." on Android and you say something there, using the standard terminal. If you don't see anything on Android, you have a problem, probably Bluetooth wired wrong way round.

Read the notes, and look at the pictures.

Note particularly that you can run the programme first using the serial monitor, ensure that all is kosher, then connect bluetooth with no change to the code. If no joy, you know where the problem isn't, and it isn't too hard to find where it is.

Nick_Pyner:
I think you might be mixing code - or trying to think about it.

I indeed mixed the test code and the actual code for my led.

Nick_Pyner:
If you don't see anything on Android, you have a problem, probably Bluetooth wired wrong way round.

Grumpy_Mike:
How have you wired up your Bluetooth adaptor?

I didn't use sofware serial since I didn't think it was necessary, so I used the standard serial connection: RX from the HC-05 to RX (port 0) form the arduino, idem for the TX (port 1). But since that didn't work I started to doubt on my wiring like you said. So I tried to change TX with RX, now is RX connected to port 1 en TX connected to port 2. How strange it sounds, when I connect the HC-05 like that I now see the text "OK, you first.." on my Android.
I tried also the led program and even that works. I find it strange you don't have to connect RX to RX and TX to TX. Does someone know why that is?

;D Already thank you very much to everyone for helping me out. I can now start on my actual project. When I need more help I'll not doubt to ask it. :smiley:

tried also the led program and even that works. I find it strange you don't have to connect RX to RX and TX to TX. Does someone know why that is?

You have discovered that they need to be cross connected Rx>Tx and Tx>Rx.

The Rx/Tx designators relate to the device they connect to. Think about it and you'll see that output Tx from the module has to connect to input Rx of the Arduino and vica versa..

R stands for receiver T for transmitter.
So you need Rx connected to Tx so you have the Recever "listening" to the Transmitter, and vica versa Transmitter "talking" to the Receiver

I find it strange you don't have to connect RX to RX and TX to TX. Does someone know why that is?

Because you should always connect an input to an output, so connecting RX to RX on the face of it is the stupid thing to do. But by an odd quirk this is sometimes what you have to do.

This means that the RX pin can be an input or an output depending on how the piece of equipment is classed. The same goes for the TX pin.

Equipment can be classed as either DTE (Data Terminal Equipment) or CTE (Computer Terminal Equipment)

TorbenO:
I didn't use sofware serial since I didn't think it was necessary, so I used the standard serial connection:

That's fine, it isn't. Indeed, it can be a liability. Just remember that the pins are indeed clearly named, and transmitters (Tx) transmit to receivers, while receivers (Rx) receive from transmitters.

It actualy makes sence now. Receiver to transmitter and transmitter to recveier. I didn't think about it like that.

Thanks a lot for explaining guys. Now I know what I did wrong, so I definitly won't do it wrong the next time!

Thanks again!

While you are at it, you might note that a 1k/2k voltage divider in the Arduino Tx line is a good idea.

Now I know what I did wrong, so I definitly won't do it wrong the next time!

Remember in reply #9 you might have to do things wrong. It is always worth a check with an LED to see what any particular piece of equipment has as its output. I always do this with a new piece of equipment with an oscilloscope.

So when connecting a CTE to a CTE or connecting a DTE to a DTE then you need to do it "wrong", that is RX to RX and TX to TX.

Hello guys,

I read this topic, however my problem is with sending button status ON/OFF from Arduino to MIT APP.
I have below code, where it's not work properly with MIT app. When HC-05 is connected and I click ON in app the status not show at the first click, it is shown after second click. Than, when I click third time ON button in app, it change status from ON to OFF ... I don't understand why it happens.

#define ledPin 7
 int state = 0;
 
void setup() {
 Serial.begin(9600); // Default communication rate of the Bluetooth module 
 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);

}


void loop() {
  
 if(Serial.available() > 0){ // Checks whether data is comming from the serial port
 state = Serial.read(); // Reads the data from the serial port

 if (state == '0') {
 digitalWrite(ledPin, LOW); // Turn LED OFF
 Serial.println("ON"); // Send back, to the phone, the String "LED: ON"
 state = 0;
 }
 else if (state == '1') {
 digitalWrite(ledPin, HIGH);
 Serial.println("OFF");
 state = 0;
 } 
}
}

If you have an Arduino + HC-05, I would debug using a Bluetooth Terminal app.

There are several in the Google Play Store.

.