Touchless soap dispenser with Arduino

Arduino touchless soap dispenser

These are crazy times we live in and I thought for a while about releasing this project. However, you don’t have that many opportunities to actively do something against the spread of viruses like the Corona virus. Stay at home and wash your hands. Well, I have an idea for hand washing and so I’ll show you how to build a touchless soap dispenser with Arduino.

Components

Circuit

The circuit consists of two partial circuits. The infrared distance sensor is connected to 5V+,GND and the analog input 0. The servo motor is also connected to 5V+ and GND. It is controlled via the digital pin 7.

Arduino soap dispender

Functionality

The sensor can be installed above the soap dispenser looking down or next to the soap dispenser looking up. It reacts within a range of 10 – 30 cm and activates the servo motor via the Arduino. The servo motor pulls on a string that runs over the pump of the soap dispenser and is fixed on the other side. This pushes the pump down and soap is dispensed. Afterwards the servo moves back to its starting position, the pump moves up again.

To ensure the function you have to adjust three values in the code. To do this, build up the circuit and do a dry test without soap dispenser. Load the code on the Arduino board and start the serial monitor of the Arduino software (Tools>Serial Monitor). Set the baud rate (at the bottom of the window) to 115200. Now you should see the values from the sensor. Hold your hand over the sensor and remember the value at which the sensor should trigger. Write the value into the variable irThreshold.

Now try out values for the servo: Where should it start and how far should it turn. You do this with the variables servoPositionPumps and servoPositionNonPumps. If everything fits, install the soap dispenser. Of course there is a little bit of handicraft work involved.

Code

#include <Servo.h>

int servoPin = 7;
int irPin = 0;

Servo myServo;                        // create servo object

int servoPositionPumpen = 30;         // adjust to your servo
int servoPositionNichtPumpen = 120;   // adjust to your servo
int irThreshold = 300;                // when shall the sensor react?

int lastState = -1;                   // -1 = pump not pushed
int state = -1;

void setup() {
  Serial.begin(115200);               // start serial monitor
  myServo.attach(servoPin);           // initialise servo motor

  
  myServo.write(servoPositionNichtPumpen);  // servo to start position
  delay(1000);                              // wait for one second
}


void loop() {
  Serial.println(analogRead(irPin));                // serial output of sensor value

  if (analogRead(irPin)>irThreshold) state = 1;     // trigger event detected
  else state = -1;                                  // no trigger event detected
  
  if ((state==1)&&(lastState!=1)){                  // prevent continious triggering
    myServo.write(servoPositionPumpen);             // pump
    delay(1000);                                    // wait for one second
    myServo.write(servoPositionNichtPumpen);        // release pump
    delay(1000);                                    // wait for one second
  }
  lastState=state;                                  
  delay(20);
}

3 thoughts on “Touchless soap dispenser with Arduino”

Leave a Reply

Your email address will not be published. Required fields are marked *