Does the HM-10 interfere with the SGP-30 sensor here?

  arduino, arduino-nano, c++

Schematic of the circuit

Hello everyone,

I am working on a circuit that measures the quality of the air. So far I have used the DHT-11 and the Adafruit SGP-30 sensors. The logic behind the code is the following: the sensors measure their parameters, which are written on an SD card via the SDshield. Here comes the BLE part. When I connect a phone via BLE (HM-10) and press a button on a separated developed android app (the Arduino receives a "1" from the phone) it starts to send the data stored on the SD through Bluetooth.
The only problem so far is that when I power on the board it initializes the sensors but the SGP-30 measurements are corrupted. Which means that instead of measurements of the sensor it displays(on the serial comm window) random symbols such as "[email protected]". In my opinion the problem is that both the SGP-30 and the SDshield use serial ports and they overlap. Any suggestions on how to proceed in order to fix this?

The code:

#include <DHT.h>
#include <DHT_U.h>
#include <Wire.h>
#include "Adafruit_SGP30.h"
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

Adafruit_SGP30 sgp;
SoftwareSerial BTserial(8,9); 
// set up variables using the SD utility library functions:
File myFile;
Sd2Card card;
SdVolume volume;
SdFile root;
int sensorValue;
char c=' ';
bool NL = true;
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;

#define DHTPIN 2     // Digital pin connected to the DHT sensor 
#define DHTTYPE    DHT11     // DHT 11

uint32_t getAbsoluteHumidity(float temperature, float humidity) {
    // approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
    const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
    const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
    return absoluteHumidityScaled;
}

// Initializare senzor DHT11.
DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {
  Serial.begin(9600);
  //initialize BT serial
   BTserial.begin(9600);  
    Serial.println("BTserial started at 9600");
  //initialize SGP30 sensor
  while (!Serial) { delay(10); } // Wait for serial console to open!


  if (! sgp.begin()){
    Serial.println("SGP sensor not found :(");
    
  }
  Serial.print("Found SGP30 serial #");
  Serial.print(sgp.serialnumber[0], HEX);
  Serial.print(sgp.serialnumber[1], HEX);
  Serial.println(sgp.serialnumber[2], HEX);

 
  // Initialize dht sensor.
  dht.begin();
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);

  // Set delay between sensor readings based on sensor details.
  
}

int counter = 0;

void loop() {
  // Delay between measurements.
  delay(10000);

  

  //DHT-11 commands
  // Get temperature event and print its value.
  sensors_event_t event;
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println(F("Error reading temperature!"));
  }
  else {
    Serial.print(F("Temperature: "));
    Serial.print(event.temperature);
    Serial.println(F("°C"));
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    Serial.println(F("Error reading humidity!"));
  }
  else {
    Serial.print(F("Humidity: "));
    Serial.print(event.relative_humidity);
    Serial.println(F("%"));
  }
  // If you have a temperature / humidity sensor, you can set the absolute humidity to enable the humditiy compensation for the air quality signals
  //float temperature = 22.1; // [°C]
  //float humidity = 45.2; // [%RH]
  //sgp.setHumidity(getAbsoluteHumidity(temperature, humidity));

  if (! sgp.IAQmeasure()) {
    Serial.println("Measurement failed");
    return;
  }
  Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppbt");
  Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");

  if (! sgp.IAQmeasureRaw()) {
    Serial.println("Raw Measurement failed");
    return;
  }
  Serial.print("Raw H2 "); Serial.print(sgp.rawH2); Serial.print(" t");
  Serial.print("Raw Ethanol "); Serial.print(sgp.rawEthanol); Serial.println("");
 
  counter++;
  if (counter == 30) {
    counter = 0;

    uint16_t TVOC_base, eCO2_base;
    if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
      Serial.println("Failed to get baseline readings");
      return;
    }
    Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
    Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);
  }
   //create SD file
  myFile = SD.open("mas.txt", FILE_WRITE);
  if (myFile) {
    myFile.println(sensorValue);
    Serial.println(" amScrisMQ");
    myFile.println(event.temperature);
    myFile.println(event.relative_humidity);
    Serial.println(" amScrisDHT");
    myFile.println(sgp.TVOC);  
    myFile.println(sgp.eCO2);
    myFile.println(sgp.rawH2);
    myFile.println(sgp.rawEthanol);
    Serial.println(" amScrisSGP");
    myFile.close();
  }
  // Read from the Bluetooth module and send to the Arduino Serial Monitor
   if (BTserial.available())
    {
        c = BTserial.read();
    }
   if (c == 1){
      
      myFile = SD.open("mas.txt"); 
      if (myFile) {
      Serial.println("test.txt:");
      // read from the file until there's nothing else in it:
    while (myFile.available()) {
        BTserial.write(myFile.read());
        Serial.println(" amCititSD");
      }
      // close the file:
      myFile.close();
      Serial.println(" aminschisSD");
      }
      SD.remove("mas.txt");
      Serial.println(" amStersSD");
      myFile = SD.open("mas.txt", FILE_WRITE);
      Serial.println(" amCreatSD");
}
}```

Source: Windows Questions C++

LEAVE A COMMENT