Adafruit BMP183 SPI Altitude Sensor not retrieving correct a

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
yigiter007
 
Posts: 175
Joined: Thu Oct 03, 2013 10:37 am

Adafruit BMP183 SPI Altitude Sensor not retrieving correct a

Post by yigiter007 »

I"m using a Adafruit BMP183 SPI Altitude Sensor in a arduino project. The sensor works when I run the example program and gets correct results, but when I use the same code in my program I get weird values for altitude, i get varying negative values.

Code: Select all

/*00000000      Libraries included    00000000*/
#include <SerialGraphicLCD.h>
#include <SoftwareSerial.h>
#include "DHT.h"
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP183.h>
#include <DS3234.h>

/*00000000      GLCD screen size  00000000*/
#define maxX 127//159 
#define maxY 63 //127

/*00000000   BMPsensor  00000000*/
#define BMP183_CLK  13  // AKA CLK
#define BMP183_SDO  12  // AKA MISO
#define BMP183_SDI  11  // AKA MOSI
#define BMP183_CS   4
Adafruit_BMP183 bmp = Adafruit_BMP183(BMP183_CLK, BMP183_SDO, BMP183_SDI, BMP183_CS);
volatile float pressure_mmHg;
volatile float pressurepascal;
volatile float Altitude;
volatile float last_microsBMP;
float seaLevelPressure;

/*00000000   DeadOn time chip  00000000*/
volatile int TimeDate [7]; //second,minute,hour,null,day,month,year
const int CSpin=10;
const int MOSIpin=11;
const int MISOpin=12;
const int CLKpin=13;
float last_microsTime;

/*00000000    DHT sensor stuff    00000000*/
#define DHTPIN 9     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);
volatile float humidity=0;
volatile float airtemp=0;
volatile float heatindex=0;
volatile float last_microsDHT;

/*00000000      variables for PINS    00000000*/
//Bup Bdown Bleft Bright Bcenter But1 But2 But3
//const int ledPin13 = 13;       // the number of the LED pin

/*00000000    variables for debounce    00000000*/
long debouncing_time = 250; //Debouncing delay coefficent
long last_micros;
int state = LOW;

/*00000000    variables for menus    00000000*/


/*00000000    LCD class declaration    00000000*/
LCD LCD;

/*00000000    CoSensor declaration    00000000*/
volatile int CoCode;
volatile int CoLevel;
float last_microsCO;
/*00000000    SETUP    00000000*/

void setup()
{
  pinMode(5, INPUT_PULLUP);     //button on pin 5
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  
  Serial.begin(9600);
  
  attachInterrupt(0, debounce, FALLING); //interrupt
  
//  pinMode(ledPin13, OUTPUT);   // pin 13 on board LED

  RTC.configure(MOSIpin,MISOpin,CLKpin,CSpin);
//  RTC.setDateTime(18,9,2014,23,28,0); // RTC.setDateTime(int d, int mo, int y, int h, int mi, int s);
  dht.begin();
  bmp.begin();

//RTC.setDateTime(24, 10, 2014, 23, 17, 0); // int d, int mo, int y, int h, int mi, int s

  //splash screen
  LCD.setBacklight(20);
  LCD.clearScreen();
  LCD.setHome();
  LCD.printStr(" Program starts in "); //displays "program is starting in 5/4/3/2/1 etc"
    for(int i = 5; i >= 0; i--) 
      {
        LCD.setX(113);
        LCD.setY(0);
        LCD.printNum(i);
        delay(500);
      } 
  LCD.clearScreen(); 
  delay(200);
}

/*00000000    LOOP    00000000*/

void loop(void)
{   
Serial.print("\n");   
Serial.print("Altitude:    "); 
Serial.print(bmp.getAltitude(seaLevelPressure)); 
Serial.println(" m");
Serial.println("");
/*Serial.print("\n"); 
Serial.print(Altitude);
Serial.println(" m");
Serial.println("");
*/
delay(50);

SensorUpdate();     //update all sensors including time
MainMenuDisplay();
BOXSELECTOR();
//menu_select();
//sensor_menu();
//time_menu();

}

void SensorUpdate()
{
  Battery();
  delay(100);
  ClockRead();
  delay(100);
  DHTsensor();
  delay(100);
  CoSensor();
  delay(100);
  BMPsensor();
  delay(100);
}

/*00000000   debounce functions    00000000*/

