LCD 16x2 HD44780 and BMP085 pressure sensor problem

Hi folks,

I recently bought an arduino leonardo and a few sensors to play with. I’m very happy with it!
I managed to get an altitude and temperature value from the BMP085 pressure sensor and store it with an SD card. Works like a charm!

Unfortunately I then tried to display the readings from the BMP085 on my LCD screen (16X2 based on HD44780 chip) and got stuck. The LCD displays weird things. (see attached photos).

//bmp.begin();

bmp.begin();

I started stripping down my code to find the problem and I got to the following code:

#include <LiquidCrystal.h>
#include <Wire.h>
#include <SPI.h>
#define SLP 1027 //Sea Level Pressure definition

#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

LiquidCrystal lcd(3, 4, 12, 11, 10, 9);

void setup()
{
  lcd.begin(16,2);
  bmp.begin(); //When I uncomment this line I get weird stuffs on the LCD as shown on picture
}

    void loop(void)
    {  
          delay(10);
          lcd.setCursor(0, 0);
          delay(10);
          int temp=63.54;
          lcd.print("Temp:");
          lcd.print(temp);
          lcd.print("C");
          delay(10);
          lcd.setCursor(0, 1);
          delay(10);
          int alt=14332;
          lcd.print("Alt :");
          lcd.print(alt);
          lcd.print("m");
          digitalWrite(13,HIGH);
    
      delay(1000);
      digitalWrite(13,LOW);
      delay(100);
    }

Notice the line bmp.begin();
When this line is removed the display works fine. When I have this line the LCD is messed up.
It looks like the bmp085 init is messing with the LCD but I don’t know what to do next in order to debug this. Thanks in advance for your help!

I guess that I should look in the header files (.h) that I included to check if there functions with the same name! I suppose that it could create a problem but I'm not sure. I've been trying this all day and can't figure it out so plese point me in the right direction! :)

I assume the sensor is wired up as per this example, yes?

 Connections
===========
Connect SCL to analog 5/SCL
Connect SDA to analog 4/SDA
Connect VDD to 3.3V DC
Connect GROUND to common ground

For some reason, the lcd.setcursor() is generating a character instead of positioning the cursor. Ordinarily, my guess would be you have a common signal pin driving both the LCD and the Sensor.

Thanks for your answer. I connected the SDA and SCL pins of the sensor to the SDA and SCL pins of my arduino board. I assumed that it was correct. :) The LCD screen is connected in 4bits mode to pins 9 - >12 of the arduino board. I connected the RW pin of the LCD to GND, Enable to pin 4 and RS to pin 3. * Edit: I checked my ground pins and they are all connected together, no floating grounds.

It sounds like the problem is pin 3.

According to the HD44780 data sheet, RS HIGH means data, RS LOW means instruction (like setcursor). It seems like when the sensor code is running, for some reason pin 3 isn't able to go LOW (hence, the extra character instead).

Thanks for the idea. I looked at the code in Adafruit_BMP085_U.cpp but didn't find anything about pin 3. I'll, however, try to use an other port for RS to see if that changes anything.

Well... That was a quick fix. I used pin 7 for RS and now it's working! Thanks a lot for your support! I'll now try to understand why pin 3 can't go low when using BMP085...

No worries. Glad it's working. Now you just need to work out why. :)

If I find the reason why pin3 doesn't work I'll post it here but for now I have no clue.

Here are pictures of my project. I'm so happy! Thanks arduinodlb. :)

I notice there's a yellow wire running from the right of the picture to somewhere near what would be pin 3 on the Arduino. What's that wire for/carrying?

There is an unused ultrasound emitter/receiver (distance measuring) on my breadboard that I left there. That yellow wire is connected to pin 5 of the arduino board and corresponds to the trigger of the sensor. The orange wire connected to pin 6 is the echo pin of the sensor.

Notice that the picture I uploaded above was taken before I changed the RS pin from pin 3 to pin 7. (RS pin is the white wire that is coming from the LCD)

Edit: I'll try to remove that ultrasonic sensor and see if I still get errors when RS is connected to pin 3.

Ok, here are the results: Removed the ultrasonic sensor made no difference for pin 3. I then tried to connect RS to all the free pins I have to see what I get. here are the results: Pin 0: works pin 1: works pin 2: doesn't work pin 3: doesn't work pin 4: works pin 5: works pin 6: works pin 7: works pin 13: works

I checked continuity between pin 2 and 3 but there is no short circuit.

Here is a picture of my board without that ultrasound sensor. This is a configuration that doesn't work (RS is connected to pin 3).

I tried to add delays as follows:

        lcd.setCursor(0,0);
        delay(1000); //On my multimeter I read about 3.65V at that moment
        lcd.print("Temp:");
        delay(2000); //On my multimeter I read about 4.25V at that moment

That gave me the time to read the voltage value on pin 3 (RS) and you can see the results as comments in the code above. I don't know if this is meaningful...

Edit: Is it ok to post multiple times as I do on this forum?

Edit2: I tried to connect an LED to pin 2 and 3 when I use pin 7 for RS. The LED lights up briefly every second. Could that indicate that there is something in my code that sets pin 2 and 3 in a special mode?

Your code looks fine to me.

Definitely weird. I think it has to be a library issue. Which version of the libraries are you using? Can you post links to them?

