Skip to content

Commit

Permalink
Merge pull request #413 from pimoroni/patch-pms3005-i2c
Browse files Browse the repository at this point in the history
PMS5003: Support for i2c version.
  • Loading branch information
Gadgetoid authored Jul 22, 2022
2 parents 6829b33 + 103fe19 commit c5d4865
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 19 deletions.
2 changes: 1 addition & 1 deletion drivers/pms5003/pms5003.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ target_sources(${DRIVER_NAME} INTERFACE
target_include_directories(${DRIVER_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})

# Pull in pico libraries that we need
target_link_libraries(${DRIVER_NAME} INTERFACE pico_stdlib hardware_uart hardware_gpio)
target_link_libraries(${DRIVER_NAME} INTERFACE pico_stdlib pimoroni_i2c hardware_uart hardware_gpio)
39 changes: 32 additions & 7 deletions drivers/pms5003/pms5003.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "pico/stdlib.h"
#include "hardware/uart.h"
#include "common/pimoroni_common.hpp"
#include "common/pimoroni_i2c.hpp"
#include <cstring>

constexpr char PMS5003_SOF[] = "\x42\x4d";
Expand All @@ -14,6 +16,8 @@ constexpr uint PMS5003_MAX_RESET_TIME = 20000;
constexpr uint PMS5003_MAX_RESP_TIME = 5000;
constexpr uint PMS5003_MIN_CMD_INTERVAL = 100;

const uint8_t PMSA003I_DEFAULT_I2C_ADDRESS = 0x12;


class PMS5003 {
public:
Expand Down Expand Up @@ -45,11 +49,14 @@ class PMS5003 {
uart_init(uart, 9600);
gpio_init(pin_tx);gpio_set_function(pin_tx, GPIO_FUNC_UART);
gpio_init(pin_rx);gpio_set_function(pin_rx, GPIO_FUNC_UART);
gpio_init(pin_reset);gpio_set_function(pin_reset, GPIO_FUNC_SIO);gpio_set_dir(pin_reset, GPIO_OUT);gpio_put(pin_reset, false);
gpio_init(pin_enable);gpio_set_function(pin_enable, GPIO_FUNC_SIO);gpio_set_dir(pin_enable, GPIO_OUT);gpio_put(pin_enable, true);

reset();
common_init();
};

PMS5003(pimoroni::I2C *i2c, uint pin_reset, uint pin_enable) : i2c(i2c), pin_reset(pin_reset), pin_enable(pin_enable) {
common_init();
}

~PMS5003() {};

void reset() {
Expand All @@ -58,13 +65,20 @@ class PMS5003 {
reset_input_buffer();
sleep_ms(100);
gpio_put(pin_reset, true);
if(i2c) {
sleep_ms(1000);
}
};

bool read(response_data &data) {
reset_input_buffer();

// Read the 32 byte transaction - SOF + Size + Data + CRC
uart_read_blocking(uart, buffer, 32);
if(i2c) {
i2c->read_blocking(PMSA003I_DEFAULT_I2C_ADDRESS, buffer, 32, false);
} else {
uart_read_blocking(uart, buffer, 32);
}

// test the checksum matches, if not quit early with a false return value
uint16_t checksum = (buffer[30] << 8) | buffer[31];
Expand Down Expand Up @@ -98,17 +112,28 @@ class PMS5003 {
}

private:
uart_inst_t *uart;
uint pin_tx;
uint pin_rx;
// I2C mode
pimoroni::I2C *i2c = nullptr;

// UART mode
uart_inst_t *uart = nullptr;
uint pin_tx = pimoroni::PIN_UNUSED;
uint pin_rx = pimoroni::PIN_UNUSED;
uint pin_reset;
uint pin_enable;

uint8_t buffer[64];

void reset_input_buffer() {
if(i2c) return;
while(uart_is_readable(uart)) {
uart_getc(uart);
}
};

void common_init() {
gpio_init(pin_reset);gpio_set_function(pin_reset, GPIO_FUNC_SIO);gpio_set_dir(pin_reset, GPIO_OUT);gpio_put(pin_reset, false);
gpio_init(pin_enable);gpio_set_function(pin_enable, GPIO_FUNC_SIO);gpio_set_dir(pin_enable, GPIO_OUT);gpio_put(pin_enable, true);
reset();
};
};
17 changes: 6 additions & 11 deletions examples/breakout_pms5003/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
set(OUTPUT_NAME pms5003_demo)
add_executable(pms5003_demo pms5003_demo.cpp)
target_link_libraries(pms5003_demo pico_stdlib pms5003)
pico_add_extra_outputs(pms5003_demo)

add_executable(
${OUTPUT_NAME}
pms5003_demo.cpp
)

# Pull in pico libraries that we need
target_link_libraries(${OUTPUT_NAME} pico_stdlib pms5003)

# create map/bin/hex file etc.
pico_add_extra_outputs(${OUTPUT_NAME})
add_executable(pmsa003i_demo pmsa003i_demo.cpp)
target_link_libraries(pmsa003i_demo pico_stdlib pms5003)
pico_add_extra_outputs(pmsa003i_demo)
45 changes: 45 additions & 0 deletions examples/breakout_pms5003/pmsa003i_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>
#include "pico/stdlib.h"
#include "common/pimoroni_common.hpp"
#include "common/pimoroni_i2c.hpp"

#include <cstring>

#include "pms5003.hpp"

using namespace pimoroni;

I2C i2c(4, 5, 100000);
PMS5003 pms5003(&i2c, 9, 10);
PMS5003::response_data data;

int main() {
stdio_init_all();

// Enable Enviro power supply!
gpio_init(11);
gpio_set_dir(11, GPIO_OUT);
gpio_put(11, true);

while(true){
bool result = pms5003.read(data);
if(result){
printf("%04x ", data.pm_1_0); // PM1.0 ug/m3 (ultrafine particles)
printf("%04x ", data.pm_2_5); // PM2.5 ug/m3 (combustion particles, organic compounds, metals)
printf("%04x ", data.pm_10); // PM10 ug/m3 (dust, pollen, mould spores)
printf("%04x ", data.pm_1_0_ao); // PM 1.0 under atmospheric environment
printf("%04x ", data.pm_2_5_ao); // PM 2.5 under atmospheric environment
printf("%04x ", data.pm_10_ao); // PM 10 under atmospheric environment
printf("%04x ", data.pm_0_3_1l); // PM 0.3 in 0.1L of air
printf("%04x ", data.pm_0_5_1l); // PM 0.5 in 0.1L of air
printf("%04x ", data.pm_1_0_1l); // PM 1.0 in 0.1L of air
printf("%04x ", data.pm_2_5_1l); // PM 2.5 in 0.1L of air
printf("%04x ", data.pm_5_1l); // PM 5 in 0.1L of air
printf("%04x ", data.pm_10_1l); // PM 10 in 0.1L of air
printf("\n");
}
sleep_ms(100);
};

return 0;
}

0 comments on commit c5d4865

Please sign in to comment.