Hello, I'm trying to display the output from a pressure sensor into a display. I don't know how to connect both to the I2C bus. I also don't know how to make the code to display it in the OLED display. Thank you
This is the code for the display (example):
#include <Wire.h> // I2C library already built in Arduino IDE
#include <LiquidCrystal_I2C.h> //library via Include Library > Manage Library >
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);
//0x27 is i2c address, EN,RW ,RS ,D4,D5,D6,D7
void setup()
{
lcd.begin(16,2); // initializing the LCD 16 x 2
lcd.setBacklightPin(3,POSITIVE); // Enable or Turn On the backlight
lcd.setBacklight(HIGH);
lcd.home();
lcd.print("SerialLCD ESP D1"); // Start Print text to Line 1
lcd.setCursor(0, 1);
lcd.print("-----------------------"); // Start Print Test to Line 2
}
void loop()
{
}
This is the code for the Adafruit MPL3115A2:
/**************************************************************************/
/*!
@file Adafruit_MPL3115A2.cpp
@author K.Townsend (Adafruit Industries)
@license BSD (see license.txt)
Example for the MPL3115A2 barometric pressure sensor
This is a library for the Adafruit MPL3115A2 breakout
----> https://www.adafruit.com/products/1893
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
// Power by connecting Vin to 3-5V, GND to GND
// Uses I2C - connect SCL to the SCL pin, SDA to SDA pin
// See the Wire tutorial for pinouts for each Arduino
// http://arduino.cc/en/reference/wire
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
void setup() {
Serial.begin(9600);
Serial.println("Adafruit_MPL3115A2 test!");
}
void loop() {
if (! baro.begin()) {
Serial.println("Couldnt find sensor");
return;
}
float pascals = baro.getPressure();
Serial.print(pascals/3377); Serial.println(" Inches (Hg)");
float altm = baro.getAltitude();
Serial.print(altm); Serial.println(" meters");
float tempC = baro.getTemperature();
Serial.print(tempC); Serial.println("*C");
delay(250);
}
Both the LCD and pressure sensor seem to be 5V devices. So they can both connect to the I2C bus. Many devices can connect to the same 2 I2C pins as long as each has a unique address.
You will make your life easier if you install the hd44780 library for your I2C LCD. For an I2C LCD display to work, the I2C address and the I2C backpack to LCD pin mapping must be correct. If the library default settings for either or both are not correct the LCD will not work. You can try to figure out the right pin mapping and use an I2C scanner to find the address, but if you install and use the hd44780 library that is done automatically by the library.
To install the hd44780 library. The hd44780 library is the best available for I2C LCDs. The library is available in the Library Manager. Go to Library Manager (in the IDE menus, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.
The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.
Ok, thanks it worked.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
