SPI for LCD and Pressure Sensor

I am using an Arduino Uno with a BMP183 barometric pressure/altitude sensor, and a Thermistor.

The output of the devices will be displayed on a 20x4 LCD. The default MOSI, MISO, SCK, and SS pins (11, 12, 13, 10 on the Arduino) are being used by the LCD, and I want to use any remaining pins on the Arduino for the BMP183 using SPI. I am having trouble writing the code correctly. I wanted to use pins 2-5 for the BMP183 using SPI.

Currently the Thermistor integration works fine, but the BMP183 is not showing reading on the LCD. I presume that I don't have the setup correct for the BMP183. Any help or feedback would be greatly appreciated.

This is what I have so far:

// which analog pin to connect
#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000
#define ANEMOMETER A1

#include <LiquidCrystal.h>

#include <SPI.h>
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3);
SPI.setClockDivider(SPI_CLOCK_DIV4);

#include <Adafruit_Sensor.h>
#include <Adafruit_BMP183.h>
#define BMP183_CLK 5
#define BMP183_SDO 4 // AKA MISO
#define BMP183_SDI 3 // AKA MOSI
#define BMP183_CS 2
Adafruit_BMP183 bmp = Adafruit_BMP183(BMP183_CLK, BMP183_SDO, BMP183_SDI, BMP183_CS);

// BS E D4 D5 D6 D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

//Thermistor-------------------------------------------------------------
int samples[NUMSAMPLES];

void setup(void) {
Serial.begin(9600);
analogReference(EXTERNAL);
lcd.begin(20, 4);
bmp.begin();

}

void loop(void) {
uint8_t i;
float average;

// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples = analogRead(THERMISTORPIN);

  • delay(10);*

  • }*

  • // average all the samples out*

  • average = 0;*

  • for (i=0; i< NUMSAMPLES; i++) {*
    _ average += samples*;_
    _
    }_
    _
    average /= NUMSAMPLES;*_

* Serial.print("Average analog reading ");*
* Serial.println(average);*

* // convert the value to resistance*
* average = 1023 / average - 1;*
* average = SERIESRESISTOR / average;*
* Serial.print("Thermistor resistance ");*
* Serial.println(average);*

* float steinhart;*
* steinhart = average / THERMISTORNOMINAL; // (R/Ro)*
* steinhart = log(steinhart); // ln(R/Ro)*
_ steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
* steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)*
* steinhart = 1.0 / steinhart; // Invert*
* steinhart -= 273.15; // convert to C*
steinhart = steinhart*9.0/5.0 + 32.0; // convert to F_

* lcd.setCursor(0, 0);*
* lcd.print("Temp: ");*
* lcd.print(steinhart);*
* lcd.print(" F");*

* //BMP183-----------------------------------------------------------------------------*

* lcd.setCursor(0, 1);*
* lcd.print("Pressure: ");*
* lcd.print( bmp.getPressure() );*
* lcd.print(" Pa");*

* //lcd.setCursor(0, 2);*
* //lcd.print("Temp: ");*
* //lcd.print( bmp.getTemperature() );*
* //lcd.print(" C");*

* float seaLevelPressure = 1013.25;*
* lcd.setCursor(0, 2);*
* lcd.print("Altitude: ");*
* lcd.print(bmp.getAltitude(seaLevelPressure));*
* lcd.print(" m");*
* delay(1000);*
}

Hi mbeard

First, could you edit your post and put [nobbc] [code] [/code] [/nobbc] tags around the program. It makes it much easier to read.

About your problem ...

the BMP183 is not showing reading on the LCD.

Do you mean that you are not reading correctly from the sensor, or that you cannot display the values on the LCD?

If it is the second case, have you tested the LCD with an example program?

Regards

Ray

Hackscribble,

Thanks for the reply! The LCD is working, but is not getting a correct signal from the BMP183. I don't think my code is properly setup

If this helps, my LCD setup is as follows:

Here is also some BMP183 info:

Please put code tags around your code in the original post.

The LCD is working, but is not getting a correct signal from the BMP183.

The LCD does not get a signal from the sensor. Your program reads the sensor, does some processing and then writes text to the LCD.

It looks like your program has Serial.print statements in it. Have you checked that your calculations are giving the right results (which means the problem is in writing those results to the LCD)?

The LCD is working

So, just to be clear, you have run another program that writes to the LCD and it displays correctly?

// which analog pin to connect
#define THERMISTORPIN A0         
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000      
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25   
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000   
#define ANEMOMETER A1

#include <LiquidCrystal.h>


#include <SPI.h>
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3);
SPI.setClockDivider(SPI_CLOCK_DIV4); 



#include <Adafruit_Sensor.h>
#include <Adafruit_BMP183.h>
#define BMP183_CLK  5
#define BMP183_SDO  4  // AKA MISO
#define BMP183_SDI  3  // AKA MOSI
#define BMP183_CS   2
Adafruit_BMP183 bmp = Adafruit_BMP183(BMP183_CLK, BMP183_SDO, BMP183_SDI, BMP183_CS);

//                BS  E  D4 D5  D6 D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

//Thermistor-------------------------------------------------------------
int samples[NUMSAMPLES];
 
void setup(void) {
  Serial.begin(9600);
  analogReference(EXTERNAL);
  lcd.begin(20, 4);
  bmp.begin();

}
 
void loop(void) {
  uint8_t i;
  float average;
 
  // take N samples in a row, with a slight delay
  for (i=0; i< NUMSAMPLES; i++) {
   samples = analogRead(THERMISTORPIN);
   delay(10);

  }
 
  // average all the samples out
  average = 0;
  for (i=0; i< NUMSAMPLES; i++) {
     average += samples;
  }
  average /= NUMSAMPLES;
 
  Serial.print("Average analog reading "); 
  Serial.println(average);
 
  // convert the value to resistance
  average = 1023 / average - 1;
  average = SERIESRESISTOR / average;
  Serial.print("Thermistor resistance "); 
  Serial.println(average);
 
  float steinhart;
  steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert to C
  steinhart = steinhart*9.0/5.0 + 32.0;        // convert to F
    
    
              
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(steinhart);
  lcd.print(" F"); 
  
  //BMP183-----------------------------------------------------------------------------
 
  lcd.setCursor(0, 1);
  lcd.print("Pressure: ");
  lcd.print( bmp.getPressure() );
  lcd.print(" Pa");
  
  
  
  //lcd.setCursor(0, 2);
  //lcd.print("Temp: ");
  //lcd.print( bmp.getTemperature() );
  //lcd.print(" C");
  
  float seaLevelPressure = 1013.25;
  lcd.setCursor(0, 2);
  lcd.print("Altitude: ");
  lcd.print(bmp.getAltitude(seaLevelPressure));
   lcd.print(" m");

  delay(1000);
}

[code]

My code has serial prints in it so I can track readings in the compiler window. I think my calculations are correct.

When I try to use this BMP183 test code, the serial window says there is a wiring problem, which leads me to think that my pin assignments/SPI setup are incorrect.

BMP Test code:

#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

SPI.setBitOrder()
SPI.setClockDivider() 

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

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

// 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://bit.ly/16Au8ol                   */
     

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

    delay(1000);
}
[code]

In your main program, you used pin 5 as software SPI CLK pin for the sensor.

In the test program, you use pin 13, which will clash with the hardware SPI for the LCD.

The MISO and MOSI pins are also different between the test program and the main program.