During the second week, we started assembling the circuit by connecting the ultrasonic sensor, servo motor, and OLED screen to the Arduino UNO. After setting up the hardware, we wrote the first version of our code. The code allowed the ultrasonic sensor to detect an object, and if the object (a hand) was within the correct range, the servo motor would move to push sanitizer. We also made the OLED display show a simple message, such as “Place your hand”. However, we faced several problems. The ultrasonic sensor sometimes gave incorrect readings, making the servo move at the wrong time. We solved this by adjusting the delay and using filtering techniques. Another problem was that the servo motor was not always responding smoothly, so we modified the code to improve timing. Also, updating the OLED display correctly while keeping the system responsive took some extra effort.
Circuit Explanation for Automatic Hand Sanitizer Dispenser
This circuit diagram represents the automatic hand sanitizer dispenser using an Arduino UNO as the main controller.
And this is the code we have used for the Arduino UNO.
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Servo.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define TRIG_PIN 9
#define ECHO_PIN 10
#define SERVO_PIN 6
Servo servoMotor;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
servoMotor.attach(SERVO_PIN);
servoMotor.write(0);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
while (1);
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10, 20);
display.print("Place");
display.setCursor(10, 40);
display.print("Your Hand");
display.display();
}
void loop() {
long duration;
int distance;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2;
if (distance > 0 && distance <= 10) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(20, 30);
display.print("Dropping");
display.display();
servoMotor.write(90);
delay(1000);
servoMotor.write(0);
delay(2000);
display.clearDisplay();
display.setCursor(10, 20);
display.print("Place");
display.setCursor(10, 40);
display.print("Your Hand");
display.display();
}
}
Comments
Post a Comment