Edit: OK. I just found an Arduino UNO image that has pin 2 marked as Interrupt 0 and pin 3 marked as interrupt 1, so that's probably the underlying source of the problem. Maybe when using SPI these pins get taken over? (I've never used SPI, or the interrupts on the Arduino).

Here is the link to the library I’m using for the BMP085: http://learn.adafruit.com/bmp085/using-the-bmp085-api-v2
For the LCD I’m using the standard liquidCrystal library.

For people who may be interested in setting up the same thing here is my full working code:

   #include <Wire.h>
   #include <SPI.h>
   #define SLP 1028 //Sea Level Pressure definition
   #include <LiquidCrystal.h>
  
   
    #include <Adafruit_Sensor.h>
    #include <Adafruit_BMP085_U.h>
    Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
     
     LiquidCrystal lcd(7, 4, 12, 11, 10, 9); //RS,EN,D4,D5,D6,D7
     
    void setup(void)
    {
      Serial.begin(9600); 
      Serial.println("Pressure Sensor Test"); Serial.println("");
      /* Initialise the sensor */
      if(!bmp.begin())
        {
        while(1){
          Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
          delay(1000);
        }
        }
        
        lcd.begin(16,2); //Init for a 16x2 LCD
        lcd.clear(); //Just to be sure...
    }
     
    void loop(void)
    {
      /* Get a new sensor event */
      sensors_event_t event;
      bmp.getEvent(&event);
      /* Display the results (barometric pressure is measure in hPa) */
      if (event.pressure)
        {
        /* Display atmospheric pressue in hPa in console*/
        Serial.print("Pressure: ");
        Serial.print(event.pressure);
        Serial.println(" hPa");
        /* First we get the current temperature from the BMP085 */
        float temperature;
        bmp.getTemperature(&temperature);
        //Display temperature on console
        Serial.print("Temp: ");
        Serial.print(temperature);
        Serial.println(" C");
        //Display temperature on LCD
        lcd.setCursor(0,0);
        lcd.print("Temp:");
        lcd.setCursor(9,0);
        lcd.print("     C"); //We do it this way so that The "C" is always at the same place on the screen
        lcd.setCursor(8,0);
        lcd.print(temperature); //Some of the digits will overwrite the spaces left before "C"
        
        /* Then convert the atmospheric pressure, SLP and temp to altitude */
        float seaLevelPressure = SLP;
        Serial.print("Alt: ");
        long altitude;
        altitude=bmp.pressureToAltitude(seaLevelPressure,
        event.pressure,
        temperature);
        //Display Altitude on console
        Serial.print(altitude);
        Serial.println(" m");
        Serial.println("");
        //Display Altitude on LCD
        lcd.setCursor(0,1);
        lcd.print("Alt:");
        lcd.setCursor(9,1);
        lcd.print("     m");
        lcd.setCursor(8,1);
        lcd.print(altitude);
       
        }
      else
        {
        Serial.println("Sensor error");
        }
      Serial.println("End of loop");
      delay(1000);
      digitalWrite(13,LOW);
    }

I’m new to all this so any tips to improve my code is welcome.

Cheers!

arduinodlb: Your code looks fine to me.

Definitely weird. I think it has to be a library issue. Which version of the libraries are you using? Can you post links to them?

Edit: OK. I just found an Arduino UNO image that has pin 2 marked as Interrupt 0 and pin 3 marked as interrupt 1, so that's probably the underlying source of the problem. Maybe when using SPI these pins get taken over? (I've never used SPI, or the interrupts on the Arduino).

I guess you meant I2C? I'm not using SPI

I also found this: "TWI: 2 (SDA) and 3 (SCL). Support TWI communication using the Wire library. " http://arduino.cc/en/Main/arduinoBoardLeonardo

Edit: Here we go: On the leonardo board pins 2 and 3 are used for I2C/TWI. http://arduino.cc/en/reference/wire

I have no idea what TWI is though... :) (Edit: TWI: Two Wire Interface. That's just an other name for I2C)

Well, I've gone past the edge of my current knowledge. Hopefully someone here can chime in as to what the problem could be. I'll be interested to here the answer.

Well, you placed me on the right tracks when you said:

"Edit: OK. I just found an Arduino UNO image that has pin 2 marked as Interrupt 0 and pin 3 marked as interrupt 1, so that's probably the underlying source of the problem. Maybe when using SPI these pins get taken over? (I've never used SPI, or the interrupts on the Arduino)."

It turns out that pins 2 and 3 are used for I2C/TWI on the arduino Leonardo board. I checked that by connecting the BMP085 sensor to pins 2 and 3 instead of SDA and SCL on the arduino board and it works the same. (see attached picture) See the edits I made to my previous posts for the links.

I guess that when I initialize the BMP085 it enables I2C, thus making pins 2 and 3 unavailable for anything else.

I guess that makes sense. Note to myself: Never use pins 2 and 3 when using I2C.

I sincerely thank you arduinodlb for your time, you really helped me and I learned a lot from all this. :)

Ahhh. Good.

Yes sorry, I meant I2c/TWI. It looks like they keep moving these pins around.

It looks like your separate SCL/SDA pins on the Leonardo are just hard-wired to pins 2/3.

http://www.flickr.com/photos/johngineer/7329403498/lightbox/

I wonder what the point of that is?

Correct. I just checked continuity with my multimeter and yes, they are hard-wired. No idea why they did that though...