HC-05 bluetooth module affects LM35 temperature reading

Hi,

My LM35 temperature readings changes a lot when I connect my HC-05 module to my nano. How do I prevent this from happening?

I have a HC-05 bluetooth module and a LM35 temp sensor connected together in a Arduino nano. The HC-05 is getting power from the 3V3 pin of nano while the LM35 is getting power from the 5V pin. When I run my program, the temperature reading from LM35 changes a lot unlike when I only have the LM35 connected to it without the HC-05 module.

int tempSensor = A1; //LM35 temp sensor
int sourceVoltPin = 5;

const float celcius = 0.470703125;//0.48828125;
unsigned long previousTime_0 = 0;
const long eventTime_0_temp = 1000;


void setup() {
  // put your setup code here, to run once:
  pinMode(sourceVoltPin, OUTPUT);
  digitalWrite(sourceVoltPin, HIGH);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
      /* Updates time frequently */
    unsigned long currentTime = millis();
    float temp = analogRead(tempSensor)*celcius;
    
    if (currentTime - previousTime_0 >= eventTime_0_temp) {
    Serial.print("Temp: ");
    Serial.println(temp,4);
    
    previousTime_0 = currentTime;
  }
}

refer_pin:
My LM35 temperature readings changes a lot when I connect my HC-05 module to my nano. How do I prevent this from happening.

This is a frequent question on this forum.
Use the search field on top of this page.

Basically

  1. Switch to the more stable 1.1volt Aref in setup (don't calculate with 0.488...)
  2. Give the LM35 it's own (not shared) ground wire to the Arduino.

Serial.println(temp,4); // wishful thinking
With default Aref and a 10-bit , tempC resolution is about 0.5C (not even one decimal place).
With 1.1volt Aref, you could be getting one decimal place.
Leo..

Wawa:
Use the search field on top of this page.

I did but looking at some and searching also via Google, it seems that tutorials and videos similar to this also do an almost same configuration. I mean different pins but still similar.

Wawa:

  1. Switch to the more stable 1.1volt Aref in setup (don't calculate with 0.488...)

How do I set that 1.1 volt Aref? My nano is connected directly to my laptop.

Wawa:
2) Give the LM35 it's own (not shared) ground wire to the Arduino.

What I did was I put the ground of my LM35 to the GND pin beside Vin while the HC-05 ground to my GND pin beside the RST pin. Is that what you're trying to say? if not, where do I place the ground in the Arduino?

Wawa:
With default Aref and a 10-bit , tempC resolution is about 0.5C (not even one decimal place).
With 1.1volt Aref, you could be getting one decimal place.

10-bit is the 1024, right? what do you mean by "tempC resolution is about 0.5C"?

refer_pin:
10-bit is the 1024, right? what do you mean by "tempC resolution is about 0.5C"?

The sensor outputs 250mV at a temp of 25C, which results in an A/D value of 250/5000 * 1024 = 51.
51 A/D values across 25 degrees C is a resolution (steps) of 0.5C. Assuming Aref is 5volt (which it rarely is).

With a lower reference, like the built-in ~1.1volt Aref of a Nano, you get 250/1100 * 1024= ~233 steps across that 25C temp range.
Resulting in an almost 5x higher resolution, and a stable readout.

You simply switch to 1.1volt Aref in setup().
See example code.
Leo..

// connect LM35 to 5volt A0 and ground
// calibrate temp by changing the last digit(s) of "0.1039"

const byte tempPin = A0;
float calibration = 0.1039;
float tempC; // Celcius
float tempF; // Fahrenheit

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); // use internal 1.1volt Aref
  // change INTERNAL to INTERNAL1V1 for a Mega
}

void loop() {
  tempC = analogRead(tempPin) * calibration; // get temp

  tempF = tempC * 1.8 + 32.0; // C to F
  Serial.print("Temperature is  ");
  Serial.print(tempC, 1); // one decimal place resolution is all you get
  Serial.print(" Celcius  ");
  Serial.print(tempF, 1);
  Serial.println(" Fahrenheit");

  delay(1000); // use a non-blocking delay when combined with other code
}

Wawa:
See example code.

I tried it but what I'm getting are values from 0.4-0.6. Is there something that I did wrong with my code based from your suggestion?

const byte tempSensor = A1; //LM35 temp sensor
int sourceVoltPin = 5;

