Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 37: Automatic soap dispenser
The Automatic Soap Dispenser project uses an Arduino Uno board along with an infrared obstacle avoidance sensor and a water pump. The sensor detects the presence of an object such as a hand, which activates the water pump to dispense soap.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
ESP32 & Development Board (ESP32 Board) |
|
- |
|
- |
|
- |
|
Wiring
Code
Code Analysis
The main idea behind this project is to create a hands-free soap dispensing system. The infrared obstacle avoidance sensor detects when an object (like a hand) is close. Upon detecting an object, the sensor sends a signal to the Arduino, which in turn triggers the water pump to dispense soap. The pump stays active for a brief period, dispensing soap, then turns off.
Defining the pins for the sensor and the pump
In this code snippet, we define the Arduino pins that connect to the sensor and pump. We define pin 7 as the sensor pin and we will use the variable
sensorValueto store the data read from this sensor. For the water pump, we use two pins, 9 and 10.// Define the pin numbers for the Infrared obstacle avoidance sensor const int sensorPin = 35; int sensorValue; // Define pin numbers for the water pump const int pump1A = 19; const int pump1B = 21;
Setting up the sensor and pump
In the
setup()function, we define the modes for the pins we’re using. The sensor pin is set toINPUTas it will be used to receive data from the sensor. The pump pins are set toOUTPUTas they will send commands to the pump. We ensure that the pinpump1Bstarts in aLOWstate (off), and we start the serial communication with a baud rate of 9600.void setup() { // Set the sensor pin as input pinMode(sensorPin, INPUT); // Initialize the pump pins as output pinMode(pump1A, OUTPUT); pinMode(pump1B, OUTPUT); // Keep pump1B low digitalWrite(pump1A, LOW); digitalWrite(pump1B, LOW); Serial.begin(9600); }
Continuously checking the sensor and controlling the pump
In the
loop()function, the Arduino constantly reads the value from the sensor usingdigitalRead()and assigns it tosensorValue(). It then prints this value to the serial monitor for debugging purposes. If the sensor detects an object,sensorValue()will be 0. When this happens,pump1Ais set toHIGH, activating the pump, and a delay of 700 milliseconds allows the pump to dispense soap. The pump is then deactivated by settingpump1AtoLOW, and a 1-second delay gives the user time to move their hand away before the cycle repeats.Note
If the sensor is not working properly, adjust the IR transmitter and receiver to make them parallel. Additionally, you can adjust the detection range using the built-in potentiometer.
void loop() { sensorValue = digitalRead(sensorPin); Serial.println(sensorValue); // If an object is detected, turn on the pump for a brief period, then turn it off if (sensorValue == 0) { digitalWrite(pump1A, HIGH); delay(700); digitalWrite(pump1A, LOW); delay(1000); } }