Hello, fellow tech enthusiasts! Today, I'm going to guide you through the process of building a touchless hand sanitizer dispenser using the Arduino Uno R3 microcontroller. In the wake of the COVID-19 pandemic, hand hygiene has become crucial, and touchless solutions are in high demand. With this project, we aim to create a simple and efficient way to sanitize hands without the need for physical contact.
In this tutorial, we'll cover the necessary components, the setup process, and the complete code required to bring this touchless hand sanitizer dispenser to life. So, let's dive in and get started!
Part 1: Components Required
Before we begin, let's take a look at the components we'll need for this project:
1. Arduino Uno R3: The Arduino Uno R3 is a popular microcontroller board that serves as the brains of our dispenser. It's equipped with an ATmega328P microcontroller and provides various digital and analog input/output pins.
2. Ultrasonic Sensor (HC-SR04): The HC-SR04 is an affordable and reliable ultrasonic sensor that measures distances using sound waves. We'll use it to detect when a user's hand is in front of the dispenser.
3. Servo Motor: A servo motor allows us to control the motion of the dispenser mechanism. It'll be responsible for triggering the release of the hand sanitizer.
4. Liquid Pump: To dispense the hand sanitizer, we'll need a small liquid pump. Make sure to choose a pump suitable for your sanitizer container and compatible with the Arduino's voltage levels.
5. Power Supply: You'll need a suitable power supply to run the Arduino board, motor, and sensor. A 9V battery or a USB power source should suffice.
6. Hand Sanitizer Container: Find a suitable container to hold your hand sanitizer. Ensure it's easy to refill and has a suitable opening for the liquid pump.
7. Jumper Wires: To connect the various components together, you'll need male-to-male jumper wires.
With these components in hand, we can move on to the setup process.
Part 2: Setting up the Hardware
Before we delve into the coding aspect, let's assemble the hardware components to build our touchless hand sanitizer dispenser.
1. Mounting the Ultrasonic Sensor:
Start by mounting the HC-SR04 ultrasonic sensor on the top of your dispenser. Position it facing downwards, so it detects the presence of hands below. Secure it using adhesive or by creating a custom mounting bracket.
2. Connecting the Ultrasonic Sensor:
Connect the VCC pin of the sensor to the 5V pin on the Arduino board. Next, connect the GND pin to any ground pin on the Arduino. Finally, connect the Trigger and Echo pins of the sensor to any digital pins on the Arduino, making sure to note down the pin numbers for later use.
3. Connecting the Servo Motor:
Connect the signal wire of the servo motor to any digital pin on the Arduino. Additionally, connect the power and ground wires of the servo motor to the 5V and GND pins on the Arduino, respectively.
4. Connecting the Liquid Pump:
The connections for the liquid pump may vary depending on the specific pump you're using. Typically, you'll need to connect the positive and negative terminals of the pump to a suitable power source. Make sure to connect the ground of the power source to the Arduino's GND pin to establish a common ground.
With the hardware setup complete, we can move on to the coding part in the next section.
Part 3: Writing the Arduino Code
Now that we have our hardware set up, it's time to write the code that will control our touchless hand sanitizer dispenser. We'll be using the Arduino programming language, which is based on C/C++.
1. Setting up the Libraries:
Before we begin writing the code, we need to install a couple of libraries that will help us interface with the ultrasonic sensor and servo motor. Open the Arduino IDE (Integrated Development Environment), go to "Sketch" -> "Include Library" -> "Manage Libraries," and search for the following libraries:
- NewPing: This library provides easy-to-use functions for the ultrasonic sensor.
- Servo: The Servo library allows us to control the servo motor's movement.
Install these libraries, and once completed, we can proceed to write our code.
2. Initializing the Libraries and Variables:
Let's start by including the required libraries and declaring the necessary variables. Copy and paste the following code at the beginning of your Arduino sketch:
#include <NewPing.h>
#include <Servo.h>
#define TRIGGER_PIN 2
#define ECHO_PIN 3
#define SERVO_PIN 4
#define PUMP_PIN 5
#define MAX_DISTANCE 20 // Maximum distance in centimeters to trigger the sanitizer
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Servo servo;
int pumpDelay = 1000; // Delay for the sanitizer pump activation (in milliseconds)
In this code snippet, we include the necessary libraries, define the pins for the ultrasonic sensor (TRIGGER_PIN and ECHO_PIN), servo motor (SERVO_PIN), and liquid pump (PUMP_PIN). We also set the maximum distance (MAX_DISTANCE) at which the sensor will trigger the sanitizer. The `pumpDelay` variable determines the duration of the sanitizer pump activation.
3. Setting Up the Arduino Setup() Function:
The `setup()` function is called once when the Arduino board powers up. It initializes the necessary settings and pin modes. Insert the following code inside the `setup()` function:
void setup() {
Serial.begin(9600); // Initialize serial communication (for debugging)
servo.attach(SERVO_PIN); // Attach the servo to the specified pin
pinMode(PUMP_PIN, OUTPUT); // Set the pump pin as an output
digitalWrite(PUMP_PIN, LOW); // Initially turn off the pump
}
In this snippet, we set up the serial communication for debugging purposes. We attach the servo to the specified pin using the `attach()` function and set the liquid pump pin as an output using `pinMode()`. We also ensure that the pump is initially turned off by setting the pin's state to LOW using `digitalWrite()`.
4. Implementing the Arduino Loop() Function:
The `loop()` function runs continuously after the `setup()` function completes. We'll include our main code logic here. Replace the existing `loop()` function with the following code:
void loop() {
int distance = sonar.ping_cm(); // Measure the distance in centimeters
if (distance > 0 && distance <= MAX_DISTANCE) {
activateSanitizer(); // Call the function to activate the sanitizer
}
delay(50); // Small delay between sensor readings for stability
}
In this code block, we use the `sonar.ping_cm()` function from the NewPing library to measure the distance detected by the ultrasonic sensor. If the measured distance is within the acceptable range (0 to MAX_DISTANCE), we call the `activateSanitizer()` function to trigger the sanitizer mechanism. We also include a small delay of 50 milliseconds to stabilize the sensor readings
Part 4: Completing the Arduino Code
5. Implementing the activateSanitizer() Function:
Now, let's write the `activateSanitizer()` function that controls the servo motor and the liquid pump. Add the following code outside of the `setup()` and `loop()` functions:
void activateSanitizer() {
Serial.println("Hand detected! Activating sanitizer...");
servo.write(90); // Rotate the servo to the desired angle (adjust as needed)
delay(pumpDelay); // Wait for the specified pump activation delay
digitalWrite(PUMP_PIN, HIGH); // Turn on the sanitizer pump
delay(pumpDelay); // Keep the pump active for the specified duration
digitalWrite(PUMP_PIN, LOW); // Turn off the sanitizer pump
servo.write(0); // Return the servo to its initial position
Serial.println("Sanitizer dispensed successfully!");
}
In this function, we first print a message to the serial monitor indicating that a hand has been detected. We then use `servo.write()` to rotate the servo motor to a specific angle (e.g., 90 degrees) to trigger the sanitizer release mechanism. After the specified `pumpDelay`, we turn on the liquid pump by setting the `PUMP_PIN` to HIGH. We keep the pump active for the duration of `pumpDelay` and then turn it off by setting the `PUMP_PIN` to LOW. Finally, we return the servo to its initial position (e.g., 0 degrees) using `servo.write()`. We print another message to the serial monitor to indicate the successful dispensing of sanitizer.
6. Uploading the Code to Arduino Uno R3:
Congratulations! You've completed the code for your touchless hand sanitizer dispenser. It's time to upload it to your Arduino Uno R3. Make sure you've selected the correct board and port under the "Tools" menu in the Arduino IDE. Connect your Arduino board to your computer using a USB cable, and click the "Upload" button in the Arduino IDE to upload the code.
7. Testing the Touchless Hand Sanitizer Dispenser:
Once the code is successfully uploaded, disconnect your Arduino board from your computer and power it using the chosen power supply (e.g., 9V battery). Place your hand in front of the ultrasonic sensor, and if everything is set up correctly, the dispenser should trigger the sanitizer release. Adjust the angles and delays in the code as needed to achieve the desired functionality.
Part 5: Enhancements and Additional Features
Welcome back to the second part of our tutorial on building a touchless hand sanitizer dispenser using Arduino Uno R3. In this section, we'll explore some enhancements and additional features to make our dispenser more user-friendly and efficient. Let's get started!
1. Adding LED Indicators:
LEDs can be used to provide visual feedback, indicating the status of the dispenser. For example, you can add a green LED to indicate that the dispenser is ready for use and a red LED to indicate when the sanitizer is being dispensed. Here's how to incorporate LED indicators into our code:
Add the following lines of code at the beginning, after the variable declarations:
#define GREEN_LED_PIN 6
#define RED_LED_PIN 7
void setup() {
// ...
pinMode(GREEN_LED_PIN, OUTPUT); // Set the green LED pin as an output
pinMode(RED_LED_PIN, OUTPUT); // Set the red LED pin as an output
}
Now, update the `activateSanitizer()` function to include LED control:
void activateSanitizer() {
// ...
digitalWrite(GREEN_LED_PIN, LOW); // Turn off the green LED
digitalWrite(RED_LED_PIN, HIGH); // Turn on the red LED
// ...
digitalWrite(GREEN_LED_PIN, HIGH); // Turn on the green LED
digitalWrite(RED_LED_PIN, LOW); // Turn off the red LED
}
In this example, we assume that the green LED is connected to pin 6 and the red LED is connected to pin 7. Modify these pin assignments based on your setup. The LEDs are initially turned off (LOW) in the `setup()` function. When the sanitizer is activated, the green LED turns off, and the red LED turns on. Once the dispensing process is complete, the green LED turns on, and the red LED turns off.
2. Adding a Buzzer for Auditory Feedback:
Another useful addition to our dispenser is a buzzer that provides auditory feedback when the sanitizer is dispensed. Here's how to incorporate a buzzer into our code:
Add the following line of code at the beginning, after the LED pin declarations:
#define BUZZER_PIN 8
void setup() {
// ...
pinMode(BUZZER_PIN, OUTPUT); // Set the buzzer pin as an output
}
Update the `activateSanitizer()` function to include buzzer control:
void activateSanitizer() {
// ...
digitalWrite(BUZZER_PIN, HIGH); // Turn on the buzzer
delay(500); // Buzz for 500 milliseconds (adjust as needed)
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
// ...
}
In this example, we assume that the buzzer is connected to pin 8. Adjust the pin assignment according to your setup. The buzzer is turned on using `digitalWrite(BUZZER_PIN, HIGH)`, and after a brief delay, it's turned off using `digitalWrite(BUZZER_PIN, LOW)`. Modify the delay duration to control the buzzer's buzzing length.
3. Implementing a Refill Indicator:
It's essential to know when the sanitizer container needs to be refilled. To implement a refill indicator, we can utilize the concept of weight sensing. By measuring the weight of the sanitizer container, we can determine its fill level. Here's how to add a refill indicator:
- Mount a load cell (such as an HX711)
on a stable platform and connect it to the Arduino board. Follow the load cell's datasheet for wiring instructions.
- Install the HX711 library in the Arduino IDE by going to "Sketch" -> "Include Library" -> "Manage Libraries" and searching for "HX711." Install the library and include it at the beginning of your code:
#include <HX711.h>
- Define the necessary pins for the load cell and create an instance of the HX711 class:
#define LOADCELL_DOUT_PIN 9
#define LOADCELL_SCK_PIN 10
HX711 scale;
void setup() {
// ...
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
}
- Update the `loop()` function to measure the weight and provide a refill indication:
void loop() {
// ...
int weight = scale.read(); // Read the weight from the load cell
if (weight < 1000) {
digitalWrite(REFILL_LED_PIN, HIGH); // Turn on the refill indicator LED
} else {
digitalWrite(REFILL_LED_PIN, LOW); // Turn off the refill indicator LED
}
// ...
}
In this example, we assume that the refill indicator LED is connected to pin `REFILL_LED_PIN`. Adjust the pin assignment accordingly. The code reads the weight from the load cell using `scale.read()`. If the weight falls below a certain threshold (here, 1000 grams), the refill indicator LED is turned on. Otherwise, it's turned off.
4. Optional: Incorporating a Motion Sensor:
To conserve power and provide an enhanced user experience, we can add a motion sensor to activate the dispenser only when someone approaches. Here's how to include a motion sensor in our project:
- Choose a suitable motion sensor (e.g., PIR sensor) and connect it to an appropriate digital pin on the Arduino board. Refer to the sensor's datasheet for wiring instructions.
- Define the pin for the motion sensor and create a variable to track motion status:
#define MOTION_SENSOR_PIN 11
bool isMotionDetected = false;
void setup() {
// ...
pinMode(MOTION_SENSOR_PIN, INPUT); // Set the motion sensor pin as an input
}
- Update the `loop()` function to check for motion before activating the sanitizer:
void loop() {
// ...
isMotionDetected = digitalRead(MOTION_SENSOR_PIN); // Check the motion sensor status
if (isMotionDetected && distance > 0 && distance <= MAX_DISTANCE) {
activateSanitizer(); // Call the function to activate the sanitizer
}
// ...
}
In this code snippet, we assume that the motion sensor is connected to pin `MOTION_SENSOR_PIN`. Modify the pin assignment according to your setup. The code uses `digitalRead()` to check the status of the motion sensor. If motion is detected and the hand is within the specified distance, the sanitizer is activated.
Part 6: Assembling the Touchless Hand Sanitizer Dispenser
Welcome to the final part of our tutorial on building a touchless hand sanitizer dispenser using Arduino Uno R3. In this section, we'll discuss the physical assembly of the dispenser and provide some closing remarks. Let's get started!
1. Mounting the Components:
Now that we have our hardware setup and the code implemented, it's time to assemble the physical components of the dispenser. Here are some general steps to guide you:
- Find a suitable enclosure or housing for your dispenser. It should be large enough to accommodate the Arduino board, ultrasonic sensor, servo motor, liquid pump, and any additional components.
- Mount the Arduino board securely inside the enclosure using screws or mounting brackets.
- Attach the ultrasonic sensor to the top of the enclosure, facing downwards. Ensure it has a clear line of sight to detect hand movements.
- Position the servo motor and liquid pump in a way that allows the sanitizer to be dispensed effectively. You may need to experiment with different angles and positions to achieve optimal results.
- Connect the necessary wires from the components to the Arduino board. Make sure the connections are secure and well-organized to avoid any interference or loose connections.
- Place the hand sanitizer container in a convenient location within the enclosure. Ensure that it is easily accessible for refilling purposes.
- Consider adding labels or markings to guide users on where to place their hands for sanitizer dispensing.
2. Powering the Dispenser:
Choose an appropriate power supply for your dispenser based on the voltage and current requirements of the components. You can use a 9V battery or a USB power source, depending on your preference and availability. Connect the power supply to the Arduino board and ensure that it powers up correctly.
3. Testing and Calibration:
Once the physical assembly is complete, test the dispenser by placing your hand in front of the sensor. Verify that the sanitizer is dispensed properly and all the indicators (LEDs, buzzer, etc.) are functioning as expected. Fine-tune the angles, distances, and timings in the code if necessary to achieve optimal performance.
4. Refining and Customizing:
Feel free to customize the appearance of your dispenser by adding additional design elements, such as graphics or labels. You can also explore using different types of sensors or actuators to enhance its functionality further. The possibilities are endless!
Congratulations on successfully building your touchless hand sanitizer dispenser using Arduino Uno R3! By combining the power of hardware and software, you've created a convenient and hygienic solution for promoting hand hygiene.
Remember, this project serves as a starting point, and you can always expand and improve upon it. Consider integrating additional features, such as data logging, wireless connectivity, or smartphone integration, to make it even more versatile and connected.
We hope you enjoyed this tutorial and found it informative. Don't forget to share your creations with others and inspire fellow makers in the community. Keep exploring, learning, and using your technical skills to make a positive impact in the world.
Stay safe, stay curious, and happy tinkering!
Future improvements
Here are a few suggestions for future improvements and enhancements to the touchless hand sanitizer dispenser:
1. Smart Connectivity: Consider adding wireless connectivity, such as Wi-Fi or Bluetooth, to the dispenser. This would allow you to monitor the sanitizer levels, track usage statistics, and receive notifications for refills or maintenance needs. You could also implement remote control or automation features through a mobile app or web interface.
2. Touchless Activation: Explore alternative methods for touchless activation. For example, you could incorporate gesture recognition or proximity sensors to detect hand movements without physical contact. This would enhance the user experience and improve the overall hygiene of the dispenser.
3. User Interface: Integrate a user interface to provide visual feedback and control options. This could include an LCD screen or a touch panel to display important information like sanitizer levels, dispenser status, or instructions for use. It could also allow users to adjust settings or select different sanitizer options.
4. Sanitizer Dispensing Options: Expand the dispenser's functionality by incorporating multiple sanitizer dispensing options. For instance, you could have different nozzles or settings for foam-based sanitizers or different sanitizer strengths. This would cater to varying user preferences and requirements.
5. Automatic Refilling: Implement an automatic refilling system to ensure a continuous supply of sanitizer. This could involve sensors to detect the sanitizer level in the container and automatically trigger a refill process when it falls below a certain threshold. It would save time and effort in manual refilling.
6. Data Analytics: Collect and analyze usage data to gain insights into hand hygiene patterns, peak usage times, or areas of high demand. This information could be valuable for facilities management, hygiene audits, or identifying areas for improvement in public health initiatives.
7. Integration with Access Control Systems: Integrate the dispenser with access control systems, such as RFID or biometric systems, to ensure that sanitizer is dispensed before entering specific areas. This would reinforce hygiene practices and help maintain a sanitized environment.
8. Energy Efficiency: Optimize power consumption by implementing sleep modes or power management techniques when the dispenser is not in use. This would prolong battery life, reduce electricity costs, and make the dispenser more environmentally friendly.
Remember, these suggestions are meant to inspire you to explore and innovate further with your touchless hand sanitizer dispenser project. Adapt and customize them according to your specific needs and creativity. Good luck with your future improvements!