先上代码:
库文件是在guihub上的大神写的https://github.com/jbanaszczyk,我拿来小改下用以支持5003ST
#include <Arduino.h> #include <pms.h> //////////////////////////////////////// #if defined PMS_DYNAMIC Pms5003 *_pms; #define pms (*_pms) #else Pms5003 pms; #endif //////////////////////////////////////// auto lastRead = millis(); void setup( void ) { Serial.begin( 115200 ); while ( !Serial ) { }; Serial.println( "PMS5003ST" ); #if defined PMS_DYNAMIC _pms = new Pms5003(); #else pms.begin(); #endif // pms.write( Pms5003::cmdWakeup ); // pms.write( Pms5003::cmdModePassive ); // Serial.println( pms.getDataSize() ); // pms.write( Pms5003::cmdModeActive ); } //////////////////////////////////////// void loop( void ) { const int n = Pms5003::Reserved; Pms5003::pmsData data[ n ]; Pms5003::PmsStatus status = pms.read( data, n ); switch ( status ) { case Pms5003::OK: { Serial.println( "_________________" ); auto newRead = millis(); Serial.print( "Wait time " ); Serial.println( newRead - lastRead ); lastRead = newRead; for ( size_t i = Pms5003::PM1dot0; i < n; ++i ) { if(i == n-3) Serial.print( float(data[ i ]/1000) ); if(i == n-2 || i == n-1) Serial.print( float(data[ i ]/10)); else Serial.print( data[ i ] ); Serial.print( "\t" ); Serial.print( Pms5003::dataNames[ i ] ); Serial.print( " [" ); Serial.print( Pms5003::metrics[ i ] ); Serial.print( "]" ); Serial.println(); } break; } case Pms5003::noData: break; default: Serial.println( "_________________" ); Serial.println( Pms5003::errorMsg[ status ] ); }; }
接线图:
PIN1-----> 5V
PIN2----->GND
PIN5----->D8(要接电平转换板,PIN5要3.3V,而D8输出是5V;或者直接用 USB转TTL 连接电脑也可以)
在来张实拍图: