I'm trying to just do a basic data display using an HC-05 and appinventor. I have discovered a problem having nothing to do with the appinventor so I have put that on hold until I solve this issue. In summery, when I connect a variable pot to Pin A0 on my Arduino Uno with the HC-05 connected, I can see the data with a terminal program (Serial Bluetooth Terminal) on my Android phone but there are additional digits added and repeats of data. This condition comes through on the appinventor app as well but not as obvious as with the terminal program.
So here's the details:
I've connected a potentiometer to 3.3V and ground with the output connected to Pin AO. I've connected the HC-05's Vcc to 5.0V and ground on the arduino and, after loading the arduino, I connect the Arduino's Tx through a voltage divider to the HC-05 Rx as well as Arduino's Rx to HC-05's Tx. I can pair the HC-05 and connect to it.
The following is my code:
[code]
/* Electronoobs Bluetooth data receive with
Android and Arduino. transmits data from pin A0
Remember to disconnect the Rx and Tx pins of the HC-06 when
uploading the code
Subscribe: http://www.youtube.com/c/ELECTRONOOBS
Tutorial: http://www.electronoobs.com/eng_arduino_tut20.php
*/
//Inputs
int in = A0;
void setup() {
Serial.begin(9600); //Set the baud rate of the comunication
pinMode(in, INPUT); //Define the pin as input
}
void loop() {
//Read the analog value
float val = analogRead(in);
//Divide by 310 to obtain a range from 0 to 3.3V
float val2 = val / 310;
//Use serial.print to send the data in a "text" format
Serial.println(val2);
delay(400);//Small delay between each data send
}
[/code]
Note there a multiple times a "1" was added to the data. In the past, there were also multiple times the data was repeated with the same time stamp and there were multiple digits added rather than just one as shown here.
This is enough to show that the title of your post is erroneous, and you have probably learned all you need to learn about HC-05. I would be very suss about reply #2. While it seems not to be really necessary, it IS good practice to to ensure 3.3v at Bluetooth Rx, but the way you are doing it is the way everybody else does it, and surely not the cause of your problem.
For all that, I can't say much about what the problem really is, but I suspect it is all about timing, even though I note that you do have a delay in the loop - a large one, not a small.
Since you are smart enough to use hardware serial, two options offer themselves:
Even though I understand the serial monitor is working perfectly, you might just try having it run simultaneously with Bluetooth, Thereby checking that the problem really is exclusivel to Bluetooth.
You might configure Bluetooth and monitor to run at 115200, as this might be one of those times when speed really counts.
This might be the time when you need to know something about data buffering - something I know nothing about. I also know nothing about reading pots, but I assume that the code is essentially kosher - just needs a bit of tweaking
You appear to be using Morisch's terminal, which is essentially the go-to app these days, and I don't think there is anything to be suss about there.
Contradiction in terms, and irrelevant to boot. By simultaneous, I mean simultaneous. The same data is fed to both together. This may not be too revealing, but may yield something, and is not hard to do.
I had difficulty changing the baud rate on one of my modules, but finally got it changed to 115200 on another module. I then changed the baud rate in the Arduino code to 115200 but again got the same results as before. This time the Terminal data is "simultaneous" with the serial monitor. You can see extra digits are added as well as multiple data points with the same time stamp.
Would you be willing to change the HC05 back to 9600 baud and try with software serial. Just to see if having serial monitor and the HC05 both connected to the same serial port may be a cause.
HC05 TX to Uno pin 2 and HC05 RX to Uno pin 3 via the voltage divider. Works fine on my setup.
/* Electronoobs Bluetooth data receive with
Android and Arduino. transmits data from pin A0
Remember to disconnect the Rx and Tx pins of the HC-06 when
uploading the code
Subscribe: http://www.youtube.com/c/ELECTRONOOBS
Tutorial: http://www.electronoobs.com/eng_arduino_tut20.php
*/
#include <SoftwareSerial.h>
SoftwareSerial ss(2,3);
//Inputs
int in = A0;
void setup() {
Serial.begin(115200); //Set the baud rate of the comunication
//pinMode(in, INPUT); //***** not required for analog in ****
ss.begin(9600);
}
void loop() {
//Read the analog value
float val = analogRead(in);
//Divide by 310 to obtain a range from 0 to 3.3V
float val2 = val / 310;
//Use serial.print to send the data in a "text" format
ss.println(val2);
Serial.println(val2);
delay(400);//Small delay between each data send
}
Question: does the Baud rate I tell the Arduino to use (eg, Serial.begin(9600)) in the Arduino code have any effect on the HC-05 or does it just dictate what Baud rate is being sent to the serial monitor? In other words, do I have to have the Baud rate set for the HC-05 the same as in my arduino code?
If you have the HC05 connected to software serial then the Serial baud (Serial.begin(baud)) rate is for serial monitor only. The software serial baud rate (ss.begin(baud)) is for the HC05.
So I used your code but changed the Serial.begin to 9600 - guess that doesn't make a difference. But the results are that all is good on the serial monitor but nothing on the terminal program other than a good connection. Haven't found any combination that makes it work.
I saw some of you other communications about this issue and have concluded I need a different HC-05. My is Version 4.0-20190728. I also think at least one is defective also and neither will hold a change in baud rate.
Thanks for your effort!
Will continue this thread if the new one doesn't work!
Probably correct. HC-05 is a Bluetooth v2 device, and any that purport to be a a v3 or v4 device are likely to be junk. I think it is pretty clear though, that these deices are quite OK so long as you do not want to send AT commands, and most people don't, or don't need to. The latter probably includes you, despite what I said, and reverting to 9600 would be a good idea.
None of the above alters the fact that your problem is unique to you - so far....
I'm surprised you ask the question, as this is surely not a problem for you. Since you are using hardware serial, the monitor AND Bluetooth must match match what you call in your code, which surely you have already ensured. If there is a mismatch, I'm sure you would get much more gibberish out of Bluetooth than the odd extra digit. There should be inverted question marks and all sorts of other crap.