1

So I'm trying to learn basic Android app dev. My master plan was to make an app using MIT App Inventor that would turn on an LED over Bluetooth using an Arduino. Once I verified that everything worked, I would write the entire app from scratch using Android Studio. This way, when I inevitably run into bugs with my written-from-scratch app, I could say with 100% certainty that the issue is in my Android code and not hardware/Arduino related.

So I'm having trouble right now. I have two separate LED circuits, a blue LED from port 13 and a green LED from pin 8. I verified that both circuits are working properly by using the Blink code, so I know the issue is definitely not with the LED circuits.

I'm using an HC 06 Bluetooth module. The pins are connected as follows:

HC 06 -> Arduino

RX -> TX

TX -> RX

5V -> 5V

GND -> GND

Here is the Arduino code:

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(0,1);
int ledblue = 13;
int ledgreen = 8;
int tx = 1;
int rx = 0;
int type;

void setup() {
  Serial.begin(9600);
  //bluetooth.begin(9600);
  pinMode(ledblue, OUTPUT);
  pinMode(ledgreen, OUTPUT);
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);
  allpinslow();
}

void loop() {
  int i = 0;
  int m = 0;
  delay(500);
  if (Serial.available() > 0) {
    while(Serial.available() > 0){
      type = Serial.read();
      Serial.println(type);
    }
    Check_Protocol(type);
  }
}

void allpinslow()
{
  digitalWrite(ledgreen, LOW);
  digitalWrite(ledblue, LOW);
}

void Check_Protocol(int inStr) {
  int i = 0;
  int m = 0;
  if (inStr == '0') { //Led Off equals("2off")
    allpinslow();
  }
  if (inStr == '1') { //Led on
    digitalWrite(ledblue, HIGH);
  }
  if (inStr == '2'){
    digitalWrite(ledgreen,HIGH);
  }
}

Here is the MIT App Inventor block code

MIT App Inventor block code

If I press the Green or Off button, the Serial Monitor spits out 255. If I press Red, nothing shows up on the serial monitor. I'm a bit dumbfounded on what is broken so I would greatly appreciate any help I can get.

Edit: @Delta_G here is the modified code

#include <SoftwareSerial.h>
int ledblue = 13;
int ledgreen = 8;
SoftwareSerial mySerial(2,4); //RX, TX
int type;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(ledblue, OUTPUT);
  pinMode(ledgreen, OUTPUT);
  allpinslow();
}

void loop() {
  int i = 0;
  int m = 0;
  delay(500);
  if (mySerial.available()) {
    while(mySerial.available()){
      type = mySerial.read();
      Serial.println(type);
    }
    Check_Protocol(type);
    //Serial.println(inSerial);
  }
}

void allpinslow()
{
  digitalWrite(ledblue, HIGH);
  digitalWrite(ledblue, LOW);
}

void Check_Protocol(int inStr) {
  int i = 0;
  int m = 0;
  if (inStr == '0') { //Led Off equals("2off")
    digitalWrite(ledblue, LOW);
    digitalWrite(ledgreen, LOW);
  }
  if (inStr == '1') { //Led on
    digitalWrite(ledblue, HIGH);
  }
  if (inStr == '2'){
    digitalWrite(ledgreen,HIGH);
  }
}
  • 1
    You have a big problem here: you are using both software and hardware serials at once. I suggest you to use only the hardware serial. To do so, remove the lines #include <SoftwareSerial.h> and SoftwareSerial bluetooth(0,1);. Then tell where is the serial terminal attached (to the arduino board?) and, finally, if it still doesn't work try swapping the RX and TX wires – frarugi87 Jun 19 '17 at 8:18
  • The HC 06 RX pin is attached to pin 1, the HC 06 TX pin is attached to pin 0. I got rid of the two lines of code, but the same exact result still occur. Swapping the pins won't work-the phone will refuse to connect to the module – theflyingoreo Jun 19 '17 at 23:35
  • Apologies for the double comment - ". Then tell where is the serial terminal attached (to the arduino board?) " I set pin 0 (the Arduino's RX pin) to input and pin 1 (the Arduino's TX) pin to output - shouldn't this tell the Arduino where the serial terminal is attached? – theflyingoreo Jun 19 '17 at 23:45
  • Your code is referencing the "Serial" object which refers to the hardware UART, not the "bluetooth" object which is a softare serial instance on the now hopefully de-conflicted pins. – Chris Stratton Jun 20 '17 at 1:49
  • Hi, I attempted to use SoftwareSerial instead. I added the updated code to my original post. All three buttons on my app still return "255" in the serial monitor. If you could check the updated code out, I'd greatly appreciate it. Thanks! – theflyingoreo Jun 20 '17 at 4:23
1

