How to print SDS011 data to SSD1306 OLED by SPI

Hello

I've been trying to print the SDS011 sensor output to OLED display. but from everything I've tried so far, I cannot get the OLED to start at all while getting sensor data.
My goals are

  1. get the OLED display to work with the SDS011 sensor at the same time.
  2. Print the sensor data to OLED display.
    Here is the spaghetti code I wrote
#include "SdsDustSensor.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

int rxPin = 0;
int txPin = 1;
SdsDustSensor sds(rxPin, txPin);

void setup() {
  Serial.begin(9600);
  
  sds.begin();
  Serial.println(sds.queryFirmwareVersion().toString()); // prints firmware version
  Serial.println(sds.setActiveReportingMode().toString()); // ensures sensor is in 'active' reporting mode
  Serial.println(sds.setContinuousWorkingPeriod().toString()); // ensures sensor has continuous working period - default but not recommended

  
  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.begin();
  display.display();
  delay(2000); 
  display.clearDisplay();


}


void loop() {

  
  PmResult pm = sds.readPm();
  if (pm.isOk()) {
    Serial.print("PM2.5 = ");
    Serial.print(pm.pm25);
    Serial.print(", PM10 = ");
    Serial.println(pm.pm10);
    
    float a = p25();

    float b = p10();

  } else {
    // notice that loop delay is set to 0.5s and some reads are not available
    Serial.print("Could not read values from sensor, reason: ");
    Serial.println(pm.statusToString());
  }

  display.setCursor(0,0);
  display.print("PM 2.5");
  display.print(a);

  display.setCursor(0,1);
  display.print("PM10");
  display.print(b);
  delay(500);
}

Add display.display(); after you print the data to the display in loop, immediately before the delay(500); . The display buffer does not get sent to the actual display until you call that function.

int rxPin = 0;
int txPin = 1;
SdsDustSensor sds(rxPin, txPin);
...
  Serial.begin(9600);

What type of Arduino are you using?

I'm currently using Arduino UNO.

In that case you cannot use pins 0 & 1 to connect to the sensor. Pick 2 other pins.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.