Communicating data from Arduino to Pc via HC-05

Hi, I have been able to get the Hc-05 communicating with the serial to turn a light on via Bluetooth when I type a number into the serial. I would now like to be able to send data from my Arduino to the serial port via Bluetooth. I have tried and I cant seem to get digital.read working for the pins when communicating through the Hc-05. And assistance would be great. All I want is to get a button to communicate back to the serial port.


/* Code written by Chams for Youtube Channel - That'sEngineering
    13/2/2020
    Youtube video: HC-05 BT module, interfacing with Arduino (via Computer)

*/

#include <SoftwareSerial.h>

char value;
int Tx = 2;       // connect BT module TX to 2
int Rx = 3;       // connect BT module RX to 3

const int LED = 7;
const int buttonPin = 4;

int buttonState = 0;


// creates a "virtual" serial port/UART
SoftwareSerial bluetooth(Tx, Rx);

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(buttonPin, INPUT);

  // start serial communication at default baud rate 9600bps
  Serial.begin(9600);
  bluetooth.begin(9600);

}

void loop() {

  if (bluetooth.available()) {

    value = bluetooth.read();

    buttonState = digitalRead(buttonPin);

    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (buttonState == HIGH) {  // turn LED on:
      digitalWrite(LED, HIGH);
      Serial.println("ON");
    } else {
      // turn LED off:
      digitalWrite(LED, LOW);
      Serial.println("OFF");
    }


    if (value == 1) {  // turn LED on:
      digitalWrite(LED, HIGH);
      Serial.println("ON");
    }

  }
}

Post your code. Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Tell us what your code actually does and how that differs from what you want.

Post a schematic.

Have you paired the HC05 to the PC?

Have you connected the HC05 to a Bluetooth app on the PC? What app?

Hi I have uploaded the code with my initial post. So my code at this point allows me to enter a 1 into the serial and via Hc-05 it will turn a led on on my Arduino.

I have connected the Hc05 to my pc, but have not used a Bluetooth app just the Bluetooth settings on my computer.

I would like to be able to click the button on my Arduino and send data to my pc, printing "on" in the serial whilst also turning on the led. I think my issue is that I have got the pc communicating data to the Arduino but not vice versa. Any assistance would be great thank you

The SoftwareSerial pins will be RX pin 2 and TX pin 3. That is how the constructor works. Look at the SoftwareSerial reference.

Waiting on that schematic.

The way that it works is, you wire the HC05 to the Arduino HC05 RX to Arduino TX and HC05 TX to Arduino RX. Next pair the HC05 to the PC. Then connect the HC05 to an app on the PC. I like to use the serial Bluetooth terminal app for testing.

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