void debounce() 
{
  if(((long)(micros() - last_micros) >= debouncing_time * 2000) || last_micros==0)
  {
    state = !state;
    //digitalWrite(13, state);
    last_micros = micros();
    //buttontest();
  }
}

void buttontest()
{
    if(digitalRead(5)==LOW)
    {sensor_menu();}
    
    if(digitalRead(6)==LOW)
    {time_menu();}
    
    if(digitalRead(7)==LOW)
    {/*multimeter();*/}
    
    if(digitalRead(8)==LOW)
    {/*sleep();*/}    
    
    if(digitalRead(9)==LOW)
    {MainMenuDisplay();}    
    
    if(digitalRead(10)==LOW)
    {/*BUTTON10=true;*/}    
}

void MainMenuDisplay()
{
optionrowdisplay(); //display info on glcd
TimeDisplay(0,0,0,0);      //Display Time on glcd
Menubuttons_MenuOptions();
}

void BOXSELECTOR()
{
LCD.drawBox(4,53,0,47,0);//x1,y1,x2,y2  draw box on S Sensor
LCD.drawBox(3,52,1,48,0);
LCD.drawBox(2,51,2,49,0);
delay(50);

}
void CoSensor()
{
  if((long)micros() - last_microsCO >= 100000 || last_microsCO==0) // Wait a few seconds between measurements. //delay(1000);
  {
  CoLevel = analogRead(A5);// 10k resistor noramlizes in basement at 70
  
  if(CoLevel<300)
    {CoCode=1;}
  else if(CoLevel>301 && CoLevel<800)
    {CoCode=2;}
  else if(CoLevel>801)
    {CoCode=3;}
  }
last_microsCO=micros();  
}

void Battery()
{
}

void ClockRead()
{
RTC.readDateTime(); //DD.MM.YYYY-hh.mm.ss
/*
RTC.time_h(); // hour
RTC.time_m(); // minutes
RTC.time_s(); // seconds
RTC.date_d(); // day
RTC.date_m(); // month
RTC.date_y(); // year
RTC.readTemp();
RTC.setDateTime(int d, int mo, int y, int h, int mi, int s);
*/
TimeDate[2]=RTC.time_h(); //2.1.0 hour minute second
TimeDate[1]=RTC.time_m();
TimeDate[0]=RTC.time_s();
TimeDate[5]=RTC.date_d(); // day
TimeDate[4]=RTC.date_m(); // month
TimeDate[3]=RTC.date_y()-2000; // year
}
  

/*0000000000000000000      BMPsensor     00000000000000000000000000000*/

void BMPsensor() 
{
 if((long)micros() - last_microsBMP >= 100500 || last_microsBMP==0) // Wait a few seconds between measurements. //delay(2050);
 {
    /* Display atmospheric pressue in Pascals */
    //float pressurepascal;
    //float pressure_mmHg;
    pressurepascal=bmp.getPressure();
    pressure_mmHg=pressurepascal*(0.0002953);

    // Pressure bmp.getPressure() Pascals
    // Pressure (bmp.getPressure() / 100) millibar (hPa)
    // Pressure (pressure_mmHg); millimeter Mercury (mmHg)
    
    /* First we get the current temperature from the BMP085 */
    float temperatureF;
    float temperatureC;
    temperatureC =bmp.getTemperature();
    temperatureF=(temperatureC*(1.8))+32; //    F = C * 180 + 32

    float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; // should be ~1000
    seaLevelPressure=1011;
    //SENSORS_PRESSURE_SEALEVELHPA millibar/hPa
    Altitude=bmp.getAltitude(seaLevelPressure); // meters
 }
last_microsBMP=micros();
}

/*0000000000000000000      DHTsensor     00000000000000000000000000000*/

void DHTsensor() 
{
  if((long)micros() - last_microsDHT >= 100100 || last_microsDHT==0) // Wait a few seconds between measurements. //delay(2100);
      {
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      humidity=dht.readHumidity();
      // Read temperature as Celsius
      float t = dht.readTemperature();
      // Read temperature as Fahrenheit
      float f = dht.readTemperature(true);
      airtemp=dht.readTemperature(true);
      // Check if any reads failed and exit early (to try again).
      if (isnan(h) || isnan(t) || isnan(f)) 
        {
          Serial.println("Failed to read from DHT sensor!");
          //last_micros2=micros();
          return;
        }
      
      // Compute heat index
      // Must send in temp in Fahrenheit!
      float hi = dht.computeHeatIndex(f, h);
      heatindex=dht.computeHeatIndex(f, h);
      }
      last_microsDHT=micros();
}

