Microchip | The Embedded Control Solutions Company Register Login
Browse All
Microchip Technology
 369 Code Snippets

Interfacing the MikroElektronika WeatherClick (SPI)

Microchip Technology

Check out this project's MPLAB Xpress Webpage to view the complete code example along with a detailed step by step write-up and description, or visit the PIC18F25K42 Device Product Page for more information.

About this Code Example


The objective of this project is to demonstrate how to use MPLAB Xpress IDE to interface the Bosch BME280 Environmental Sensor on the MikroElektronika Weather Click board using the Serial Peripheral Interface (SPI) module on the PIC18F25K42 device. The goal is to create a PIC powered CuriosityHPC "weather station" that detects environmental humidity, pressure, and temperature from the Bosch BME280 sensor over SPI in real time. The raw data collected over the SPI bus will be arithmetically processed and then transmitted through the UART module to a serial terminal display.

Board Connection / Setup:

The development board used for this example was the Curiosity HPC Development Board (DM164136). The click board used for the environmental sensing was the MikroElektronika Weather Click, and the hardware used for the USB to UART serial converter was the MCP2200 breakout module. The screenshots below show what the board setup should look like once everything has been connected, as well as the MCC pin manager window that shows all of the pins assigned and implemented in this project.

Curiosity HPC with MikroElektronika WeatherClick & MCP2200 Breakout Module:

MCC Pin Manager: Grid View:

SPI Module Setup:

The MikroElektronika Weather Click board has an integrated environmental sensing unit (BME280) that can detect temperature, pressure and humidity. The Weather Click Board was designed to communicate with the target microcontroller using SPI or I2C interface, acting as a slave for either protocol. For the purposes of this example the target microcontroller is the PIC18F25K42 which will interface the BME280 environmental sensor on the Weather Click board using the SPI protocol. It is important that when configuring the SPI module as a master on the bus, that you ensure that SPI 'mode' is matched up across devices on the bus. The SPI slave in this example supports SPI mode '00' (CPOL = CPHA = 0) in respect to the clock polarity and phase settings.

When setting up the PIC18F25K42 (SPI Master) the Clock Polarity (CKP) and Clock Edge (CKE) bits are used to configure the SPI mode. For this example, the CKE bit should be set (CKE = 1) and the CKP bit should be cleared (CKP = 0) to match the slave device SPI edges. The slave select interface of the slave device is active-low according to the device datasheet, so when configuring the master device be sure to make the slave select signal active-low as well. Refer to the BME280 Environmental Sensor device datasheet for specifications about SPI write/read protocol and timing. For this example the SPI master (PIC18F25K42) SCK line was programmed onto RB1, the SDI line was tied to RB2, and the SDO line was tied to RB3. The slave select line (CS) was programmed onto pin RA3 and configured to start high since the SPI slave has an active-low slave select. All of these SPI pins were mapped in MCC.

UART Module Setup:

The UART module of the PIC18F25K42 is implemented in this example to display the post processed and calculated temperature, pressure, and humidity results in real time via serial port to a PC. In order to implement the UART module in this code example, MCC must be opened and the UART1 module was added to the project. The screenshot below shows the configuration settings used in this project. The UART was configured in Asynchronous 8-bit mode at a baud rate of 9600 and transmission was enabled. In addition to this the "Redirect STDIO to UART" software setting was enabled. The UART RX pin was configured on pin RB4.

UART1 MCC Configuration Settings:

Converting SPI Raw Data to Temperature, Pressure and Humidity:

The SPI module reads back the temperature and pressure as 20-bit signed values, and reads back humidity as a 16-bits signed value. These sensor readings captured by the SPI module are raw data measurements from the BME280 on the Weather Click board, and a calculation/compensation routine must be performed to turn them into readings that make sense. Please note that the BME280 Environmental Sensor has compensation parameters programmed into the device NVM that must be read/stored to be used in post processing of the raw SPI data. Below are the calculation routines that are implemented in this code example to convert the raw SPI temperature, pressure and humidity readings to the values displayed on the terminal output. The calculations needed to convert the raw sensor values to the displayed results are also available in the BME280 Device Datasheet.

Temperature Calculation / Compensation Routine:

    var1 = (((double)temp_raw.raw)/16384.0 - ((double)digT1)/1024.0)*((double)digT2);
    var2 = ((((double)temp_raw.raw)/131072.0-((double)digT1)/8192.0)*(((double)temp_raw.raw)/131072.0-((double)digT1)/8192.0))*((double)digT3);
    t_fine = (int32_t)(var1+var2);
    T = (var1 + var2) / 5120.0;

Pressure Calculation / Compensation Routine:

    var1 = ((double)t_fine/2.0)-64000.0;
    var2 = var1 * var1 * ((double)digP6) / 32768.0;
    var2 = var2 + var1 * ((double)digP5) * 2.0;
    var2 = (var2/4.0)+(((double)digP4)*65536.0);
    var1 = (((double)digP3)*var1*var1/524288.0+((double)digP2)*var1)/524288.0;
    var1 = (1.0 + var1 / 32768.0)*((double)digP1);
    if (var1 == 0.0)
        P = 0;
    P = 1048576.0 - (double)press_raw.raw;
    P = (P-(var2/4096.0))*6250.0 / var1;
    var1 = ((double)digP9)*P*P/2147483648.0;
    var2 = P*((double)digP8) / 32768.0;
    P = P + (var1 + var2 + ((double)digP7))/16.0;

Humidity Calculation / Compensation Routine:

    var1 = (t_fine - ((int32_t) 76000));
    var1 = (((((temp_raw.raw << 14)-(((int32_t) digH4) << 20)-(((int32_t) digH5) * var1))+((int32_t) 16384)) >> 15)*(((((((var1 * ((int32_t) digH6)) >> 10)*(((var1 * ((int32_t) digH3)) >> 11)+((int32_t) 32768))) >> 10)+((int32_t) 2097152))*((int32_t) digH2) + 8192) >> 14));
    var1 = (var1 - (((((var1 >> 15)*(var1 >> 15)) >> 7)*((int32_t) digH1)) >> 4));
    var1 = (var1 < 0 ? 0 : var1);
    var1 = (var1 > 419430400 ? 419430400 : var1);
    H = (uint32_t) (var1 >> 12);
    H = H / 1024.0;

UART Terminal Results:

The figure below illustrates what the serial output from the PIC18F25K42 should look like, and the format that the environmental sensor values will be returned in. The application returns temperature(degC), pressure(Pa), and relative humidity. The PIC microcontroller reads the sensor values using the SPI protocol every 500ms, and with every read the device ID is also verified and printed to ensure that a valid SPI connection is being made and that the slave device is working correctly.


This project provides a code example of interfacing with a more complex SPI slave device that requires full-duplex communication and shows how to send/receive data using the SPI protocol. Please refer to the PIC18F25K42 datasheet or the MikroElekronika Weather Click board product page for more information or specifications. By interfacing the Weather Click board with a PIC18F25K42 using the SPI protocol on the Curiosity HPC Development Board, this code example created a real time weather station that transmits environmental sensor data serially to a PC.


Release History

  • Dec 4, 2018

    Version: 1.0.0

    Initial Release

Download Contact the Developer

*Required Fields

  • Current Version:1.0.0
  • Created:Dec 4, 2018
  • Updated:Dec 4, 2018
  • Downloads:516
  • C Compiler:XC8 (v1.45)
  • Development Tools:Curiosity High Pin Count (HPC) Development Board
  • Supported Devices:PIC18F25K42

Corporate News


Stay Connected

Stay up to date with the latest news from Microchip Technology.