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
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);
}
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...
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
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:
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":