I am building a automatic hand sanitizer dispensor with an arduino nano. Basically, there is this motor which pushes down on the dispenser and the dispenser dispenses the sanitizer. The motor is controlled by the ultrasonic sensor. I added an LCD just to show off.
Part List:
- Arduino Nano
- LCD
- L298N Motor Driver module to control the motor
- Motor
- Ultrasonic Sensor
Now to the main part.
I completed coding and building the project and was at the last stage, connecting it to external power source. I connected the +ve pin of a 9V battery to the +12V pin of the driver module and -ve pin of the 9V battery to the GND pin of the driver module (Which was connected to the GND pin of the Arduino). I had connected a jumper wire from the +12V pin to the Vin pin of the arduino. Now the results were a bit strange and varied. I have attached the videos of the outputs.
The strange part was that the project worked perfectly fine when connected to a computer with a USB port. Just adding external power ruined it all! I have attached the expected output (When it was connected with a USB port).
The Connections:
- L298N Driver Input 1 - Arduino D9
- L298N Driver Input 2 - Arduino D10
- L298N Driver +12V - Arduino Vin and +ve terminal of 9V Battery
- L298N Driver +5V - Arduino 5V
- L298N Driver GND - Arduino GND
- Ultrasonic Sensor VCC - Arduino 5V
- Ultrasonic Sensor GND - Arduino GND
- Ultrasonic Sensor Trig Pin - Arduino D11
- Ultrasonic Sensor Echo Pin - Arduino D12
- LCD I2C SDA Pin - Arduino A4
- LCD I2C SCL Pin - Arduino A5
- LCD I2C VCC Pin - Arduino 5V
- LCD I2C GND Pin - Arduino GND
Here’s the code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LCD.h>
#define I2C_ADDR 0x27 //Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
const int echoPin = 12;
const int trigPin = 11;
const int motorPin1 = 9;
const int motorPin2 = 10;
long duration;
int distance;
void setup() {
lcd.begin (16,2);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
lcd.setCursor(1,0);
lcd.print("Stay Calm and");
lcd.setCursor(4,1);
lcd.print("Sanitize");
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
if (distance < 5.5) {
Serial.println("Sanitizing...");
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
delay(1500);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
delay(2000);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
}
I am very confused as to what is happening. What is the problem here?
Please help.
Thanks.
(Here are all of the attachments because the forum only allows 2mb of files)