1

How to combine two different sensor sketches to one complete sketch for a complete circuit that attaches to one Arduino Uno?

Sharp Dust Sensor

int measurePin = A0; // Connect dust sensor to Arduino A0 pin
int ledPower = D8;   // Connect 3 led driver pins of dust sensor to Arduino D2

int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;

float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;

void setup(){
  Serial.begin(9600);
  pinMode(ledPower,OUTPUT);
}

void loop(){
  digitalWrite(ledPower,LOW); // power on the LED
  delayMicroseconds(samplingTime);

  voMeasured = analogRead(measurePin); // read the dust value

  delayMicroseconds(deltaTime);
  digitalWrite(ledPower,HIGH); // turn the LED off
  delayMicroseconds(sleepTime);

  // 0 - 5V mapped to 0 - 1023 integer values
  // recover voltage
  calcVoltage = voMeasured * (5.0 / 1024.0);
  dustDensity = 0.17 * calcVoltage - 0.1;

  Serial.print("Raw Signal Value (0-1023): ");
  Serial.print(voMeasured);

  Serial.print(" - Voltage: ");
  Serial.print(calcVoltage);

  Serial.print(" - Dust Density: ");
  Serial.println(dustDensity); // unit: mg/m3

  delay(1000);
}

MQ-135 Sensor

int airquality = A2;
void setup()
{
  Serial.begin(9600);

}
void loop()
{

  int sensorValue = analogRead(A2);
  Serial.print("Air Quality = ");
  Serial.print(sensorValue);

  Serial.print("*PPM");
  Serial.println();
  delay(1000);


}
1
  • it is unclear what you are asking .... combine 2 sensors has multiple meanings .......... please ask a detailed question
    – jsotola
    Feb 28, 2019 at 2:33

2 Answers 2

3

I will leave a simplified answer and I would recommend to read https://www.makeuseof.com/tag/getting-started-with-arduino-a-beginners-guide/ or similar to get a understanding of how to code.

An Arduino sketch has a structure.

  1. Initialising values
  2. A void setup() this code only run once the MCU is started
  3. A void loop() and this code runs continuously.

So to merge two sketches you need to combine the code that is in the same sections and of course avoid duplicate code and ensure that you have unique variable names

Sharp Dust Sensor

int measurePin = A0; // Connect dust sensor to Arduino A0 pin
int ledPower = D8;   // Connect 3 led driver pins of dust sensor to Arduino D2

void setup(){
  Serial.begin(9600);
  pinMode(ledPower,OUTPUT);
}

MQ-135 Sensor

int airquality = A2;
void setup()
{
  Serial.begin(9600);
}

Combined code

int measurePin = A0; // Connect dust sensor to Arduino A0 pin
int ledPower = D8;   // Connect 3 led driver pins of dust sensor to Arduino D2
int airquality = A2;

void setup(){
  Serial.begin(9600);
  pinMode(ledPower,OUTPUT);
}
2

There is no way of "automatically" combine two sketches into one. That's because resources of Arduino are very limited. For example UNO it has only 4 timers. If both of the sketches want to use same resource they will not work properly.

If you want to combine two sketches into one you have to write a new sketch that combines mechanisms of both sketches. It's not easy, because delay and delayMicroseconds are going to give you problems, but you should be able to do that using micros() and millis() plus some modular math.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.