dont know how to use bluetooth module hc-05

Status
Not open for further replies.
i tred this code and it doidnt worked.
i use this code
void setup() {
Serial.begin(38400); // im not sure for this
}
void loop() {
if(Serial.available() > 0){
Serial.println(Serial.read());
}
}
and it prints ascii code.
int led=13;
int state;
void setup() {
pinMode(led,OUTPUT);
Serial.begin(38400); // im not sure for this
}
void loop() {
if(Serial.available() > 0){
state=Serial.read();
}
if (state == 48){
digitalWrite(led,LOW);
}
if (state == 49){
digitalWrite(led,HIGH);
}
Serial.println(state);
}
i used this code and everything is ok.
but when i want to do something more comlex i cant becuse ascii code
 
Tty this
AS is and say what happens
Code:
#define rxPin 2
#define txPin 3
#define ledPin 7
#include <SoftwareSerial.h>
SoftwareSerial mySerial(rxPin, txPin);
int state = 0;
void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  mySerial.begin(9600); // Default communication rate of the Bluetooth module
}
void loop() {
  if(mySerial.available() > 0){ // Checks whether data is comming from the serial port
    state = mySerial.read(); // Reads the data from the serial port
 }
 if (state == '0') {
  digitalWrite(ledPin, LOW); // Turn LED OFF
  mySerial.println("LED: OFF"); // Send back, to the phone, the String "LED: ON"
  state = 0;
 }
 else if (state == '1') {
  digitalWrite(ledPin, HIGH);
  mySerial.println("LED: ON");;
  state = 0;
 } 
}

don't change it Use just as is
 
You type a 1 send it the led comes on you type a 0 send that led goes off if your using a bluetooth terminal
and using the stuff I showed you.

Now your HC-05 maybe set to use 38400 that depends on how you messed with it.
 
doesent work
That doesn't tell me nothing did you hook it up like i posted in post 26 ???

That code works only for that way

If you hooked it up like that and used the code I posted your HC-05 maybe still set for 38400
 
If one of those don't work you have sent at commands and changed your HC-05 settings and it will need to be resetup
 
this code works as i sayd
int led=13;
int state;
void setup() {
pinMode(led,OUTPUT);
Serial.begin(38400); // im not sure for this
}
void loop() {
if(Serial.available() > 0){
state=Serial.read();
}
if (state == 48){
digitalWrite(led,LOW);
}
if (state == 49){
digitalWrite(led,HIGH);
}
Serial.println(state);
}
becuse 48 ascii code means 0 and 49 means 1
 
why you always want to add this to code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(rxPin, txPin);
why just don use built in tx rx
 
SoftwareSerial mySerial does what you want

Using the hardware serial formats for the serial monitor adds a lot o problems like what your asking
your not using the bluetooth serial right if your not getting it to work sending a 1 or 0
your setting are wrong but it hard to see that cus you post little or nothing to go with.
 
Last edited:
If you change the line Serial.println(Serial.read());
to Serial.write(Serial.read()); It might do what you want. (I have a very poor understanding of the syntax of "C" programming)

Les.
 
Try this
Code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
    mySerial.begin(38400);
  }
void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

It let's you talk from PC to phone both ways
and should work try it out
 
Serial.write(Serial.read()); is the right way but with this your just talking from the pc to atmega .
My code lets you talk PC to atmega to bluetooth to phone which is what you need to under stand first.
How to do
 
Just changing things at random (The IDE version) is just confusing things. You don't seem to be making any attempt to undestand the things you are being told. The fact that you are still asking the TX-Rx / TX - TX question shows that you are not reading the replies to your questions. The answer to that question depends on which configuration you are using. Also when you say that something is working or not working you are not telling us the exact configuration. I am getting near the point of giving up on trying to help you.

Les.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…