User avatar
yigiter007
 
Posts: 175
Joined: Thu Oct 03, 2013 10:37 am

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by yigiter007 »

here is the second file with the display code.

Code: Select all

/*00000000000000              menu display       00000000000000000000000*/
void optionrowdisplay()
{ 
  LCD.setHome(); //Battery
  LCD.printStr("BATT ");
  delay(35);
  LCD.setX(25);
  LCD.setY(00);
  LCD.printNum(100);
  delay(35);
  LCD.setX(45);
  LCD.setY(00);
  LCD.printStr("%");
  delay(35);
  
  LCD.drawBox(55,0,56,8,1);//(x1,y1,x2,y2,1)
  delay(35);
  
  LCD.setX(60); //Humidity
  LCD.setY(0);
  LCD.printStr("Humidity");
  delay(35);
  LCD.setX(110);
  LCD.setY(0);
  LCD.printNum(humidity);
  delay(35);
  LCD.setX(122);
  LCD.setY(0);
  LCD.printStr("%");
  delay(35);
  
  LCD.setX(0); //Bar pressure
  LCD.setY(10);
  LCD.printStr("BP");
  delay(35);
  LCD.setX(15);
  LCD.setY(10);
  LCD.printNum(pressure_mmHg);
  delay(35); 
  LCD.setX(28);
  LCD.setY(10);
  LCD.printStr("mmHg");
  delay(35);
  
  LCD.drawBox(55,9,56,18,1);//(x1,y1,x2,y2,1)
  delay(35);

  LCD.setX(60); //Temp
  LCD.setY(10);
  LCD.printStr("Temp");
  delay(35);
  LCD.setX(86);
  LCD.setY(10);
  LCD.printNum(airtemp);
  delay(35);
  LCD.setX(98);
  LCD.setY(10);
  LCD.printStr("/");
  delay(35);
  LCD.setX(104);
  LCD.setY(10);
  LCD.printNum(heatindex);
  delay(35);
  LCD.setX(116);
  LCD.setY(10);
  LCD.printStr("*");
  delay(35);
  LCD.setX(122);
  LCD.setY(10);
  LCD.printStr("F");
  delay(35);

if(Altitude<10 && Altitude>=1) //between 1 and 10
{
  LCD.setX(0); //altitude
  LCD.setY(20);
  LCD.printStr("ALA");
  delay(35);
  LCD.setX(22);
  LCD.setY(20);
  LCD.printNum(0);
  delay(35);
  LCD.setX(28);
  LCD.setY(20);
  LCD.printNum(Altitude);
  delay(35);
  LCD.setX(37);
  LCD.setY(20);
  LCD.printStr("m");
  delay(35);
}
else if(Altitude>10 && Altitude<99) //between 10 and 99  || Altitude<1
{
  LCD.setX(0); //altitude
  LCD.setY(20);
  LCD.printStr("ALB");
  delay(35);
  LCD.setX(22);
  LCD.setY(20);
  LCD.printNum(Altitude);
  delay(35);
  LCD.setX(37);
  LCD.setY(20);
  LCD.printStr("m");  
  delay(35);
}
else if(Altitude>99)
{
  LCD.setX(0); //altitude
  LCD.setY(20);
  LCD.printStr("ALC");
  LCD.setX(22);
  LCD.setY(20);
  LCD.printNum(Altitude);
  delay(35);
  LCD.setX(43);
  LCD.setY(20);
  LCD.printStr("m");  
  delay(35);
}
else if(Altitude<1)
{
  LCD.setX(0); //altitude
  LCD.setY(20);
  LCD.printStr("ALD");
  LCD.setX(18);
  LCD.setY(20);
  LCD.printNum(Altitude);
  delay(35);
  LCD.setX(48);
  LCD.setY(20);
  LCD.printStr("m");  
  delay(35);
}
  LCD.drawBox(55,19,56,26,0);//(x1,y1,x2,y2,1)
  delay(35);
  
  LCD.setX(60); //CO level
  LCD.setY(20);
  LCD.printStr("CO");
  delay(35);
  LCD.setX(73); //CO level
  LCD.setY(20);
  LCD.printStr("Level");
  delay(35);

LCD.eraseBlock(103, 20, 127, 27);

switch (CoCode) 
{
    case 1:
      LCD.setX(103);
      LCD.setY(20);
      LCD.printStr("GOOD");
      delay(35);
      break;
    case 2:
      LCD.setX(108);
      LCD.setY(20);
      LCD.printStr("OK");
      delay(35);
      break;
    case 3: 
      LCD.setX(105);
      LCD.setY(20);
      LCD.printStr("BAD");
      delay(35);
      break;
  }

  LCD.setX(0);
  LCD.setY(29);
  LCD.printStr("<-");
  delay(35);
  LCD.setX(12);
  LCD.setY(29);
  LCD.printStr("PW");
  delay(35);
  
  LCD.drawBox(27,28,28,36,0);//(x1,y1,x2,y2,1)
  delay(35);
  LCD.drawBox(94,28,95,36,0);//(x1,y1,x2,y2,1)
  delay(35);

  LCD.setX(0);
  LCD.setY(38);
  LCD.printStr("<-");
  delay(35);
  LCD.setX(12);
  LCD.setY(38);
  LCD.printStr("DIAL");
  delay(35);
   
  LCD.drawBox(36,37,37,44,0);//(x1,y1,x2,y2,1)
  delay(35); 

  LCD.setX(100); //UP BUTTON
  LCD.setY(29);
  LCD.printStr("UP");
  delay(35);
  LCD.setX(115); //UP Arrow
  LCD.setY(29);
  LCD.printStr("->");
  delay(35);
  
  LCD.drawBox(86,37,87,44,0);//(x1,y1,x2,y2,1)
  delay(35); 
  
  LCD.setX(90); //Down BUTTON
  LCD.setY(38);
  LCD.printStr("DOWN");
  delay(35);
  LCD.setX(115); //DOWN arrow
  LCD.setY(38);
  LCD.printStr("->");
  delay(35);
} 

