Go Down

Topic: Displaying temperature and pressure at the same time using java  (Read 700 times) previous topic - next topic

brunopeniche

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 <Wire.h>



#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 <Wire.h>

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 <Wire.h>
#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!! 



AWOL

Code: [Select]
float c = (bmp280.getPressure());What units does getPressure return?


Please remember to use code tags when posting code

brunopeniche

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

AWOL

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

Use unsigned long

brunopeniche

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

Thank you so much for your help!!!

AWOL

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

Your choice.

brunopeniche

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)


brunopeniche

#include <Wire.h>
#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

}

wildbill

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

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

brunopeniche

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

AWOL

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

brunopeniche


brunopeniche

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?

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.

Go Up