const float celcius = 0.1039;
unsigned long previousTime_0 = 0;
const long eventTime_0_temp = 1000;


void setup() {
  // put your setup code here, to run once:
  analogReference(INTERNAL);
  pinMode(sourceVoltPin, OUTPUT);
  digitalWrite(sourceVoltPin, HIGH);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
      /* Updates time frequently */
    unsigned long currentTime = millis();
    float temp = analogRead(tempSensor)*celcius;
    
    if (currentTime - previousTime_0 >= eventTime_0_temp) {
    Serial.print("Temp: ");
    Serial.println(temp,4);
    
    previousTime_0 = currentTime;
  }
}

Question, do I have to connect my positive pin of my LM35 to the ref pin or 5V pin when I'm using the analogReference(INTERNAL) function?

refer_pin:
The HC-05 is getting power from the 3V3 pin of nano

Not likely to be correct. Check the back of the bluetooth breakout board.

Nick_Pyner:
Not likely to be correct. Check the back of the bluetooth breakout board.

The back says Vcc can receive 3.6v but it seems to be working well so far.

refer_pin:
I tried it but what I'm getting are values from 0.4-0.6. Is there something that I did wrong with my code based from your suggestion?

Did you try my code on it's own.
That should work, unless you have a wiring problem (upload a picture).
Or a 'fake' sensor (there seem to be many around).

Don't use an Arduino pin to power the sensor. It should be powered directly from the 5volt pin of the Nano.
Don't connect anything to the Ref pin, unless you use it to inject an external reference (which you don't).
Serial.println(temp,4); // the last three decimal places display nonsense
Serial.println(temp,1); // use this instead

The BT module has it's own 3.3volt regulator, and should also be powered from the 5volt pin.
The 3.3volt pin of a Nano can't deliver the required current either.
The BT module should have a 1:2 voltage divider on it's RX pin.
Leo..

Wawa:
Did you try my code on it's own.

Yes I did. It worked when I connected my LM35 Vcc to the 5V pin and use analogReference(INTERNAL).

Wawa:
The 3.3volt pin of a Nano can't deliver the required current either.

The HC-05 still works even if I connect it's Vcc to 3V3 pin. Should I connect both the Vcc pin of LM35 and HC-05 with my nano 5V pin?

Here's my code so far:

const byte tempSensor = A1; //LM35 temp sensor
int sourceVoltPin = 5;
int state = 0;

const float celcius = 0.1039;//0.107421875;
unsigned long previousTime_0 = 0;
const long eventTime_0_temp = 500;


void setup() {
  // put your setup code here, to run once:
  analogReference(INTERNAL);
  pinMode(sourceVoltPin, OUTPUT);
  digitalWrite(sourceVoltPin, HIGH);
  Serial.begin(38400);
}

void loop() {

    unsigned long currentTime = millis();
    float temp = analogRead(tempSensor)*celcius;

    if(Serial.available() > 0){ // Checks whether data is comming from the serial port
    state = Serial.read(); // Reads the data from the serial port
    }
    if (currentTime - previousTime_0 >= eventTime_0_temp) {

    Serial.print(temp,1);
    Serial.print(',');
    
    previousTime_0 = currentTime;
  }
}

refer_pin:
Should I connect both the Vcc pin of LM35 and HC-05 with my nano 5V pin?

Since

  1. You state that Bluetooth is the cause of the problem
  2. You have already worked out that the voltage is substandard
  3. You have been advised that same applies to current available on the 3.3v pin

You may safely conclude that fighting with one hand tied behind your back is not a good idea, and the answer is yes. It may not solve your problem, but at least you are playing by the rules, which increases your chances of solving it, not to mention having a sensible discussion about it.

Yes, you should power both from the 5volt pin, but give each their own ground connection to the Arduino.

Why is your sketch still using "sourceVoltPin".
You can remove the three lines for that.

temp is being read almost 10,000 times per second, but only used twice per second.
Can move that temp line inside the if() statement, just above the print statement.

Why "state' and 'Serial.available'.
You don't seem to use it.
Or have you connected the HC-05 to the USB<>Serial pins (not so wise).
Leo..

Nick_Pyner:
You may safely conclude that fighting with one hand tied behind your back is not a good idea, and the answer is yes. It may not solve your problem, but at least you are playing by the rules, which increases your chances of solving it, not to mention having a sensible discussion about it.

