Close

Arduino code

A project log for IAQ WSN

We are building a wireless sensor network that is monitoring indoor air quality

vlapp17vlapp17 01/15/2016 at 09:030 Comments

The code we wrote for the sensor circuit is very straightforward (see Appendix). It basically reads the voltage belonging to each sensor. The voltage of the temperature circuit is then converted to degrees Celsius. For simplicity reasons the voltages of the gas sensors are simply mapped to a scale from 0 to 100. So you can read the gas concentration in percent. The CO sensor requires the application of a one second heating cycle in connection with a circuit voltage cycle. Each heating cycle consists of 4.8V being applied for 14ms followed by a 0V pulse for the remaining 986ms. The circuit voltage cycle starts with 0V being applied for 995ms, followed by 5ms of 5V. This is implemented in the code using the digitalWrite and analogWrite commands. For the analogWrite command, the required PWM frequency for the wanted voltage needs to be determined. The output consists of several numbers, namely the data from each sensor. The data from all sensors per time slot can be broadcasted as one data packet.

int VOCSensor = A0; //TGS2602
int COSensor = A1; //TGS2442
int TempSensor = A2; //TMP36
//int SoundSensor = A3; 
int circ = 5;
int heat = 6;
int circheat = 9;
float VOC, CO, Temp, Sound;

void setup() {
Serial.begin(9600);
pinMode(circ, OUTPUT);
pinMode(heat, OUTPUT);
pinMode(circheat, OUTPUT);
}

void loop() {

GetVOC();
GetCO();
GetTemp();

Serial.println(VOC);
Serial.println(CO);
Serial.println(Temp);
//Serial.println(Sound);
delay(1000);
}

void GetVOC()
{
 digitalWrite(circheat, HIGH);
 float VOC = analogRead(VOCSensor);
 VOC = map(val0, 0, 1023, 0, 100); //in %
}

void GetCO()
{
 digitalWrite(circ, LOW);
 analogWrite(heat, 245); //PWM pin (range 0 to 255), PWM pin value = 255* (AnalogVolts / 5), here: AnalogVolts: 4.5, gives PWM pin value 245
 delay(14);
 analogWrite(heat, 0);
 delay(981);
 digitalWrite(circ, HIGH);
 delay(5); //original code: 3
 float val1 = analogRead(COSensor);
 CO = map(val1, 0 , 1023, 0, 100); //in %
}

void GetTemp()
{
 Temp = (getVoltage(TempSensor)-0.5)*100; //in degrees C
}

float getVoltage(int pin) //This converts the analog reading (range from 0 - 1023) to a voltage (range from 0 to 5V)
{
  return (analogRead(pin)*(5.0/1023.0));
}

float map(float x, float in_min, float in_max, float out_min, float out_max)
{
 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Discussions