Hello, just checking if my arduino and bluetooth module were still running I tried this code that I found on the internet.
int in = A0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(in,INPUT);
delay(1000);
}
void loop() {
float val = analogRead(in);
float val2 = val/205.0;
Serial.print(val2);
Serial.print("\n");
delay(1000);
// put your main code here, to run repeatedly:
}
In the montior serial of IDE Arduino it shows the right numbers ranging from 0V to 5V.
But in the bluetooth android app it keeps changing from (if the value is for example 4.4V) 4.4V to 44V
It seems the decimal information of the number is wrong. Can someone tell me the probable cause? Maybe it's the refresh rate (in the APP I setup a clock to check incoming data af 500ms)
The probable cause is that the code on the other end is flawed. If this code writes to the serial monitor correctly, there is NOTHING wrong with this code.
1 Change you BT connection from hard UART Port to soft UART Port (SUART) as per Fig-1.
Figure-1:
2. Upload the following sketch (yours one with slight modification).
#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3);
int in = A0;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
SUART.begin(9600);
//pinMode(in, INPUT);
//delay(1000);
}
void loop()
{
float val = analogRead(in); //3.3V --> 1023/5*3.3 = 675
float val2 = val / 205.0; //675/205.0 = 3.29
Serial.print(val2);
Serial.print('\n');
//------------------
SUART.print(val2);
SUART.print('\n');
delay(1000);
// put your main code here, to run repeatedly:
}
3. Check that the BT Terminal of the Android Phone shows correct value at 1-sec interval. I have checked in my Phone, and it shows 3.28 with A0-pin connected at 3.3V point.
4. You have operated your BT in parallel with Serial Monitor (Fig-2) -- a network which is neither recommended nor to be working reliably.
Figure-2:
Read reply #1 again - twice. The last thing you should do is read reply#2. Leave Bluetooth on pins 0,1 and serial monitor turned on i.e. Arduino is sending identical data to both.
There is nothing to suggest your code is not 100% kosher.
If terminal says 44v,
you know it should be 4.4v, and
monitor confirms it is indeed 4.4v,
it is definitely time to get another terminal app.
It is disconcerting, indeed surprising, that an app can be so fundamentally wrong. I have tried several, and have never seen that. They all seem to be the same, except that I found that one doesn't log the data, which is pretty stupid. Maybe it is the one you've been using(?)
With my HC05 BT connected at RX/TX terminals of UNO, there is a problem in uploading the sketch. So, I disconnected the BT temporarily from RX/TX terminals and uploaded the sketch and then connected back the BT. The BT Terminal of my Phone shows correct values.
GolamMostafa:
With my HC05 BT connected at RX/TX terminals of UNO, there is a problem in uploading the sketch.
It's not just your HC-05, if Bluetooth is left connected to serial while uploading, there will always be a problem. The solution is: don't do it.
This might sound tedious, but there is no actual need to have Bluetooth connected in the first place. All code debugging, including two-way traffic, can be proven before Bluetooth needs to be connected. The "problem", such as it is, comes about because the UART and the serial port are shared. How long the "problem" takes to not be a problem simply depends on how far you are along the Road to Damascus before you see the light. Rather like not starting the car with a gear engaged.
Note that, while having Bluetooth on proper serial allows you to have the serial monitor to do what it is supposed to do - monitor, it can only do exactly that. You cannot send through the serial monitor.