I agree so I connected the Vcc pin of both LM35 and HC-05 module.

Wawa:
Yes, you should power both from the 5volt pin, but give each their own ground connection to the Arduino.

Yep, I did that after you told me to put them in separate ground connections and it worked.

This is my code so far after your suggestions:

const byte tempSensor = A1; //LM35 temp sensor
int state = 0;

const float celcius = 0.1039;//0.107421875;
unsigned long previousTime_0 = 0;
const long eventTime_0_temp = 500;


void setup() {
  // put your setup code here, to run once:
  analogReference(INTERNAL);
  Serial.begin(38400);
}

void loop() {

    unsigned long currentTime = millis();
    
    if(Serial.available() > 0){ // Checks whether data is comming from the serial port
    state = Serial.read(); // Reads the data from the serial port
    }
    if (currentTime - previousTime_0 >= eventTime_0_temp) {
    float temp = analogRead(tempSensor)*celcius;
    Serial.print(temp,1);
    Serial.print(',');
    
    previousTime_0 = currentTime;
  }
}

Wawa:
temp is being read almost 10,000 times per second, but only used twice per second.

Yes, would it be better to read data more than twice per second?

Wawa:
Why "state' and 'Serial.available'.
You don't seem to use it.
Or have you connected the HC-05 to the USB<>Serial pins (not so wise).

I'm not using state as of the moment so I removed it. Same with "Serial.available " but I'm going to add some code for conditions I'll be apply later.

Wawa:
Or have you connected the HC-05 to the USB<>Serial pins (not so wise).

Are you referring to the tx and rx pins of arduino? I was told to use software serial and other pins for tx and rx but I'm not sure of the benefits of doing so.

Pin 0 and 1 are already used for the USB<>Serial chip.
Connecting another device to those pins could sort-off work if you understand the limitations.
Maybe safer to use SoftwareSerial on two different pins.

Data is only useful if you're using it.
A slight improvement could be adding smoothing code (averaging multiple temp readings) to the temp code.
Or dump the LM35, and get a digital sensor (DS18B20 etc).
Leo..

refer_pin:
Are you referring to the tx and rx pins of arduino? I was told to use software serial and other pins for tx and rx but I'm not sure of the benefits of doing so.

IF your programme does not employ the serial monitor, there are no benefits whatsoever - except for the lazy and incompetent. The limitations of software serial are matters of memory and computer resources , the most obvious being the rate at which you can run Bluetooth.. These are not necessarily fatal but can be something you may eventually regret.

The limitations of using Bluetooth on hardware serial, pins 0,1, are

  1. Bluetooth has to be disconnected while uploading your programme - something I'm sure you already know, and apparently can live with.
  2. You are not able to send output from the serial monitor, i.e. use the keyboard - something you don't appear to need.

If you have two serial devices, you may use one software serial as a last resort. It may be fine, even if it isn't a great idea. If it is not fine you can get an Arduino with more than one hardware serial port.

Wawa:
Pin 0 and 1 are already used for the USB<>Serial chip.
Connecting another device to those pins could sort-off work if you understand the limitations.
Maybe safer to use SoftwareSerial on two different pins.

Yes, I'll try to use SoftwareSerial as you suggested.

Wawa:
Data is only useful if you're using it.
A slight improvement could be adding smoothing code (averaging multiple temp readings) to the temp code.

Wouldn't even adding smoothing code affect the overall accuracy of the temp data due to it being an average of in your suggestion, 10000 temp inputs? or are there any ways to try to calibrate LM35 accuracy?

Wawa:
Or dump the LM35, and get a digital sensor (DS18B20 etc).

As of the moment, I can't get out or have orders delivered in my place but I'll consider that.

I did not suggest 10000 readings.
Your code did, and used two of them.

There is a smoothing example on the Arduino site.
Google it.
Or ask in your cross-post.
Leo..

Wawa:
I did not suggest 10000 readings.
Your code did, and used two of them.

Wait, my code can do that? I put a millis only to print out two values per second but how can it read 10000 temp values when we only have 1000 milliseconds in a minute?

Your original loop() only contained a serial available test, a millis test and an analogRead.
The analogRead takes the longest at about 100us.
So about 10,000 loops per second, with about 10,000 resulting readings.
Only when the millis test completed, you printed the last temp reading.
Not a problem though, just good to understand when one day you need to do more in loop().
Leo..