void Menubuttons_MenuOptions()
{
  LCD.setX(0); //Sensor Clock Meter Sleep menu options
  LCD.setY(47);
  LCD.printStr("Sensor");  
  delay(35);
  LCD.setX(38); 
  LCD.setY(47);
  LCD.printStr("Time");
  delay(35);
  LCD.setX(65); 
  LCD.setY(47);
  LCD.printStr("Meter");
  delay(35);
  LCD.setX(97); 
  LCD.setY(47);
  LCD.printStr("Sleep");
  delay(25); 

  LCD.setX(0); //LEFT RIGHT ENTER BACK AND EXIT BUTTON
  LCD.setY(56);
  LCD.printStr("<- ->");
  delay(35);
  LCD.setX(36); 
  LCD.setY(56);
  LCD.printStr("ENTER");
  delay(35);
  LCD.setX(73); 
  LCD.setY(56);
  LCD.printStr("BACK");
  delay(25);
  LCD.setX(103); 
  LCD.setY(56);
  LCD.printStr("EXIT");
  delay(35); 
}

int TimeDisplay(int txstart, int tystart, int dxstart, int dystart)
{
  if((long)micros() - last_microsTime >= 500000 || last_microsTime==0) // Wait a few seconds between measurements. //delay(1000);
      {
        if(TimeDate[2]<10)  //hour
          {
            LCD.setX(txstart+39); 
            LCD.setY(tystart+29);
            LCD.printNum(TimeDate[2]); 
            delay(35);
          }
        else
          {
            LCD.setX(txstart+33);
            LCD.setY(tystart+29);
            LCD.printNum(TimeDate[2]); 
            delay(35);
          }

            LCD.setX(txstart+44);
            LCD.setY(tystart+29);
            LCD.printStr(":");//semicolon
            delay(35);
  
        if(TimeDate[1]<10) //minute
          {
            LCD.setX(txstart+50); 
            LCD.setY(tystart+29);
            LCD.printNum(0); 
            delay(35);
            LCD.setX(txstart+56); 
            LCD.setY(tystart+29);
            LCD.printNum(TimeDate[1]);
            delay(35);
          }
        else
          {
            LCD.setX(txstart+50); 
            LCD.setY(tystart+29);
            LCD.printNum(TimeDate[1]); 
            delay(35);
          }
          
            LCD.setX(txstart+61);
            LCD.setY(tystart+29);
            LCD.printStr(":");//semicolon
            delay(35);
  
       if(TimeDate[0]<10)  //second
         {
            LCD.setX(txstart+66);
            LCD.setY(tystart+29);
            LCD.printNum(0);
            delay(35);
            LCD.setX(txstart+72);
            LCD.setY(tystart+29);
            LCD.printNum(TimeDate[0]);
            delay(35);
         }
       else
         {
            LCD.setX(txstart+66);
            LCD.setY(tystart+29);
            LCD.printNum(TimeDate[0]);
            delay(35); 
         }

      if(TimeDate[2]<12)  //AM/PM
        {
            LCD.setX(txstart+79);
            LCD.setY(tystart+29);
            LCD.printStr("AM"); 
            delay(35);
        }
      else
        {
            LCD.setX(txstart+79);
            LCD.setY(tystart+29);
            LCD.printStr("PM");
            delay(25);
        }

if(TimeDate[4]<10)
        {
            LCD.setX(dxstart+45); //Date month
            LCD.setY(dystart+38);
            LCD.printNum(TimeDate[4]);
            delay(35);
        }
else if(TimeDate[4]>10)
        {
            LCD.setX(dxstart+38); //Date month
            LCD.setY(dystart+38);
            LCD.printNum(TimeDate[4]);
            delay(35);
        }

            LCD.setX(dxstart+50); // "/"  
            LCD.setY(dystart+38);
            LCD.printStr("/");
            delay(35);  
            
if(TimeDate[5]<10)
        {
            LCD.setX(dxstart+60); //Day
            LCD.setY(dystart+38);
            LCD.printNum(TimeDate[5]);
            delay(35);  
        }
else if(TimeDate[5]>9)
        {
            LCD.setX(dxstart+56); //Day
            LCD.setY(dystart+38);
            LCD.printNum(TimeDate[5]);
            delay(35);  
        } 
            
            LCD.setX(dxstart+68); // "/" 
            LCD.setY(dystart+38);
            LCD.printStr("/");
            delay(35);  
            
            LCD.setX(dxstart+74); //year
            LCD.setY(dystart+38);
            LCD.printNum(TimeDate[3]);
            delay(35);

        }    

}

}

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by Franklin97355 »

What are the other devices and how do you have them connected to the system?

User avatar
yigiter007
 
Posts: 175
Joined: Thu Oct 03, 2013 10:37 am

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by yigiter007 »

The main board is a Sparkfun red board along with Deadon RTC module also running on SPI. There is CO (ADC) sensor a DHT temperature and humidity sensor and 4 buttons that connect to digital IO pins and the interrupt pin. All of the outputs put data to a serial GLCD display. SPI chip select pins are 4 and 10. I'm not sure if that answered your question.
Attachments
photo (1).JPG
photo (1).JPG (616.45 KiB) Viewed 910 times

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by Franklin97355 »

Have you checked to see if perhaps you are running out of memory? I don't have all the libraries you are using so I can't run your code to test. Since the sensor works with the example code there must be an interaction with the other parts/code you are using and since most of those are from Sparkfun I would suggest you posting this question in their forums.

User avatar
yigiter007
 
Posts: 175
Joined: Thu Oct 03, 2013 10:37 am

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by yigiter007 »

I will run a memory code in the code soon. I tried loading the Adafruit demo sketch for the Baro sensor while connected to everything else and it works fine without changing any of the wires. I also ran the Deadon RTC demo code by itself without switching wires and it works too. The other pieces of data that the Baro sensor is retrieving and displaying is correct, only altitude is wrong. I also made sure if the wires were incorrectly numbered and they are not. I took the code in the Adafruit library demo and put it into a function to call and added a few lines to convert certain values into different units. The main board is a Uno clone and the only Deadon RTC is the only other component that is Sparkfun the rest is universal sensors. I figured I would ask you guys since the Baro sensor is the part not working and not the Deadon RTC or the board.
I can give you the libraries I'm using if you want to test it out.

I think the problem is with the way the Baro sensor code is retrieving data inside my function. Maybe there is something with the data type or the way I'm calling the altitude value.

User avatar
yigiter007
 
Posts: 175
Joined: Thu Oct 03, 2013 10:37 am

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by yigiter007 »

