Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 1 addition & 182 deletions content/examples/communityImplementation/bmp280.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,185 +4,4 @@ weight: 102
tags: ["BMP280"]
---

### Description

The BMP280 is a Pressure and temperature sensor accessed via I2C or SPI. The BMP280
is a simple device that requires the connection of few wires to operate.
Because of this simplicity the device can serve as a very easy project for initial Pi and
Pi4j usage. This document will explain the more simple means to connect the sensor and
access the device with existing software requiring no coding. The document will
also explain minimal coding required to allow greater flexibility.

A later section explains creating a more robust proto-type environment to better
support using various integrated circuits, see [Prototype board](/examples/communityimplementation/prototype-board/).

### Connecting Adafruit BMP280

After soldering the pins to the PCB you can use female-female jumpers to connect
directly to the Pi GPIO pins.

{{% notice note %}}
Power off the Pi with `sudo shutdown` prior to making connections.
Or if you complete these connections with power applied, connect CS to
3.3v before connecting Vin. This is required to ensure the I2C interface is
enabled, else the chip enables SPI communication protocol.
{{% /notice %}}

### Component and wiring

#### BMP280 module

The following shot is the Adafruit BMP280 module. The BMP280 chip is mounted
on a small circuit board with pins to solder in for connections.

{{< gallery >}}
{{< figure link="/assets/examples/community/BMP280/BMP280_Module.png" caption="BMP280 module" caption-position="center" caption-effect="fade" >}}
{{< /gallery >}}
{{< load-photoswipe >}}

#### 40 pin identification

This shot is the Pi with an easy to read card that identifies the 40 pin
connector.

{{< gallery >}}
{{< figure link="/assets/examples/community/BMP280/BMP280_pi_cana_card.png" caption="BMP280 pi_canca_card" caption-position="center" caption-effect="fade" >}}
{{< /gallery >}}

#### 40 pin connection

This is the BMP280 module connected to the Pi 40 pin connector.

{{< gallery >}}
{{< figure link="/assets/examples/community/BMP280/BMP280_pi_module_cmpl.png" caption="BMP280 wire_cmpl" caption-position="center" caption-effect="fade" >}}
{{< /gallery >}}

#### Header Pin/Colors – Pi 40 pin connector

| Pin | Color | Description |
| :--- | :--- | :--- |
| Vin | Red | to Pi 3.3 pin 1 |
| 3v | N/C | |
| Gnd | Brown | to Pi pin 6 |
| SCK | Green | to Pi pin 5 |
| SDO | N/C | |
| SDI | Blue | to Pi pin 3 |
| CS | Orange | to Pi pin 17 |

#### Validate Connections

Power on the Pi. Log in as usual.
Run the command `i2cdetect -y 1`. This command will display all I2C devices
connected to I2C bus 1. In this case only the BMP280 is connected so the table
should include a device 77, 0x77 being the device address of the BMP280.

{{< gallery >}}
{{< figure link="/assets/examples/community/BMP280/BMP280_i2cdetect.png" caption="BMP280 i2cdetect" caption-position="center" caption-effect="fade" >}}
{{< /gallery >}}

### Build via Maven

The Pi4j project uses git via github for the source code repository. The
main Pi4J documentation provides details of the repository and maven build process.

