Im using multiplexer for my project which is water monitoring system, i have 3 parameters temperature, ph level and turbidity. ph level and turbidity need a analog sensor but esp8266 only have one analog pin so i use muliplexer, the problem when i test the turbidity sensor, ph level output also change even i didnt put it to a water.
this is my code
#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */
#define S0 D0 /* Assign Multiplexer pin S0 connect to pin D0 of NodeMCU */
#define S1 D1 /* Assign Multiplexer pin S1 connect to pin D1 of NodeMCU */
#define S2 D2 /* Assign Multiplexer pin S2 connect to pin D2 of NodeMCU */
#define S3 D3 /* Assign Multiplexer pin S3 connect to pin D3 of NodeMCU */
#define SIG A0 /* Assign SIG pin as Analog output for all 16 channels of Multiplexer to pin A0 of NodeMCU */
/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "TMPL4Cvekawb"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
char auth[] = "V9bTsvdXRzmSSQ4BfYVhC_nEN3N4ZMAA";
char ssid[] = "Pro";
char pass[] = "123456789";
BlynkTimer timer;
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
const int sensorPin = A0;
float temp;
float calibration_value = 21.34;
int phval = 0;
unsigned long int avgval;
int buffer_arr[10],tem;
int decimal = 1; // Decimal places of the sensor value outputs
int sensorVal; /* Assign the name "sensor0" as analog output value from Channel C0 */
int sensor1; /* Assign the name "sensor1" as analog output value from Channel C1 */
void setup()
{
// Debug console
pinMode(S0,OUTPUT); /* Define digital signal pin as output to the Multiplexer pin SO */
pinMode(S1,OUTPUT); /* Define digital signal pin as output to the Multiplexer pin S1 */
pinMode(S2,OUTPUT); /* Define digital signal pin as output to the Multiplexer pin S2 */
pinMode(S3,OUTPUT); /* Define digital signal pin as output to the Multiplexer pin S3 */
pinMode(SIG, INPUT); /* Define analog signal pin as input or receiver from the Multiplexer pin SIG */
Serial.begin(115200);
DS18B20.begin();
Blynk.begin(auth, ssid, pass, "blynk.cloud");
timer.setInterval(5000L, getSendData);
}
void loop()
{
Blynk.run();
timer.run();
}
void getSendData()
{
//float sensorValue = analogRead(sensorPin);
//float volt = sensorValue*(5.0/1024.0);
DS18B20.requestTemperatures();
temp = DS18B20.getTempCByIndex(0); // Celcius
digitalWrite(S0,LOW); digitalWrite(S1,LOW); digitalWrite(S2,LOW); digitalWrite(S3,LOW);
float sensorVal = analogRead(SIG);
float volt = sensorVal*(5.0/1024.0);
// Channel 1 (C1 pin - binary output 1,0,0,0)
digitalWrite(S0,HIGH); digitalWrite(S1,LOW); digitalWrite(S2,LOW); digitalWrite(S3,LOW);
sensor1 = analogRead(SIG);
for(int i=0;i<10;i++)
{
buffer_arr[i]=analogRead(SIG);
delay(30);
}
for(int i=0;i<9;i++)
{
for(int j=i+1;j<10;j++)
{
if(buffer_arr[i]>buffer_arr[j])
{
tem=buffer_arr[i];
buffer_arr[i]=buffer_arr[j];
buffer_arr[j]=tem;
}
}
}
avgval=0;
for(int i=2;i<8;i++)
avgval+=buffer_arr[i];
float ph =(float)avgval*5.0/1024/6;
float ph_act = -5.70 * ph + calibration_value;
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" \xC2\xB0"); // shows degree symbol
Serial.print("C | ");
Serial.print(sensorVal);
int turbidity = map(sensorVal, 0, 750, 100, 0);
delay(100);
if (turbidity < 20) {
Serial.print(" CLEAR ");
}
if ((turbidity > 20) && (turbidity < 50)) {
Serial.print(" CLOUDY ");
}
if (turbidity > 50) {
Serial.print(" its DIRTY ");
}
delay(500);
Serial.print(" | ");
Serial.print("PH LEVEL: ");
Serial.print(ph_act);
Serial.println("");
delay(1000);
Blynk.virtualWrite(V3, temp); //virtual pin V3 for Temperature Sesor
Blynk.virtualWrite(0, volt); //virtual pin V0 for Turbidity Sensor
Blynk.virtualWrite(1, ph_act); //virtual pin V0 for Turbidity Sensor
}