I went and shortened my code and took stuff out to reduce ram (free ram is at 1251 bytes) and i got readings that were reasonable between 10m and 40m for my basement for a while. Then it stopped working again and went back to negative numbers. I tried running the demo program BMP183 test that comes with the sensor library and it reads -81 meters. I tried rewiring it. All I have connected is a DHT temp/Humdity sensor, a CO2 sensor and the BMP183 and a GLCD to display all the info on. The negative altitude does change when I change the sealevelpressure variable. Is the BMP183 broken?

User avatar
adafruit2
 
Posts: 22146
Joined: Fri Mar 11, 2005 7:36 pm

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by adafruit2 »

no, it is not broken. yes you have to set the sea level pressure to calculate altitutde

https://learn.adafruit.com/adafruit-bmp ... -reference

Code: Select all

However, you can only really do a good accurate job of calculating altitude if you know the hPa pressure at sea level for your location and day! The sensor is quite precise but if you do not have the data updated for the current day then it can be difficult to get more accurate than 10 meters.
sea level pressure changes with the weather, you must update it constantly to get accurate altitude

User avatar
yigiter007
 
Posts: 175
Joined: Thu Oct 03, 2013 10:37 am

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by yigiter007 »

I have set the sea level pressure both as a global variable and separately as a variable in the BMP function and it doesn't affect the negative readings I'm getting. Does it matter where it is set. Is that what's causing the negative altitudes I.e -81 meters. This device seems to work on and off randomly, on for 1 week then off again another. Is there something I'm not setting or is there something I need to rest to make it work?

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by Franklin97355 »

Could you post the exact code you are loading on the micro in it's entirety so we can check it?

User avatar
yigiter007
 
Posts: 175
Joined: Thu Oct 03, 2013 10:37 am

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by yigiter007 »

Here is the code for my project. I comment out the RTC.configure and void ClockRead() to disable time breakout.

Code: Select all

/*00000000      Libraries included    00000000*/
#include <SerialGraphicLCD.h>
#include <SoftwareSerial.h>
#include "DHT.h"
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP183.h>
#include <DS3234.h>
#include <MemoryFree.h>

/*00000000      GLCD screen size  00000000*/
#define maxX 127//159 
#define maxY 63 //127

/*00000000   BMPsensor  00000000*/
#define BMP183_CLK  13  // AKA CLK
#define BMP183_SDO  12  // AKA MISO
#define BMP183_SDI  11  // AKA MOSI
#define BMP183_CS   4
Adafruit_BMP183 bmp = Adafruit_BMP183(BMP183_CLK, BMP183_SDO, BMP183_SDI, BMP183_CS);
volatile float pressure_mmHg;
volatile float pressurepascal;
volatile float Altitude;
volatile float last_microsBMP;
float seaLevelPressure=1013.25;
float SENSORS_PRESSURE_SEALEVELHPA=1013.25;
float pressureMillbar;

/*00000000   DeadOn time chip  00000000*/
volatile int TimeDate [7]; //second,minute,hour,null,day,month,year
const int CSpin=10;
const int MOSIpin=11;
const int MISOpin=12;
const int CLKpin=13;
float last_microsTime;

/*00000000    DHT sensor stuff    00000000*/
#define DHTPIN 9     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);
volatile float humidity=0;
volatile float airtemp=0;
volatile float heatindex=0;
volatile float last_microsDHT;

/*00000000      variables for PINS    00000000*/
//Bup Bdown Bleft Bright Bcenter But1 But2 But3
//const int ledPin13 = 13;       // the number of the LED pin

/*00000000    variables for debounce    00000000*/
long debouncing_time = 250; //Debouncing delay coefficent
long last_micros;
int state = LOW;

/*00000000    variables for menus    00000000*/


/*00000000    LCD class declaration    00000000*/
LCD LCD;

/*00000000    CoSensor declaration    00000000*/
volatile int CoCode;
volatile int CoLevel;
float last_microsCO;
/*00000000    SETUP    00000000*/

void setup()
{
  pinMode(5, INPUT_PULLUP);     //button on pin 5
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  
  Serial.begin(9600);
  
  attachInterrupt(0, debounce, FALLING); //interrupt

  RTC.configure(MOSIpin,MISOpin,CLKpin,CSpin);
  //RTC.setDateTime(15,11,2014,19,11,0); // RTC.setDateTime(int d, int mo, int y, int h, int mi, int s);
  dht.begin();
  bmp.begin();

  //splash screen
  LCD.setBacklight(20);
  LCD.clearScreen();
  LCD.setHome();
  LCD.printStr(" Program starts in "); //displays "program is starting in 5/4/3/2/1 etc"
    for(int i = 5; i >= 0; i--) 
      {
        LCD.setX(113);
        LCD.setY(0);
        LCD.printNum(i);
        delay(500);
      } 
  LCD.clearScreen(); 
  delay(200);
}