The project [Example Devices](https://github.com/taartspi/pi4j-example-devices)
uses the prescribed Maven build process. It is easiest to git clone the
[Example Devices](https://github.com/taartspi/pi4j-example-devices) directory
and use these steps to build. Note: each device within this directory
contains a README.md that explains the expected Hardware connections,
build process, and how to use the device via these modules.

```bash
$ mvn clean package
$ cd target/distribution
$ sudo ./runBMP280.sh
```

### Program output

```bash
[main] INFO com.pi4j.util.Console - I2C detail : com.pi4j.plugin.linuxfs.provider.i2c.LinuxFsI2C@45018215 bus : 1 address : 119
[main] INFO com.pi4j.util.Console - Temperatue C = 32.37366343149915
[main] INFO com.pi4j.util.Console - Temperatue F = 90.27259417669848
[main] INFO com.pi4j.util.Console - Pressure Pa = 97930.29684231618
[main] INFO com.pi4j.util.Console - Pressure InHg = 28.91894505802476
[main] INFO com.pi4j.util.Console - Pressure mb = 979.2223518169782
```

### Supporting BMP280 Source Code

[BMP280 source files](https://github.com/taartspi/pi4j-example-devices/blob/master/src/main/java/com/pi4j/devices/bmp280)

This is part of a larger project
[Example Devices](https://github.com/taartspi/pi4j-example-devices)

### Code

A simple example on how to use the BMP280 sensor. This requires
[Example Devices](https://github.com/taartspi/pi4j-example-devices) was built.

```java
package com.pi4j.test.devices.bmp280;

import com.pi4j.Pi4J;
import com.pi4j.devices.bmp280.BMP280Device;
import com.pi4j.plugin.linuxfs.provider.i2c.LinuxFsI2CProvider;
import com.pi4j.util.Console;

public class BMP280 {

public static void main(String[] args) throws Exception {
// ------------------------------------------------------------
// Initialize the Pi4J Runtime Context
// ------------------------------------------------------------
// Before you can use Pi4J you must initialize a new runtime
// context.
//
// The 'Pi4J' static class includes a few helper context
// creators for the most common use cases. The 'newAutoContext()'
// method will automatically load all available Pi4J
// extensions found in the application's classpath which
// may include 'Platforms' and 'I/O Providers'

var pi4j = Pi4J.newContextBuilder().add(
LinuxFsI2CProvider.newInstance()).build();

// print installed providers
System.out.println("----------------------------------------------------------");
System.out.println("PI4J PROVIDERS");
System.out.println("----------------------------------------------------------");
pi4j.providers().describe().print(System.out);
System.out.println("----------------------------------------------------------");

final Console console = new Console();
console.print("==============================================================");
console.print("startup BMP280 ");
console.print("==============================================================");

var bmpDev = new BMP280Device(pi4j, console, 1, 0x77, "info");
bmpDev.initSensor();
console.println(" Dev I2C detail " + bmpDev.i2cDetail());
console.println(" Setup ----------------------------------------------------------");

console.println(" I2C detail : " + bmpDev.i2cDetail());

double reading1 = bmpDev.temperatureC();
console.println(" Temperatue C = " + reading1);

double reading2 = bmpDev.temperatureF();
console.println(" Temperatue F = " + reading2);

double press1 = bmpDev.pressurePa();
console.println(" Pressure Pa = " + press1);

double press2 = bmpDev.pressureIn();
console.println(" Pressure InHg = " + press2);

double press3 = bmpDev.pressureMb();
console.println(" Pressure mb = " + press3);

// Shutdown Pi4J
pi4j.shutdown();
}
}
```
{{< github-readme "https://github.com/Pi4J/pi4j-example-devices/blob/67c85e0ce2fc2ef62899bd669f029062c7139667/src/main/java/com/pi4j/devices/bmp280/README.md" >}}
19 changes: 19 additions & 0 deletions themes/hugo-theme-learn/layouts/shortcodes/github-readme.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{- $url := .Get 0 -}}
{{- $rawURL := "" -}}

{{- if strings.Contains $url "github.com" -}}
{{- $rawURL = replace $url "github.com" "raw.githubusercontent.com" -}}
{{- $rawURL = replace $rawURL "/blob/" "/" -}}
{{- else -}}
{{- errorf "Invalid GitHub URL provided to github-readme shortcode: %s" $url -}}
{{- end -}}

{{- with try (resources.GetRemote $rawURL) -}}
{{- with .Err -}}
{{- errorf "Unable to fetch README from %s: %s" $rawURL . -}}
{{- else with .Value -}}
{{- .Content | markdownify -}}
{{- else -}}
{{- errorf "Unable to get remote resource %q" $rawURL -}}
{{- end -}}
{{- end -}}