How to seperate Data

Hello. I am doing a science project that involves Arduino. My team and I are almost finished with the project. But with just one problem, we don't know how to use the Arduino to detect Chlorine, which is the main part of our project. I am using the Capacitive Sensor program as here in Malaysia we cant afford to buy a colorinmeter. Could someone please tell me how to use the capacitive sensor to detect chlorine?

I am not quite familiar with the coding. Your answers are mostly appreciated. Thank you.

Hello. I am doing a science project that involves Arduino. My team and I are almost finished with the project. But with just one problem, we don't know how to use the Arduino to detect Chlorine, which is the main part of our project. I am using the Capacitive Sensor program as here in Malaysia we cant afford to buy a colorinmeter. Could someone please tell me how to use the capacitive sensor to detect chlorine?

I am not quite familiar with the coding. Your answers are mostly appreciated. Thank you.

Here's the coding:

#include <CapacitiveSensor.h>

CapacitiveSensor capSensor_4_10 = CapacitiveSensor(4,10);
CapacitiveSensor capSensor_4_12 = CapacitiveSensor(4,12);

const int ledPin = 3;
const int outPin = 2;
int analogVal ;
float voltage;
float mass;
float ppm;
float chlorine;

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

void loop()
{
long value1=capSensor_4_10.capacitiveSensor(30);
long value2=capSensor_4_12.capacitiveSensor(30);
if (value2>200) digitalWrite (outPin,HIGH); else digitalWrite (outPin,LOW);

analogVal = analogRead(value1);
mass = analogRead(chlorine);
ppm = mass / 1.5 * 1,000,000;
Serial.println(ppm);

if (ppm > 50)
{
digitalWrite(ledPin,HIGH);
delay(100);
digitalWrite(ledPin,LOW);
delay(100);}
else
{digitalWrite(outPin,LOW);}

delay(1000);
}

What sensor is this?

Can you post a link to it?

Also a link to that library?