/*00000000    LOOP    00000000*/

void loop(void)
{   
SensorUpdate();     //update all sensors including time
MainMenuDisplay();
//BOXSELECTOR();
//menu_select();
//sensor_menu();
//time_menu();

//    Serial.print("freeMemory()=");
  //  Serial.println(freeMemory());
    //delay(1000);
}

void SensorUpdate()
{
  Battery();
  delay(100);
  ClockRead();
  delay(100);
  DHTsensor();
  delay(100);
  CoSensor();
  delay(100);
  BMPsensor();
  delay(100);
}

/*00000000   debounce functions    00000000*/

void debounce() 
{
  if(((long)(micros() - last_micros) >= debouncing_time * 2000) || last_micros==0)
  {
    state = !state;
    last_micros = micros();
    //buttontest();
  }
}

void buttontest()
{
    if(digitalRead(5)==LOW)
    {sensor_menu();}
    
    if(digitalRead(6)==LOW)
    {time_menu();}
    
    if(digitalRead(7)==LOW)
    {/*multimeter();*/}
    
    if(digitalRead(8)==LOW)
    {/*sleep();*/}    
    
    if(digitalRead(9)==LOW)
    {MainMenuDisplay();}    
    
    if(digitalRead(10)==LOW)
    {/*BUTTON10=true;*/}    
}

void MainMenuDisplay()
{
optionrowdisplay(); //display info on glcd
TimeDisplay(0,0,0,0);      //Display Time on glcd
Menubuttons_MenuOptions();
}

void BOXSELECTOR()
{
LCD.drawBox(4,53,0,47,0);//x1,y1,x2,y2  draw box on S Sensor
LCD.drawBox(3,52,1,48,0);
LCD.drawBox(2,51,2,49,0);
delay(100);
}


void CoSensor()
{
  if((long)micros() - last_microsCO >= 100000 || last_microsCO==0) // Wait a few seconds between measurements. //delay(1000);
  {
  CoLevel = analogRead(A5);// 10k resistor noramlizes in basement at 70
  
  if(CoLevel<300)
    {CoCode=1;}
  else if(CoLevel>301 && CoLevel<800)
    {CoCode=2;}
  else if(CoLevel>801)
    {CoCode=3;}
  }
last_microsCO=micros();  
}

void Battery()
{
}

void ClockRead()
{
RTC.readDateTime(); //DD.MM.YYYY-hh.mm.ss
TimeDate[2]=RTC.time_h(); //2.1.0 hour minute second
TimeDate[1]=RTC.time_m();
TimeDate[0]=RTC.time_s();
TimeDate[5]=RTC.date_d(); // day
TimeDate[4]=RTC.date_m(); // month
TimeDate[3]=RTC.date_y()-2000; // year
}
  

/*0000000000000000000      BMPsensor     00000000000000000000000000000*/

void BMPsensor() 
{
 if((long)micros() - last_microsBMP >= 100500 || last_microsBMP==0) // Wait a few seconds between measurements. //delay(2050);
 {
    pressurepascal=bmp.getPressure();
    pressure_mmHg=pressurepascal*(0.0002953);
    float temperatureF;
    float temperatureC;
    temperatureC =bmp.getTemperature();
    temperatureF=(temperatureC*(1.8))+32; //    F = C * 180 + 32
    seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; // should be ~1000
    Altitude=bmp.getAltitude(seaLevelPressure); // meters
    pressureMillbar=bmp.getPressure() / 100;
 }
last_microsBMP=micros();
}

/*0000000000000000000      DHTsensor     00000000000000000000000000000*/

