Sensor with ON/OFF function.

Dear all
I'm just beginner in Arduino platform and trying to control LED by a sensor (digital input). This circuit should do the following functions:

  1. when the sensor is OFF, the LED is OFF.
  2. when the sensor is ON, the LED is ON for 1 sec and then OFF (although the sensor is ON).
  3. the loop should be repeated, when the sensor is OFF and then ON.

I've written the following code, but it dosen't work as above.

int ledPin = 13;
int inputPin = 6;

void setup() {

pinMode(ledPin,OUTPUT);
pinMode(inputPin,INPUT);

}

void loop() {
int pinValue=digitalRead(inputPin);

if(pinValue==HIGH){
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);

} else{
digitalWrite(ledPin,LOW);
}

}

I appreciate anyone who can help me about this.

Hello there!

Can you tell us what sensor you are using?

You should supply information about why it does not do what you want. Your problem is most likely the delay which will cause the arduino to halt for 1 second and you can avoid this by using timing with "millis()".

It may also be wise to use pinMode(inputPin, INPUT_PULLUP); unless you have an external pullup or pull-down resistor.

...R

Thanks for your reply....
The sensor is (Gravity: Conductivity Switch Sensor– SEN0223).
Actually, I don't have the experience to use timing with "millis()".
If it is possible to correct my code.

This is a very poor datasheet for the sensor in question, and equally poor example code. That said, I can see they intent to use the internal pull-up resistor based on the lines:

pinMode(inputPin, INPUT);
digitalWrite(ledPin, HIGH);

This is an antiquated way of turning on a pull-up resistor on the input and has been replaced with:

pinMode(inputPin, INPUT_PULLUP);

as @Robin2 pointed out. From there, it makes no sense to me that a HIGH would be the active state of the sensor, so I will assume that the sensor goes low when it senses conductivity less than 10 MOhms. Based on my assumption, I have fixed the code as follows:

const int LedPin = 13;
const int InputPin = 6;
int lastPinValue;
unsigned long ledOnTimestamp = 0;

void setup() {
  pinMode(LedPin, OUTPUT);
  digitalWrite(LedPin, LOW);
  pinMode(InputPin, INPUT_PULLUP);

}

void loop() {
  int pinValue = digitalRead(InputPin);
  if (pinValue != lastPinValue && pinValue == LOW) {
    digitalWrite(LedPin, HIGH);
    ledOnTimestamp = millis();
  }
  else lastPinValue = pinValue;
  if (millis() - ledOnTimestamp >= 1000UL) {
    digitalWrite(LedPin, LOW);
  }
}

Perehama:
. That said, I can see they intent to use the internal pull-up resistor based on the lines:

pinMode(inputPin, INPUT);

digitalWrite(ledPin, HIGH);

I think not. The lines refer to different pins

...R

Robin2:
I think not. The lines refer to different pins
...R

I see that now... So, if we assume there is a pull-down resistor on-board on the sensor, then the following code should work...

const int InputPin = 6;
int lastPinValue;
unsigned long ledOnTimestamp = 0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  pinMode(InputPin, INPUT);

}

void loop() {
  int pinValue = digitalRead(InputPin);
  if (pinValue != lastPinValue && pinValue == HIGH) {
    digitalWrite(LED_BUILTIN, HIGH);
    ledOnTimestamp = millis();
  }
  else lastPinValue = pinValue;
  if (millis() - ledOnTimestamp >= 1000UL) {
    digitalWrite(LED_BUILTIN, LOW);
  }
}

Perehama:
So, if we assume there is a pull-down resistor on-board on the sensor,

Have you any reason for that assumption?

...R

Robin2:
Have you any reason for that assumption?

...R

only that there is so limited information in the documentation on the spec sheet, but their poor example program operates on a LOW being no conductivity and HIGH being conductive. I personally would look over the sensor and identify the chips and parts and add a pull-down resistor if needed. Basically, I only added to OP code the lines to insure off before on and 1 second on the switch before turning off without delay. it wasn't requested, but I'd also add a bit of debounce timing in the input. The way the code is now, if contact is made, then unmade, then made etc. within the window of 1 second, the LED on interval will restart and only turn off 1 second after the last unmade->made transition.

Thanks all for your comments.
Dear Perehama, the sensor has a resistor. please see the following link for more information about it:

I've tested your code, and the LED is OFF after 1 sec, when the sensor is OFF. Actually, I want to turn off the LED after 1 sec, when the sensor is ON, so how can I do that?

const int LedPin = 13;
const int InputPin = 6;
int lastPinValue;
unsigned long ledOnTimestamp = 0;

void setup() {
pinMode(LedPin, OUTPUT);
digitalWrite(LedPin, LOW);
pinMode(InputPin, INPUT);
Serial.begin(9600);

}

void loop() {
int LED=digitalRead(LedPin);
int pinValue = digitalRead(InputPin);
if (pinValue != lastPinValue && pinValue == HIGH) {
digitalWrite(LedPin, HIGH);
ledOnTimestamp = millis();
}
else lastPinValue = pinValue;
if (millis() - ledOnTimestamp >= 1000UL) {
digitalWrite(LedPin, LOW);
}

// print the results to the serial monitor:
Serial.print("LED Value = ");
Serial.print(LED);
Serial.println(" Signal");
delay(500);
}

AHM83:
Dear Perehama, the sensor has a resistor. please see the following link for more information about it:
Gravity: Conductivity Sensor Switch - DFRobot

There is no where on that page, or in the links for additional information that gives a schematic for the sensor circuit or indicates that it is pulled high or low in a non-active state. It's less than adequate documentation.

AHM83:
I've tested your code, and the LED is OFF after 1 sec, when the sensor is OFF. Actually, I want to turn off the LED after 1 sec, when the sensor is ON, so how can I do that?

You need to change one line:

if (pinValue != lastPinValue && pinValue == HIGH) {

to

if (pinValue != lastPinValue && pinValue == LOW) {