Interfacing PIC18F4550 MCU with BMP280 sensor and Nokia 5110 LCD

This post shows how to interface PIC18F4550 microcontroller with BMP280 barometric pressure & temperature sensor and Nokia 5110 (Nokia 3310) graphical LCD which has a resolution of 84×48 pixel. In this project the BMP280 sensor is used in I2C mode and the compiler used is CCS PIC C.

To see how to interface the PIC18F4550 with the Nokia 5110 for the first time, visit the post below: Interfacing PIC18F4550 MCU with NOKIA 5110 LCD

The BMP280 sensor from Bosch Sensortec is a low cost pressure and temperature sensor with good accuracy. Because pressure changes with altitude we can use it as an altimeter with ±1 meter accuracy (pressure accuracy = ±1 hPa). Some parameters of the sensor are listed below:

Pressure range: 300…1100 hPa (equivalent to +9000…-500m above/below sea level)
Pressure resolution: 0.01 hPa ( < 10 cm)
Temperature range: -40…85 °C
Temperature resolution: 0.01 °C
Interface: I2C and SPI

The BMP280 chip works with maximum voltage of 3.6V (supply voltage range is from 1.71 to 3.6V) which means we’ve to use a 3V3 voltage regulator to supply it from a 5V source.
Also if we’re working with a 5V system (development board, microcontroller …) like the PIC18F4550 microcontroller we’ve to use a voltage level shifter (level converter) which converts the 3.3V (comes from the BMP280 chip) into 5V (goes to the PIC18F4550 MCU) and vice versa. This level shifter is for the I2C bus lines (clock and data).

Some BMP280 modules come with 3V3 voltage regulator and level shifter like the one provided by Adafruit Industries which is shown below. A module like this can be used with 3.3V or 5V system without any problem. Ardafruit BMP280 sensor breakout board

In this example I’m going to use a Chinese BMP280 module which has a 3.3V regulator and level shifter, this means circuit connection will be more easier!

Hardware Required:

  • PIC18F4550 microcontroller
  • Nokia 5110 LCD screen
  • BMP280 sensor module with 3.3V regulator and level shifter   —->  BMP280 datasheet
  • AMS1117 3V3 voltage regulator
  • 10 uF capacitor
  • 100 nF ceramic capacitor
  • 5 x 3.3k ohm resistor
  • 5 x 2.2k ohm resistor
  • Breadboard
  • Jumper wires

PIC18F4550 with BMP280 sensor and Nokia 5110 LCD circuit:
The following image shows project circuit schematic diagram.
Note that the BMP280 module shown in the circuit diagram has a 3.3V regulator and level shifter. PIC18F4550 BMP280 I2C sensor Nokia 5110 LCD circuit

All the grounded terminals are connected together.

Generally, the BMP280 module has at least 4 pins because it can work in SPI mode or I2C mode. For the I2C mode we need 4 pins: VCC, GND, SDA and SCL where:
VCC is the supply pin, it is connected to +5V
GND (ground) is connected to circuit ground (0V)
SDA is I2C bus serial data line, connected to PIC18F4550 pin RB0 (#33)
SCL is I2C bus serial clock line, connected to PIC18F4550 pin RB1 (#34).

Nokia 5110 LCD pins are connected to PIC18F4550 microcontroller as follows (each one through voltage divider where each voltage divider consists of 2.2k and 3.3k resistor):

RST (reset) pin is connected to PIC18F4550 RD0 (#19)
CE (chip enable) pin is connected to PIC18F4550 RD1 (#20)
DC (data/command) pin is connected to PIC18F4550 RD2 (#21)
DIN (data in)  pin is connected to PIC18F4550 RD3 (#22)
CLK (clock) pin is connected to PIC18F4550 RD4 (#27).

VCC and BL are connected to 3.3V that comes from the voltage regulator output AMS1117 3V3.
GND is connected to circuit ground (0V).

In this project the PIC18F4550 uses its internal oscillator and MCLR pin function is disabled.

PIC18F4550 MCU with BMP280 sensor and Nokia 5110 LCD

PIC18F4550 with BMP280 sensor and Nokia 5110 LCD C code:
The C code below is for CCS C compiler, it was tested with version 5.051.

To be able to compile the C code below, 2 libraries are required:
The first library is a driver for the Nokia 5110 LCD, its full name is NOKIA5110.c, download link is the one below:
Nokia 5110 LCD driver for CCS C compiler

The second library is for the BMP280 sensor which could be downloaded from the following link, its name is BMP280_Lib.c:
BMP280 CCS C driver

The installation of the two drivers is easy, just add them to project folder or CCS C drivers folder (for example C:\Program Files (x86)\CCS\Drivers).

For more details about the BMP280 library for CCS C compiler, take a look at this post:
Interfacing PIC MCU with BMP280 temperature and pressure sensor

The connection of the LCD pins RST, CE, DC, DIN and CLK is defined in the code as shown below where software SPI is used:

Software SPI is initialized as shown below:

In this project I used software SPI because the PIC18F4550 has one MSSP module which could be configured as SPI or I2C (the I2C is needed for the BMP280 sensor).

As any other I2C device, the BMP280 sensor has an I2C slave address which is 0xEC or 0xEE. This address depends on the connection of the SDO pin (used for SPI mode as serial data out or MISO), if the SDO pin is connected (directly or through resistor) to VCC (3.3V) the address will be 0xEE, and if it’s connected to GND the address will be 0xEC.

The default I2C address of the library is defined as 0xEE and my device I2C address is 0xEC.
In the code, the definition of the I2C slave address is as shown below:

The BMP280 sensor is initialized with the function BMP280_begin(MODE_NORMAL) which returns 1 if ok and 0 if error. In the code the initialization of the sensor is as shown below:

The LCD displays “connection error!” if there was an error while initializing the device.

Reading the values of temperature and pressure is done as shown below.
Note that the library returns the temperature in hundredths °C which means we’ve to divide it by 100 and it returns the pressure in Pa, to get the pressure in hPa we’ve to divide it by 100.

Temperature and pressure values are displayed on the NOKIA 5110 LCD.

1 bar = 10000 Pa = 100 hPa. ( 1 hPa = 100 Pa)
Pa: Pascal
hPa: hectoPascal

Full CCS C Code:

The following video shows my hardware circuit result:

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top