Recently, I was given a project where I had to read multiple sensors with a UART interface. Many in the Arduino user community stated that this couldn't be done on a UNO board. I was able to make it work for four sensors. I have attached the code here. Hopefully, this is valuable to other users.
#include <SoftwareSerial.h>
SoftwareSerial mySerial_1(8, 9);
SoftwareSerial mySerial_2(10, 11);
SoftwareSerial mySerial_3(12, 13);
SoftwareSerial mySerial_4(A0,A1);
#include <LiquidCrystal.h>
LiquidCrystal lcd (2, 3, 4, 5, 6, 7);
unsigned char hexdata[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
int SensorSelect = 1; // Start reading from the 1st CO2 sensor
int TD = 1000; // time delay
void setup() {
Serial.begin(9600); //Serial communication with computer
lcd.begin (16, 2); //Set the size of the LCD display
while (!Serial)
{
}
lcd.setCursor(0, 0); // Thanking NASA for the opportunity
lcd.print(" THANK YOU NASA ");
lcd.setCursor(0, 1);
lcd.print("SPACE CO2 DETECT");
delay(1000);
}
void loop()
{
switch (SensorSelect)
{
case 1:
Serial.println("Attempting to read sensor 1...");
mySerial_1.begin(9600);
delay(TD);
mySerial_1.write(hexdata, 9);
delay(TD);
for (int i1 = 0; i1 < 9; i1++) //CO2 sensor 1
{
if (mySerial_1.available() > 0)
{
Serial.println("Reading sensor 1...");
long hi_1, lo_1, CO2_1;
int ch_1 = mySerial_1.read();
if (i1 == 2)
{
hi_1 = ch_1; //high concentration of CO2
}
if (i1 == 3)
{
lo_1 = ch_1; //low concentration of CO2
}
if (i1 == 8)
{
CO2_1 = hi_1 * 256 + lo_1; //CO2 concentration equation
Serial.println("Sensor 1 Reading");
Serial.print("CO2 concentration: ");
Serial.print(CO2_1);// display concentration on computer
Serial.println(" ppm");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor 1 Reading");
lcd.setCursor(0, 1);
lcd.print("CO2: "); //display concentraition on LCD
lcd.print(CO2_1);
lcd.print(" ppm");
}
}
}
SensorSelect = 2; // Read the next sensor
break;
case 2:
mySerial_2.begin(9600);
delay(TD);
Serial.println("Attempting to read sensor 2...");
mySerial_2.write(hexdata, 9);
delay(TD);
for (int i2 = 0; i2 < 9; i2++) //CO2 sensor 2
{
if (mySerial_2.available() > 0)
{
Serial.println("Reading sensor 2...");
long hi_2, lo_2, CO2_2;
int ch_2 = mySerial_2.read();
if (i2 == 2)
{
hi_2 = ch_2; //high concentration of CO2
}
if (i2 == 3)
{
lo_2 = ch_2; //low concentration of CO2
}
if (i2 == 8)
{
CO2_2 = hi_2 * 256 + lo_2; //CO2 concentration equation
Serial.println("Sensor 2 Reading");
Serial.print("CO2 concentration: ");
Serial.print(CO2_2);// display concentration on computer
Serial.println(" ppm");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor 2 Reading");
lcd.setCursor(0, 1);
lcd.print("CO2: ");
lcd.print(CO2_2);
lcd.print(" ppm");
}
}
}
SensorSelect = 3; // Read the next sensor
break;
case 3:
mySerial_3.begin(9600);
delay(TD);
Serial.println("Attempting to read sensor 3...");
mySerial_3.write(hexdata, 9);
delay(TD);
for (int i3 = 0; i3 < 9; i3++) //CO2 sensor 3
{
if (mySerial_3.available() > 0)
{
Serial.println("Reading sensor 3...");
long hi_3, lo_3, CO2_3;
int ch_3 = mySerial_3.read();
if (i3 == 2)
{
hi_3 = ch_3; //high concentration of CO2
}
if (i3 == 3)
{
lo_3 = ch_3; //low concentration of CO2
}
if (i3 == 8)
{
CO2_3 = hi_3 * 256 + lo_3; //CO2 concentration equation
Serial.println("Sensor 3 Reading");
Serial.print( "CO2 concentration: ");
Serial.print(CO2_3);// display concentration on computer
Serial.println(" ppm");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor 3 Reading");
lcd.setCursor(0, 1);
lcd.print("CO2: ");
lcd.print(CO2_3);
lcd.print(" ppm");
}
}
}
SensorSelect = 4; // Read the next sensor
break;
case 4:
mySerial_4.begin(9600);
delay(TD);
Serial.println("Attempting to read sensor 4...");
mySerial_4.write(hexdata, 9);
delay(TD);
for (int i4 = 0; i4 < 9; i4++) //CO2 sensor 4
{
if (mySerial_4.available() > 0)
{
Serial.println("Reading sensor 4...");
long hi_4, lo_4, CO2_4;
int ch_4 = mySerial_4.read();
if (i4 == 2)
{
hi_4 = ch_4; //high concentration of CO2
}
if (i4 == 3)
{
lo_4 = ch_4; //low concentration of CO2
}
if (i4 == 8)
{
CO2_4 = hi_4 * 256 + lo_4; //CO2 concentration equation
Serial.println("Sensor 4 Reading");
Serial.print( "CO2 concentration: ");
Serial.print(CO2_4);// display concentration on computer
Serial.println(" ppm");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor 4 Reading");
lcd.setCursor(0, 1);
lcd.print("CO2: ");
lcd.print(CO2_4);
lcd.print(" ppm");
}
}
}
SensorSelect = 1; // Read the next sensor
break;
}
delay(500);
}