Sanitizer dispenser code help

hi all ,

i am trying to make ultrasonic sensor based hand sanitizer using arduino with relay.very close to it but facing some problem with the timer.hope u guys can help me correct the code

Materials i am using :
-Arduino nano
-5v relay (input pin connected to pin8)
-PC USB connection for powering the ardunio
-ultrasonic sensor
-3-6v dc pump
My code :

int pump = 8;
const int trigPin = 3;
const int echoPin = 2;

float duration, distance;


void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(pump, OUTPUT);
  digitalWrite(pump, HIGH);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);

}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration * .0343) / 2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
  if (distance < 30) { //use can increase the distance
    digitalWrite(pump, HIGH);
    delay(50);//You can increase the water timing
    
  
  }
  else {
    digitalWrite(pump, LOW);
  delay(100);
  }
}

what the above code does : it turns on the relay and then turns it off , but when i bring hand second time , it stays on :confused:

what i want to do : i want the relay to turn on for 1sec then off and then dont turn on until i bring my hand the second time

thanks a lot in advance :slight_smile:

P.S. - a similar thread on this forum with the same issue i am facing , but he is using IR sensor .

i am trying to make ultrasonic hand sanitizer

Ultraviolet may be a useful sanitiser, ultrasound less so.

TheMemberFormerlyKnownAsAWOL:
Ultraviolet may be a useful sanitiser, ultrasound less so.

I am trying to use it with pump

Hi,
your if and else do the same. One of the cases must be digitalWrite(pump, HIGH);
based on the configuration of your relay.

At the moment, both are set to digitalWrite(pump, LOW);

critycal_:
Hi,
your if and else do the same. One of the cases must be digitalWrite(pump, HIGH);
based on the configuration of your relay.

At the moment, both are set to digitalWrite(pump, LOW);

hi i have correct that , please help me with rest of function

Hi,
there is a very similar thread in this topic, where I already wrote a code for that.
You could use this and modify it to your needs. Look at this thread:

Auto Hand Sanitizer Dispenser - Code Help

critycal_:
Hi,
there is a very similar thread in this topic, where I already wrote a code for that.
You could use this and modify it to your needs. Look at this thread:

Auto Hand Sanitizer Dispenser - Code Help

hi.thanks for reply , i have seen the thread already .being complete noob i tried to modify using following code but after uploading the code relay is constantly on.its not getting trigger.

here is updated/modified code (not working) :

const int trigPin = 3;
const int echoPin = 2;

#define DCwater_pump 8
    
// defines pins
    long duration;
    int distance;
int pumpState = 0;
int lastPumpState = 0;

    void setup() 
{
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input
    pinMode(DCwater_pump, OUTPUT);
    Serial.begin(9600); // For serial communication
}
    void loop() 
{
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    //Distance calculation
    distance= duration*0.034/2;
    // Printinng the distance on the Serial Monitor
    Serial.print("Distance: ");
    Serial.println(distance);
    
 if (distance < 30)
{
 pumpState = digitalRead(echoPin);
  if (pumpState != lastPumpState) {
    if (pumpState == HIGH) {
   digitalWrite(DCwater_pump,HIGH);
 Serial.println("DC Pump is ON Now!!");
 delay(500);
digitalWrite(DCwater_pump, LOW);
      Serial.println("DC Pump is OFF Now!!");
}
delay(50);
  
{
 lastPumpState = pumpState;

}
}}}

Hi,
because you coded the pumpState to the echoPin, which is always set as high.
I will try to guide you, so you can code it yourself:

The trigger from the other code was an IR sensor, which has only two states:
High and low. So whenever this state was changed, the pump was allowed to run, once the trigger was high again.

Your sensor returns values, which are being processed into a distance, so a whole
lot of numbers. Not only high and low, which is equal to 1 and 0.

You need to think about how to implement this state change with your sensor in mind.
There are a lot of options how to do that.

Friend i tried to put

pumpState = digitalRead(Distance)

Instead of

pumpState = digitalRead(echoPin)

But it didn't work

i guess i need to make changes in the following part :

 if (distance < 30)
{
 pumpState = digitalRead(echoPin);
  if (pumpState != lastPumpState) {
    if (pumpState == HIGH) {
   digitalWrite(DCwater_pump,HIGH);
 Serial.println("DC Pump is ON Now!!");
 delay(500);
digitalWrite(DCwater_pump, LOW);
      Serial.println("DC Pump is OFF Now!!");
}
delay(50);
{
 lastPumpState = pumpState;

}

critycal_:
Hi,
because you coded the pumpState to the echoPin, which is always set as high.
I will try to guide you, so you can code it yourself:

The trigger from the other code was an IR sensor, which has only two states:
High and low. So whenever this state was changed, the pump was allowed to run, once the trigger was high again.

Your sensor returns values, which are being processed into a distance, so a whole
lot of numbers. Not only high and low, which is equal to 1 and 0.

You need to think about how to implement this state change with your sensor in mind.
There are a lot of options how to do that.

or should i use distance instead of echoPin

Dos this problem solved

if problem solved please help us

Hi,

Please find the code which I used with same setup to sanitizer dispenser.

/*
 Vinod Amarathunga
 WaterPump Ultrasonic Distance Based V 2.0
 vinod.amarathunga@dmselectronics.com

*/

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration;
int distance;

//define priState

bool priState;

void setup() {
 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
 pinMode(echoPin, INPUT); // Sets the echoPin as an Input
 Serial.begin(9600); // Starts the serial communication
 pinMode(6, OUTPUT); //Sets the Relay Digital Input
 priState = false;


}

void loop() {
 // Clears the trigPin
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);

 // Sets the trigPin on HIGH state for 10 micro seconds
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

 // Reads the echoPin, returns the sound wave travel time in microseconds
 duration = pulseIn(echoPin, HIGH);

 // Calculating the distance
 distance = duration * 0.034 / 2;

 /*
   // Prints the distance on the Serial Monitor
   Serial.print("Distance: ");
   Serial.println(distance);
 */

 if (distance < 10 ) {

   if (priState == false) {
     Serial.println("Pump On");
     digitalWrite (6, HIGH);
     priState = true;
     delay (500);
     digitalWrite (6, LOW);
   }

   delay (5000);
 }





 else {
   digitalWrite (5, LOW);
   priState = false;


 }
}

Video How it works: Quick Share
Valid until: Jun 19, 2020

Edit delay to dispense more

priState = true;
     delay (500);

Items used: HC-SR04/ Arduino Nano/5V DC Relay/Small 7V DC Aquarium Peristaltic pump

   digitalWrite (5, LOW);

What is attached to pin 5?

Sorry, It should be

else {
    digitalWrite (6, LOW);
    priState = false;

vinodamarathunga:
Sorry, It should be

else {
digitalWrite (6, LOW);
priState = false;

No, it should be digitalWrite (pumpPin, LOW);

D PIN 6 is the Relay Control Input na d I have not defined any pumpIN variable in my code.

vinodamarathunga:
D PIN 6 is the Relay Control Input na d I have not defined any pumpIN variable in my code.

You should

Reason ?

vinodamarathunga:
Reason ?

It's a great habit to learn.
And it makes boo-boos likedigitalWrite (5, LOW); even easier to spot, fix or modify.