I'm writing a software that can receive data from Arduino via Bluetooth. My software is pretty fine now, at least it can pair with my hardware but when I try to test whether it can receive data via Bluetooth, I'm stuck. My Arduino receives temperature readings from a sensor at Analog pin 0. My Bluetooth module HC-05 connects with Arduino at Digital 0 and 1 (RX and TX pin respectively, as default). I don't know how to push data from Analog pin 0 to the Bluetooth module. Thanks in advance for any suggestion.
-
You don't. You send a message via Bluetooth that the pin has changed.– Ignacio Vazquez-AbramsCommented Sep 16, 2014 at 1:48
-
Sorry, could you give me more explanation, Ignacio?– Phan NamCommented Sep 16, 2014 at 2:23
-
Let's start simple. How would you do it if you were using the serial connection?– Ignacio Vazquez-AbramsCommented Sep 16, 2014 at 2:24
-
You meant if the Analog pin 0 is HIGH, set TX pin of Arduino HIGH?– Phan NamCommented Sep 16, 2014 at 2:38
-
you will need to read this arduino.cc/en/Serial/Print And you might want to try some of the basic tutorials you can find online or a book on starting arduino.– imjoshCommented Sep 17, 2014 at 15:47
2 Answers
The HC-05 looks like like any other terminal to the Arduino so any serial operations that you would use for a wired terminal can be used with the HC-05. See the HC-05's manual for how to set it up initially. Note that HC-05s don't forget their settings and they have no reset command to put them into a known state, so write down your settings for your future reference.
- Read the analog pin value;
- Convert the value into whatever physical units you require;
- Print the result with Serial.print() or Serial.println()
Edit: You wrote that your HC-05 is connected to Digital 0 and 1, but you created your SoftwareSerial, mySerial
, to transmit on Digital 11. In that case your HC-05's receive pin must be connected to Digital 11.
-
While this is correct, the asker has no idea how to perform serial operations. Commented Sep 16, 2014 at 20:23
-
Tks guys. I know how to convert my readings into Celcius values and print the result on Serial Monitor by Serial.print but I wonder whether I should convert my readings into bits (I mean analog-digital conversion) before pushing them into the Rx pin of Hc-05; as abovementioned, my sensor is connected to an analog pin while hc-05 is connected to 2 digital pins.– Phan NamCommented Sep 17, 2014 at 22:43
-
1If you're trying to send the readings as text via Bluetooth, just as you would to a wired terminal, you don't need to do anything but replace the serial cable with the HC-05. It is designed to look to the Arduino like a serial terminal.– JRobertCommented Sep 19, 2014 at 0:00
-
Could you look at my codes above and tell me if anything wrong, JRobert? I assume I connected HC-05 to Arduino correctly because I watched and followed exactly some tutorials on Arduino and HC-05.– Phan NamCommented Sep 19, 2014 at 15:17
-
1I don't see any obvious mistakes in the code. Have you tried the suggestion in my edit, above? Have you tried replacing the HC-05 with a serial cable to make sure your codes transmits what you expect?– JRobertCommented Sep 24, 2014 at 13:59
It's my code. Can anyone suggest anything wrong with it if my idea is to push data from Analog pin 0 to Digital pin 11, which is the Tx pin on Arduino connecting to Rx pin on HC-05?
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
int tempPin=0;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{
float reading = analogRead(tempPin); // reading on tempPin
float voltage = reading*5.0/1024.0; // the resolution of a pin is 10 bit,
float tempC = voltage/0.01; // 10mV = 1 Celcius
mySerial.write(tempC);
delay(3000);
}
-
-
You're using
mySerial.write()
which will send binary data down the serial line. Were you expecting text? Maybe try mySerial.println().– KingsleyCommented Feb 10, 2016 at 23:23