HC-05 serial monitor

Hi! I am really new to Arduino and everything so I don't know all the terms, but I have a color sensor connected to my Arduino Uno and the rgb values show up in the Arduino serial monitor when connected with a usb. However we want it to transfer the data without a usb so we got a HC-05 to connect it. After looking at many tutorials we cannot get anything to work. I can connect it to my MacBook bluetooth but all the terminal emulator apps I have downloaded to see to the color sensor data don't work. Is there anything I am doing wrong, or any apps you recommend?

How did you connect the Bluetooth module to the Arduino.
Can you show your sketch (between code-tags, use the </> button).

// Connect ...
// arduino>>bluetooth
// D11   >>>  Rx
// D10   >>>  Tx
#include <SoftwareSerial.h>// import the serial library

SoftwareSerial Genotronex(10, 11); // RX, TX
int ledpin = 13; // led on D13 will show blink on / off
int BluetoothData; // the data given from Computer

void setup() {
  // put your setup code here, to run once:
  Genotronex.begin(9600);
  Genotronex.println("Bluetooth On please press 1 or 0 blink LED ..");
  pinMode(ledpin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Genotronex.available()) {
    BluetoothData = Genotronex.read();
    if (BluetoothData == '1') { // if number 1 pressed ....
      digitalWrite(ledpin, 1);
      Genotronex.println("LED  On D13 ON ! ");
    }
    if (BluetoothData == '0') { // if number 0 pressed ....
      digitalWrite(ledpin, 0);
      Genotronex.println("LED  On D13 Off ! ");
    }
  }
  delay(100);// prepare for next data ...
}

We did copy it off somewhere but they gave no explanation so I have no idea what any of it means. I have very little experience coding.
I have also tried this code while connected rx to 0 and tx to 1, however I had no luck with it either.

#include <SoftwareSerial.h> 
SoftwareSerial MyBlue(2, 3); // RX | TX 
int flag = 0; 
int LED = 8; 
void setup() 
{   
 Serial.begin(9600); 
 MyBlue.begin(9600); 
 pinMode(LED, OUTPUT); 
 
 Serial.println("Ready to connect\nDefualt password is 1234 or 000"); 
} 
void loop() 
{ 
 if (MyBlue.available()) 
   flag = MyBlue.read(); 
 if (flag == 1) 
 { 
   digitalWrite(LED, HIGH); 
   Serial.println("LED On"); 
 } 
 else if (flag == 0) 
 { 
   digitalWrite(LED, HIGH); 
   Serial.println("LED Off"); 
 } 
}

Let's call the messages to the serial monitor of the Arduino IDE the "debug messages".

The first sketch sends "debug messages" to the Bluetooth module.
The second sketch is better. The Serial.println() is used for the "debug messages" and the MyBlue.read() to read from the Bluetooth module.
You could send everything that is received to the serial monitor, then you know if something is received.

The function Serial.available() or MyBlue.available() returns a number. Therefor I compare it with a number by using: if( Serial.available() > 0)

void loop()
{
  if (MyBlue.available() > 0)
  {
    int inChar = MyBlue.read();

    Serial.print( "Received: ");
    Serial.print( (char) inChar);
    Serial.println();
  }
}

The number 1 in a sketch is the number one.
The character '1' in a sketch is a readable character.
Bluetooth communication is often with readable characters.

if (flag == '1')

To avoid confusion, you can add the word "pin" to a variable that is a pin number. It can be made a 'const', because it does not change.

const int ledPin = 8;

Your variable 'flag' is only used in the loop(), but it is a global variable. It is preferred to make that variable local in the loop() function.

When nothing is received, you don't want the sketch to go nuts. Only when something is received, than some action should be done.

void loop()
{
  if( MyBlue.available() > 0)
  {
    int inChar = MyBlue.read();   // a 'int' or 'byte' or 'char'
    
    if( inChar == '1')
    {
      digitalWrite( LED, HIGH);
      Serial.println( "LED On");
    }
    else if( inChar == '0')
    {
      digitalWrite( LED, HIGH);
      Serial.println( "LED Off");
    }
  }
}

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