(Even for a widely distributed library, which I'm not sure that is, it's always critical to link to the actual version of the library you're using. Partly because of how easy it is to fork things on github, there is a proliferation of identically named but subtly/not-so-subtly different libraries. Often in github forks, people don't even modify the readme or other documentation to describe what they've changed)

I can’t tell you how to use a capacitive sensor to detect chlorine, but I can say at least this is wrong:

   analogVal = analogRead(value1);
   mass = analogRead(chlorine);

because analogRead() takes a pin number as parameter. The variable “chlorine” is an undefined float.

6v6gt:
The variable “chlorine” is an undefined float.

No, it is a defined float.
Because it is global, it has been initialised to 0.0, so the read will be from A0.

Much more problematic is analogVal = analogRead(value1);

I believe he uses this library, which enables to make capacitive touch dectectors as shown below:

The following lines are ok, as they are adpated from the library's example:

   long value1=capSensor_4_10.capacitiveSensor(30);
   long value2=capSensor_4_12.capacitiveSensor(30);

But the rest, as you said before, is weird:

   analogVal = analogRead(value1);
   mass = analogRead(chlorine);

I don't know if such a sensor can detect the presence of chlorine or any other gas. Googling "Arduino gas sensor" provides with some cheap gas sensors that "could" be used, but I'm far from being an expert, so are they suited to chlorine?

If it's not gaseous chlorine, the subject has already been treated on the forum: use the search engine of the forum or see here for an example

So the coding is still weird. It often goes up and goes down. I modified the coding under you guys' opinions.
But it still doesn't display what i wanted to display. Could you recommend me some simple yet functional coding that can help the capacitive sensor to detect chlorine? The library i use is the standard Arduino library. Unfortunately i cant link the library folder for some reason. Could you guys recommend some simple yet functional coding instead? Thanks for your answers.

#include <CapacitiveSensor.h>

CapacitiveSensor capSensor_4_10 = CapacitiveSensor(4, 10);

const int ledPin = 3;
int analogVal ;
float mass;
float ppm;

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

void loop()
{
long value1 = capSensor_4_10.capacitiveSensor(30);
mass = analogRead(value1);
ppm = mass / 1500 * 1, 000, 000;
Serial.println(ppm);

if (ppm > 170)
{
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
else
{
digitalWrite(ledPin, LOW);
}
delay(1000);
}

Also here's the screenshot of the receiving data.

https://1drv.ms/u/s!AjAildox26fUhBCELXFotqAPCYSZ

Threads merged.

sp. "separate"

Please remember to use code tags when posting code

long value1 = capSensor_4_10.capacitiveSensor(30); 
   mass = analogRead(value1);

Why use a 32 bit (signed!) variable to specify a pin number?

In these lines:

  long value1 = capSensor_4_10.capacitiveSensor(30); 
   mass = analogRead(value1);

You define value1 as the result of the reading of the capacitive sensor. It's not the pin number, it's the value read.
So the second line is not good.

You don't need an analogRead with this library. It says:

long capacitiveSensorRaw(byte samples)
capacitiveSensorRaw requires one parameter, samples, and returns a long integer containing the absolute capacitance, in arbitrary units. The samples parameter can be used to increase the returned resolution, at the expense of slower performance. The returned value is not averaged over the number of samples, and the total value is reported.

But as it is a long int, it cannot represent formally a capacitance (you need a float for that). They say it's arbitrary units.

Anyways, value1 can't be used in the mass calculation as this:

   mass = analogRead(value1);
   ppm = mass / 1500 * 1, 000, 000;

(BTW, use dots '.' for floats, not commas ',')

What you need to do is calibrate your sensor. Use several test samples with known chlorine concentrations and measure the associated value1 values. Then draw a curve showing the variations of value1 against the concentration, which you will then use for routine measurements afterwards.

So how about this coding?

#include <CapacitiveSensor.h>

CapacitiveSensor capSensor_4_10 = CapacitiveSensor(4,10);

const int ledPin = 3;
int inPin;
int inputPin = 10;
int analogVal;
float mass;
float chlorine;
float ppm;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(inputPin,INPUT);
Serial.begin(9600);
}

void loop()
{
long value1 = capSensor_4_10.capacitiveSensor(30);
inPin = analogRead(value1);
inputPin = analogRead(chlorine);
analogVal = analogRead(inputPin);
mass = analogRead(analogVal);
ppm = mass / 1.5 * 1, 000, 000;
Serial.println(ppm);

if (ppm > 170)
{
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
else
{
digitalWrite(ledPin, LOW);
}
delay(1000);
}

long value1 = capSensor_4_10.capacitiveSensor(30); 
  inPin = analogRead(value1);

Again?

Please remember to use code tags when . . . Oh! What's the point?

Then how about this? (I'm getting mental because of this lol)

#include <CapacitiveSensor.h>

CapacitiveSensor capSensor_4_10 = CapacitiveSensor(4,10);

const int ledPin = 3;
int inputPin = 10;
int analogVal;
float mass;
float chlorine;
float ppm;

void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(inputPin,INPUT);
Serial.begin(9600);
}

void loop()
{
long value1 = capSensor_4_10.capacitiveSensor(30);
inputPin = analogRead(chlorine);
analogVal = analogRead(inputPin);
mass = analogRead(analogVal);
ppm = mass / 1.5 * 1, 000, 000;
Serial.println(ppm);

if (ppm > 170)
{
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
}
else
{
digitalWrite(ledPin, LOW);
}
delay(1000);
}

inputPin = analogRead(chlorine);
  analogVal = analogRead(inputPin);

Does that look any more sensible?

Code tags.

Please stop and think, and read what you have been told.

I actually don't know

Just do this

void loop()
{
  long value1 = capSensor_4_10.capacitiveSensor(30); 
  Serial.println((int)value1);
  delay(300);
}

And see what the serial monitor displays.
You can also use the serial graph plotter

which will provide this kind of graph

Then try your new sensor with several test samples and see if the readings vary and how. If you see representative variations, then you can conclude your sensor is relevant. The next step will be to calibrate it, meaning find the relation between the ppm values and the readings of the sensor. But this comes after you make sure that the readings are associated with the chlorine concentration.

So does this work? I feel like it does as the readings are stable and not up and down.

#include <CapacitiveSensor.h>

CapacitiveSensor capSensor_4_10 = CapacitiveSensor(4,10);

const int ledPin = 3;
int inputPin = 10;
int analogVal;
float mass;
float chlorine;
float ppm;

void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(inputPin,INPUT);
Serial.begin(9600);
}

void loop()
{
long value1 = capSensor_4_10.capacitiveSensor(30);
inputPin = digitalRead(chlorine);
analogVal = digitalRead(inputPin);
mass = digitalRead(analogVal);
ppm = mass / 1.5;
Serial.println((int)value1);
delay(300);

if (ppm > 170)
{
digitalWrite(ledPin,HIGH);
delay(500);
digitalWrite(ledPin,LOW);
delay(500);
}
else
{
digitalWrite(ledPin,LOW);
}
delay(1000);
}

So hows this one? I think its good cos the data given is stable.

#include <CapacitiveSensor.h>

CapacitiveSensor capSensor_4_10 = CapacitiveSensor(4,10);

const int ledPin = 3;
int inputPin = 10;
int analogVal;
float mass;
float chlorine;
float ppm;

void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(inputPin,INPUT);
Serial.begin(9600);
}

void loop()
{
long value1 = capSensor_4_10.capacitiveSensor(30);
inputPin = digitalRead(chlorine);
analogVal = digitalRead(inputPin);
mass = digitalRead(analogVal);
ppm = mass / 1.5;
Serial.println((int)value1);
delay(300);

if (ppm > 170)
{
digitalWrite(ledPin,HIGH);
delay(500);
digitalWrite(ledPin,LOW);
delay(500);
}
else
{
digitalWrite(ledPin,LOW);
}
delay(1000);
}

inputPin = digitalRead(chlorine);

Can we please just stop posting alphabetti spaghetti coding?
chlorine, a float, is never initialised, so is zero.
Is it sensible to read from pin zero?

Please read the answers you get and try what we suggest, instead of ignoring it.

Until you haven't proven that your sensor is relevant for measuring chlorine concentration, no need to go further in the code. And to be sure it works, you need to test several different solutions or gases with different concentration values. Only if in this case you get different readings can you be sure the sensor is relevant.

I won't answer anymore as long as this is not proven.

I (and I'm sorry if this sounds cruel) am chuckling at the OP saying their chlorine detection project is almost finished, just need to do the chlorine detection part.

JasonCMJ:
My team and I are almost finished with the project. But with just one problem, we don't know how to use the Arduino to detect Chlorine, which is the main part of our project.

I hope they are aware of the dangers of chlorine gas; my son who is a ff/emt just pointed out "Wouldn't want any untested, non Intrinsically safe electronics near a cloud".