If you are using Serial then you can't use pins 0 and 1 for something else as those are the hardware serialpins. Pick different pins for the Bluetooth.

  • If I use softwareserial and while attaching the HC 06 TX to pin 4 and HC 06 RX to pin 2, I get the same results (255) – theflyingoreo Jun 20 '17 at 0:55
  • 255 is diagnostic. That's what you'd get if you called read() when there is no data available. – Delta_G Jun 20 '17 at 1:52
  • But for the life of me I can't figure out why it's not getting any data. If you look at the app's block code, it is clearly sending 48, 49, and 50 yet the Arduino isn't getting anything – theflyingoreo Jun 20 '17 at 2:35
  • Show the new code with the new pins. Who is doing the sending? The bluetooth? Are you reading the software serial instance? Or are you just reading from Serial like in the code you posted? – Delta_G Jun 20 '17 at 2:41
  • I added the new code to my original post – theflyingoreo Jun 20 '17 at 3:11
1

From reading the comments, I assume you are using an Arduino Uno, and you have a PC (or Mac, or any type of computer) connected to its USB port, and an Android device at the other end of the Bluetooth link.

I can't see what is wrong with your sketch. I will then only suggest two methods for checking the low-level communication is working properly. Once you know the communication is good, debugging should be easier.

Test bypassing the microcontroller

First, load an empty sketch in the Arduino, then connect the Bluetooth module as follows:

HC 06   Arduino
───────────────
 GND ─── GND
  5V ─── 5V
  RX ─── RX
  TX ─── TX

It is important to load the empty sketch first: you do not want the Arduino to access its hardware serial port. Now you can open your serial monitor on the PC, set to 9600/8N1.

With this setup, the HC 06 will be using the USB/serial bridge of the Arduino (the ATmega16U2), whereas the main microcontroller (the ATmega328P) is just a bystander. Whatever the HC 06 outputs through its TX should appear on your serial monitor. The data path is as follows:

┌─────────┐  Bluetooth  ┌───────┐  Serial  ┌──────┐     ┌──────┐  USB  ┌────┐
│ Android │ ──────────→ │ HC 06 │ ───────┐ │ 328P │ ┌─→ │ 16U2 │ ────→ │ PC │
└─────────┘             └───────┘        │ └──────┘ │   └──────┘       └────┘
                                         └──────────┘

Note the 328P disconnected from the serial link.

Test with an echo sketch

Load the following sketch:

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    if (Serial.available())
        Serial.write(Serial.read());
}

then connect the Bluetooth module as follows:

HC 06   Arduino
───────────────
 GND ─── GND
  5V ─── 5V
  RX ─── TX
  TX ─── RX

With this setup, whatever the HC 06 outputs on its TX should appear on your serial monitor and should be sent back through Bluetooth. The data path is:

┌─────────┐  Bluetooth  ┌───────┐  Serial  ┌──────┐     ┌──────┐  USB  ┌────┐
│ Android │ ──────────→ │ HC 06 │ ───────→ │ 328P │ ┌─→ │ 16U2 │ ────→ │ PC │
│         │ ←────────── │       │ ←──────┐ └──────┘ │   └──────┘       └────┘
└─────────┘             └───────┘        └─────┴────┘

Note that the output of the 328P is received by both the 16U2 and the HC 06. Since the Android device seems to ignore whatever it receives through Bluetooth, the setup should work fine for debugging you app too.

  • For the first method, I loaded an empty sketch, reconnected the HC 06, and opened the Serial Monitor in the IDE. Nothing happened. I connected to the HC 06 via Bluetooth on a second computer, opened TeraTerm VT, set it to 9600/8N1, tried pressing the buttons on the app. Nothing happened. None of the buttons I pressed elicited a response. For the second method, I uploaded the echo sketch and opened the Serial Monitor again. This time, all I got was one backward question mark each time I pressed a button imgur.com/a/I1qaP Is it possible the module is faulty? – theflyingoreo Jun 20 '17 at 13:56
  • Then I would conclude that you have a communication problem upstream of the Arduino. – Edgar Bonet Jun 20 '17 at 14:00
  • So it potentially could be a busted Bluetooth module? The problem with the app is that this is a dummy app written in MIT App Inventor. There is no way for me to jump into the source code and verify that everything is actually correct. I essentially have to assume that the App Inventor compiled the app properly. – theflyingoreo Jun 20 '17 at 14:09
  • I cannot say, I do not know how the Bluetooth module is supposed to work, or how you can configure it. The experiment only proves it is not sending what you expect, so it's not your Arduino sketch that is faulty. If you have a scope, you could try to look at the module's TX when sending data from your app. – Edgar Bonet Jun 20 '17 at 14:45
  • I used Step 2 from here to try and test my BT module: create.arduino.cc/projecthub/mjrobot/… There was no response to any AT commands. Is this a possible indication of a DOA module? – theflyingoreo Jun 20 '17 at 18:50
1

Update: It was a faulty Bluetooth module that was causing the issues. I bought a new one and everything works perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.