Hello,
I am using some sensors with UNO R2 board. Since it has only 6 ADC ports, I am trying to build multiplexer to attach all sensors at once.
Here is the code:
#include <SD.h> //http://www.ladyada.net/products/microsd/
#include <Wire.h>
#include <SPI.h>
int CS_pin = 10;
int SD_led = 8;
#include "floatToString.h"
#include "RTClib.h"
RTC_DS1307 RTC;
float COValue = 0;
float NO2Value = 0;
float O3Value = 0;
float e_NO2Value = 0;
float e_NOValue = 0;
float e_COValue = 0;
float celsius = 0, kelvin=0;
#include <math.h>
int dustPin=0;
int ledPower=2;
int delayTime=280;
int delayTime2=40;
float offTime=9680;
float ZeroPercentVoltage = 0.8;
float val = 0;
float max_voltage = 3.27;
char date_time[20];
char CO_Vout[20];
char NO2_Vout[20];
char O3_Vout[20];
char cel[20];
char e_NO2_Vout[20];
char e_NO_Vout[20];
char e_CO_Vout[20];
char dust[20];
#include <avr/pgmspace.h>
int _outputserialchar( char c, FILE *t) {
Serial.write( c );
return 1;
}
void setup()
{
pinMode(4, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
fdevopen( &_outputserialchar, 0);
Wire.begin();
Serial.begin(9600);
SPI.begin();
Serial.println("Initializing Card");
pinMode(10, OUTPUT);
RTC.begin();
pinMode(ledPower,OUTPUT);
pinMode(5, OUTPUT);
if (!SD.begin(CS_pin))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");
//Write Log File Header
File logFile = SD.open("ARD1.csv", FILE_WRITE);
if (logFile)
{
logFile.println(", , , , , , , ,"); //Just a leading blank line, incase there was previous data
String header = "Date_Time, Temp, e_NO2_Vout, e_NO_Vout, e_CO_Vout, CO_Vout, NO2_Vout, O3_Vout, Dust_Vout";
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println("Couldn't open log file");
}
}
void loop()
{
delay(3000);
//Read Value of 4051 analog-in 5 by setting the values of s0,s1 and s2
digitalWrite(4, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(3000);
int readInFive = analogRead(0);
kelvin = readInFive * 0.004882812 * 100;
celsius = (kelvin - 273.15) - 5;
floatToString(cel, celsius, 1);
//Read Value of 4051 analog-in 1 by setting the values of s0,s1 and s2
digitalWrite(4, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
delay(2000);
int readInOne = analogRead(0);
val = readInOne;
digitalWrite(ledPower,LOW);
delayMicroseconds(delayTime);
int dust_Vout = analogRead(dustPin);
delayMicroseconds(delayTime2);
digitalWrite(ledPower,HIGH);
delayMicroseconds(offTime);
floatToString(dust, dust_Vout, 1);
//Read Value of 4051 analog-in 2 by setting the values of s0,s1 and s2
digitalWrite(4, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
delay(3000);
int readInTwo= analogRead(0);
e_NO2Value = readInTwo * 0.004882812;
floatToString(e_NO2_Vout, e_NO2Value, 8);
//Read Value of 4051 analog-in 3 by setting the values of s0,s1 and s2
digitalWrite(4, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
delay(3000);
int readInThree= analogRead(0);
e_COValue = readInThree * 0.004882812;
floatToString(e_CO_Vout, e_COValue, 8);
//Read Value of 4051 analog-in 4 by setting the values of s0,s1 and s2
digitalWrite(4, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
delay(3000);
int readInFour= analogRead(0);
e_NOValue = readInFour * 0.004882812;
floatToString(e_NO_Vout, e_NOValue, 8);
NO2Value = analogRead(1) * 0.004882812;
COValue = analogRead(2) * 0.004882812;
O3Value = analogRead(3) * 0.004882812;
floatToString(CO_Vout, COValue, 8);
floatToString(NO2_Vout, NO2Value, 8);
floatToString(O3_Vout, O3Value, 8);
DateTime now = RTC.now();
sprintf_P(date_time,PSTR("%d/%d/%d %d:%d:%d"),now.day(),now.month(),now.year(),now.hour(),now.minute(),now.second());
String dataString = String(date_time) + ", " + String(cel) + ", " + String(e_NO2_Vout) + ", " + String(e_NO_Vout) + ", " + String(e_CO_Vout) + ", " + String(CO_Vout) + ", " + String(NO2_Vout) + ", " + String(O3_Vout) + ", " + String(dust_Vout);
File logFile = SD.open("ARD1.csv", FILE_WRITE);
if (logFile)
{
logFile.println(dataString);
logFile.close();
printf_P(PSTR("%s, %s, %s, %s, %s, %s, %s, %s, %s\r\n"), date_time, cel, e_NO2_Vout, e_NO_Vout, e_CO_Vout, CO_Vout, NO2_Vout, O3_Vout, dust);
pinMode(SD_led, OUTPUT);
digitalWrite(SD_led, HIGH);
}
else
{
Serial.println("Couldn't open log file");
}
delay(15000);
digitalWrite(SD_led, LOW);
delay(1000);
}
For multiplexing (with 4051) I followed a tutorial on this page Andrew Burrell | Home
My circuit is connected exactly as the photo on the website suggests to.
Problems:
- This code doesn't write values to the csv file on SD card except for header. Although when I remove the multiplexing part from the code, it does work perfectly (writes data on SD and serial monitor).
- On serial monitor I see values (but not on SD card). Also, after 5-6 measurement cycles, code stops working.
Kindly help me fix these issues.
Thanks a lot.
Best,
Z