Displaying temperature and pressure at the same time using java

Hi!!

I am very new in arduino and in programming (I started 4 days ago :D ). And now I am trying to display the data from my thermistor (from sunfounder) and from my barometer (also from sunfounder BMP280) in the same way that a lot of people are doing: https://www.youtube.com/watch?v=lFZ26gD7OIE . I manage to get the right codes for my thermistor and barometer separately but when I connect the two sensors in my arduino and try to do the same thing that is in the youtube video, It doesnt work entirely. I manege to get the temperature well, in celsius, but the pressure values are not shown in Pa. I cant understand why... Can someone help me with this, please?

Here are the individual codes for the thermistor and barometer:

Thermistor:

include

define analogPin A0 //the thermistor attach to

define beta 3950 //the beta of the thermistor

define resistance 10 //the value of the pull-up resistor

void setup() {

Serial.begin(9600); }

void loop() { long a =1023 - analogRead(analogPin); //read thermistor value

//the calculating formula of temperature

float tempC = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;

Serial.println(tempC);

delay(1000); //wait for 100 milliseconds }

Barometer:

/* * bmp280_example.ino * Example sketch for BMP280 * * Copyright (c) 2016 seeed technology inc. * Website : www.seeedstudio.com * Author : Lambor, CHN * Create Time: * Change Log : * * The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */

include "Seeed_BMP280.h"

include

BMP280 bmp280;

void setup() { Serial.begin(9600); if(!bmp280.init()){ Serial.println("Device error!"); } }

void loop() { float pressure;

//get and print atmospheric pressure data

Serial.print(bmp280.getPressure());

Serial.println("\n");//add a line between output of different times.

delay(1000); }

My code following the youtube template to show the two graphs in java:

include

include "Seeed_BMP280.h"

BMP280 bmp280;

define analogPin A0 //the thermistor attach to

define beta 3950 //the beta of the thermistor

define resistance 10 //the value of the pull-up resistor

void setup() {

Serial.begin(9600); if(!bmp280.init()) Serial.println("Device error!"); }

void loop() {

long a =1023 - analogRead(analogPin); int sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0; float c = (bmp280.getPressure()); int sensor2 = c;

char text [40]; sprintf (text, "%d, %d\n", sensor1, sensor2); Serial.println (text);

delay(1000); //wait for 100 milliseconds

}

With that coding I get this:

25, -28643 (as an example)

Again, the temperature is fine, but I cant understand why the pressure isnt in the correct units...

Can someone help me? :)

Cheers!!

float c = (bmp280.getPressure());What units does getPressure return?

Please remember to use code tags when posting code

Sorry!! In pascals. It is really strange because, as soon as I put the Serial.print before the (bmp280.getPressure()) I got the right values: 10241224, 6. But of course in a completely wrong way of displaying the thing. The 102412 is the pressure in pascals the following two and four is the temperature and then I dont know what is the 6 after the comma ahah

You can't fit the pressure in Pascals into a sixteen bit "int".

Use unsigned long

how? Just change the int sensor2 = (...) to long sensor2 = (...)?

Thank you so much for your help!!!

You can use long if you expect to measure negative pressure, but I suggested unsigned long.

Your choice.

I used the unsigned long (just replaced the int sensor2 = (...) to unsigned long sensor2 = (...)) and the values that I am getting are the same: - 28665 (as an example)

Post the code.

include

include "Seeed_BMP280.h"

BMP280 bmp280;

define analogPin A0 //the thermistor attach to

define beta 3950 //the beta of the thermistor

define resistance 10 //the value of the pull-up resistor

void setup() {

Serial.begin(9600); if(!bmp280.init()) Serial.println("Device error!"); }

void loop() {

long a =1023 - analogRead(analogPin); int sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0; float c = (bmp280.getPressure()); unsigned long sensor2 = c;

char text [40]; sprintf (text, "%d, %d\n", sensor1, sensor2); Serial.println (text);

delay(1000); //wait for 100 milliseconds

}

You need to let sprintf know that it's being passed an unsigned long:

  sprintf (text, "%d, %lu\n", sensor1, sensor2);

Yes!!!! Thank you so much to you too!!! Thanksss guys you really made my day happier!

You wouldn't have had the problem if you'd just used Serial.print :)

how could I do that? Can you exemplify, please? :slight_smile:

One more thing guys: In the temperature values, I only get exact values like 24,25,26 and not 23.4 , 23.9, 24.56,etc. etc. Do you know why?

You're storing the result of your temperature calc in an integer. By definition, integers don't have a fractional component. Use float instead.

wildbill: You're storing the result of your temperature calc in an integer. By definition, integers don't have a fractional component. Use float instead.

Can you help me with that? I tried to change the code using float but I dont displays the right values

#include 
#include "Seeed_BMP280.h"
BMP280 bmp280;

#define analogPin A0 //the thermistor attach to 
#define beta 3950 //the beta of the thermistor
#define resistance 10 //the value of the pull-up resistor

void setup() {


  Serial.begin(9600);
 if(!bmp280.init())
    Serial.println("Device error!");
}

void loop() {

 long a =1023 - analogRead(analogPin); 
 float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
 float c =  bmp280.getPressure();
 unsigned long sensor2 = c;
  
  char text [40];
  sprintf (text, "%s, %lu\n", sensor1, sensor2);
  Serial.println (text);

  delay (500);
}

You can't use sprintf with floats easily on AVRs, but you'd need to use %f if you could.

As I pointed out earlier, you don't need sprintf.

 float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
 unsigned long sensor2 = c;
  sprintf (text, "%s, %lu\n", sensor1, sensor2);

The format specifier for a float is NOT %s. That is for strings.

By the way, by default, support for floats in sprintf() is disabled.

I assume you're using sprintf because it was in the example code you copied. As mentioned above, you really don't need it:

 float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
 float c =  bmp280.getPressure();
 Serial.print(Sensor1);
 Serial.print(", ");
 Serial.println(c);

Not tested. Not even compiled.

wildbill: I assume you're using sprintf because it was in the example code you copied. As mentioned above, you really don't need it:

 float sensor1 = beta /(log((1025.0 * 10 / a - 10) / 10) + beta / 298.0) - 273.0;
 float c =  bmp280.getPressure();
 Serial.print(Sensor1);
 Serial.print(", ");
 Serial.println(c);

Not tested. Not even compiled.

Thank you so much guys again! I learnt one more thing thanks to you both!! It worked!! Now I will try to display on a LCD, wish me luck :D ahah