void DHTsensor() 
{
  if((long)micros() - last_microsDHT >= 100100 || last_microsDHT==0) // Wait a few seconds between measurements. //delay(2100);
      {
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      humidity=dht.readHumidity();
      // Read temperature as Celsius
      float t = dht.readTemperature();
      // Read temperature as Fahrenheit
      float f = dht.readTemperature(true);
      airtemp=dht.readTemperature(true);
      // Check if any reads failed and exit early (to try again).
      if (isnan(h) || isnan(t) || isnan(f)) 
        {
          Serial.println("Failed to read from DHT sensor!");
          //last_micros2=micros();
          return;
        }
      // Compute heat index
      // Must send in temp in Fahrenheit!
      float hi = dht.computeHeatIndex(f, h);
      heatindex=dht.computeHeatIndex(f, h);
      }
      last_microsDHT=micros();
}

User avatar
yigiter007
 
Posts: 175
Joined: Thu Oct 03, 2013 10:37 am

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by yigiter007 »

Here is the demo code included in the adafruit library BMP_183 that also gives me the same results.

Code: Select all

#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP183.h>

// For hardware SPI:
// Connect SCK to SPI Clock, SDO to SPI MISO, and SDI to SPI MOSI
// See  http://arduino.cc/en/Reference/SPI for your Arduino's SPI pins!
// On UNO, Clock is #13, SDO/MISO is #12 and SDI/MOSI is #11

// You can also use software SPI and define your own pins!
#define BMP183_CLK  13
#define BMP183_SDO  12  // AKA MISO
#define BMP183_SDI  11  // AKA MOSI

// You'll also need a chip-select pin, use any pin!
#define BMP183_CS   10

// initialize with hardware SPI
//Adafruit_BMP183 bmp = Adafruit_BMP183(BMP183_CS);
// or initialize with software SPI and use any 4 pins
Adafruit_BMP183 bmp = Adafruit_BMP183(BMP183_CLK, BMP183_SDO, BMP183_SDI, BMP183_CS);

/**************************************************************************/
/*
    Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void) 
{
  Serial.begin(9600);
  Serial.println("BMP183 Pressure Sensor Test"); Serial.println("");
  
  /* Initialise the sensor */
  if(!bmp.begin())
  {
    /* There was a problem detecting the BMP183 ... check your connections */
    Serial.print("Ooops, no BMP183 detected ... Check your wiring!");
    while(1);
  }
}

/**************************************************************************/
/*
    Arduino loop function, called once 'setup' is complete (your own code
    should go here)
*/
/**************************************************************************/
void loop(void) 
{
    /* Display atmospheric pressue in Pascals */
    Serial.print("Pressure:    ");
    Serial.print(bmp.getPressure());
    Serial.print(" Pascals / ");
    Serial.print(bmp.getPressure() / 100);
    Serial.println(" millibar (hPa)");

    /* First we get the current temperature from the BMP085 */
    float temperature;
    temperature = bmp.getTemperature();
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" C");
    
    /* Calculating altitude with reasonable accuracy requires pressure    *
     * sea level pressure for your position at the moment the data is     *
     * converted. If you don't have these values, a 'generic' value of    *
     * 1013.25 mbar can be used (defined as SENSORS_PRESSURE_SEALEVELHPA  *
     * in sensors.h), but this isn't ideal and will give variable         *
     * results from one day to the next.                                  *
     *                                                                    *
     * You can usually find the current SLP value by looking at weather   *
     * websites or from environmental information centers near any major  *
     * airport.                                                           *
     *                                                                    *
     * For example, for Paris, France you can check the current mean      *
     * pressure and sea level at: http://BANNED/16Au8ol                   */
     

    /* Then convert the atmospheric pressure, SLP and temp to altitude    */
    /* Update this next line with the current SLP for better results      */
    float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; // should be ~1000
    Serial.print("Sea level pressure: "); 
    Serial.print(SENSORS_PRESSURE_SEALEVELHPA);
    Serial.println(" millibar/hPa");
    
    Serial.print("Altitude:    "); 
    Serial.print(bmp.getAltitude(seaLevelPressure)); 
    Serial.println(" m");
    Serial.println("");

    delay(1000);
}

User avatar
yigiter007
 
Posts: 175
Joined: Thu Oct 03, 2013 10:37 am

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by yigiter007 »

At this point I'm going to assume the BMP183 is broken. I tried it on my Uno and on a teensy with the adafruit example code library and it gives the same result. Could I get a refund or refund on the module?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Adafruit BMP183 SPI Altitude Sensor not retrieving corre

Post by adafruit_support_mike »

Sure.

Send a note to [email protected] with a link to this thread and the folks there will get you another BMP183 breakout.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”