• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

dont know how to use bluetooth module hc-05

Thread starter #62
This code let's you use the atmega both ways it to show you how it work from there you can go on and do what you want.
Code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}
void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}
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
 

be80be

Well-Known Member
#63
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
 

be80be

Well-Known Member
#65
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.
 

be80be

Well-Known Member
#68
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
 
Thread starter #69
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
 
Thread starter #70
why you always want to add this to code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(rxPin, txPin);
why just don use built in tx rx
 

be80be

Well-Known Member
#71
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:

Les Jones

Well-Known Member
#72
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.
 

be80be

Well-Known Member
#74
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
 

be80be

Well-Known Member
#75
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
 

Les Jones

Well-Known Member
#80
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.
 

Latest threads

EE World Online Articles

Loading

 
Top