Connect arduino to MIT APP inventor via bluetooth

Good morning

I want to connect the MIT APP inventor to Arduino. I am using HC-06 bluetooth module. There are four connection on the bluetooth module VCC, GND, RX and TX. I used a voltage divider for the RX pin of the bluetooth HC-06 and I am using pins 0 and 1 on Arduino for TX and RX communication.

I downloaded the below code in Arduino

#define ledPin 13
int state = 0;

void setup() {
 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);
 Serial.begin(38400); // Default communication rate of the Bluetooth module
}

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("LED: OFF"); // Send back, to the phone, the String "LED: ON"
 state = 0;
 }
 else if (state == '1') {
 digitalWrite(ledPin, HIGH);
 Serial.println("LED: ON");;
 state = 0;
 } 
}

I tried MIT code following the tutorial in the link below. The bluetooth is connected but the internal LED of arduino is not controlled by the application.
I added images of the circuits and the built code. Also, the serial mointor in Arduino does not receive any information from the application. Can you assist please

https://howtomechatronics.com/tutorials/arduino/how-to-build-custom-android-app-for-your-arduino-project-using-mit-app-inventor/




You started a topic in the Uncategorised category of the forum

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

Take a look immediately under the heading of this topic

image

maybe worth printing the received data

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

to check what is being received
have you tried send 0 and 1 from a terminal program? e.g. Android serial_bluetooth_terminal

Does the arduino code behave as expected when you disconnect the Bluetooth module and use serial monitor?

Yes I tried the blink example and I worked

I print the state in the serial mointor but nothing is displayed

yes but it's not controlling the internal led of arduino

Why? This prevents you from any kinda debug serial printing (and opening Serial Monitor will disrupt the communincation).
Use SoftwareSerial library to connect HC06 to any other pins pair, then you can use Serial Monitor for any status and debug printing...

How to modify the code to include softwareSerial library. I did not use this library before

Thanks in advance

I tend to use AltSoftSerial - see sample code

No it isn't. It is 9600.
Make sure your connections are Rx to Tx and Tx to Rx.

Check this out (connect BT TXD to pin 8 and RXD to 9, btw 9600 baud is the default speed...):

#include <SoftwareSerial.h>

#define ledPin 13
#define BT_TX 8
#devinf BT_RX 9

int state = 0;

SoftwareSerial Bluetooth(BT_TX, BT_RX);

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);
  Bluetooth.begin(9600);
  Serial.println("STARTED");
}

void loop() {
 if(Bluetooth.available() > 0) {
    state = Bluetooth.read(); // Reads the data from the serial port
    Serial.print("state=");Serial.println(state);
    switch (state) {
      case '0':
        digitalWrite(ledPin, LOW); // Turn LED OFF
        Serial.println("  LED: OFF"); // Send back, to the phone, the String "LED: ON"
        break;
      case '1':
        digitalWrite(ledPin, HIGH);
        Serial.println("  LED: ON");;
        break;
    }
}

I see a resistor there, brown-black-yellow.
That's 100kΩ → improper value.

I changed the resistor value

Thank you docdoc. The code is working now.
The application controls the internal led of arduino.
There is one more issue the application does not show "LED ON" or "LED OFF"
Can you assist please. Thanks in advance

does the serial monitor display anything?
make sure the baudrate of the microcontroller (9600) and Serial monitor is the same?

Looks like you either aren't sending the required characters, btw you should see the "state=" line showing what has been received. What does it show? If you want to see exactly the byte value change that line to:

    Serial.print("state=");Serial.print(state);Serial.print(" 0x");Serial.println(state, HEX);

and copy/paste here the results.
You can also remove the useless double semicolon I mistakingly put at the end of "LED: ON".

Anyway, even if I think it should give the same results, on an old project I made with AppInventor sending commands to Arduino, I see I used "SendText", not "Send1ByteNumber":

image

Give it a try.

To what?

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