tungbuivn
Published © LGPL

Soap dispenser arduino nano r3

The simple soap dispenser for the hand wash some disc in the kitchen

BeginnerProtip1 hour207
Soap dispenser arduino nano r3

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Linear Regulator (7805)
Linear Regulator (7805)
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Resistor 10k ohm
Resistor 10k ohm
×3
Capacitor 220 µF
Capacitor 220 µF
×1
Capacitor 22 µF
Capacitor 22 µF
×1
e18 d80nk promitxi sensor
×1
power supply 12v 3a
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×1
rs385 pump dc motor 12v
×1
d882 transitor npn
×1
s8550 transitor pnp
×1

Software apps and online services

VS Code
Microsoft VS Code

Story

Read more

Schematics

schematic

circuit diagram

Code

the code

Arduino
this code copied from arduino sample with abit modified
#include <avr/sleep.h>

/* Sleep Demo Serial
 * -----------------
 * Example code to demonstrate the sleep functions in an Arduino.
 *
 * use a resistor between RX and pin2. By default RX is pulled up to 5V
 * therefore, we can use a sequence of Serial data forcing RX to 0, what
 * will make pin2 go LOW activating INT0 external interrupt, bringing
 * the MCU back to life
 *
 * there is also a time counter that will put the MCU to sleep after 10 secs
 *
 * NOTE: when coming back from POWER-DOWN mode, it takes a bit
 *       until the system is functional at 100%!! (typically <1sec)
 *
 * Copyright (C) 2006 MacSimski 2006-12-30
 * Copyright (C) 2007 D. Cuartielles 2007-07-08 - Mexico DF
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses></1sec>.
 *
 */
int pumpPin = 8;
int wakePin = 2;     // pin used for waking up
int sleepStatus = 0; // variable to store a request for sleep
volatile int count = 0;       // counter
volatile bool triggered = false;   

void wakeUpNow() // here the interrupt is handled after wakeup
{
  if (triggered) {
    return;
  }
  triggered=true;
  count++;
  // execute code here after wake-up before returning to the loop() function
  // timers and code using timers (serial.print and more...) will not work here.
  // we don't really need to execute any special functions here, since we
  // just want the thing to wake up
}

void setup()
{
  pinMode(wakePin, INPUT);
  pinMode(pumpPin, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(115200);

  /* Now it is time to enable an interrupt. In the function call
   * attachInterrupt(A, B, C)
   * A   can be either 0 or 1 for interrupts on pin 2 or 3.  
   *
   * B   Name of a function you want to execute while in interrupt A.
   *
   * C   Trigger mode of the interrupt pin. can be:
   *             LOW        a low level trigger
   *             CHANGE     a change in level trigger
   *             RISING     a rising edge of a level trigger
   *             FALLING    a falling edge of a level trigger
   *
   * In all but the IDLE sleep modes only LOW can be used.
   */
  // use interrupt 0 (pin 2) and run function
  // wakeUpNow when pin 2 gets LOW
  // attachInterrupt(digitalPinToInterrupt(wakePin), wakeUpNow, LOW);
  digitalWrite(pumpPin, LOW);
  digitalWrite(LED_BUILTIN, LOW);
}

void sleepNow() // here we put the arduino to sleep
{
  /* Now is the time to set the sleep mode. In the Atmega8 datasheet
     * https://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf on page 35
     * there is a list of sleep modes which explains which clocks and
     * wake up sources are available in which sleep mode.
     *
     * In the avr/sleep.h file, the call names of these sleep modes are to be found:
     *
     * The 5 different modes are:
     *     SLEEP_MODE_IDLE         -the least power savings
     *     SLEEP_MODE_ADC
     *     SLEEP_MODE_PWR_SAVE
     *     SLEEP_MODE_STANDBY
     *     SLEEP_MODE_PWR_DOWN     -the most power savings
     *
     * For now, we want as much power savings as possible, so we
     * choose the according
     * sleep mode: SLEEP_MODE_PWR_DOWN
     *
     */
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here

  sleep_enable(); // enables the sleep bit in the mcucr register
                  // so sleep is possible. just a safety pin

  /* Now it is time to enable an interrupt. We do it here so an
     * accidentally pushed interrupt button doesn't interrupt
     * our running program. if you want to be able to run
     * interrupt code besides the sleep function, place it in
     * setup() for example.
     *
     * In the function call attachInterrupt(A, B, C)
     * A   can be either 0 or 1 for interrupts on pin 2 or 3.  
     *
     * B   Name of a function you want to execute at interrupt for A.
     *
     * C   Trigger mode of the interrupt pin. can be:
     *             LOW        a low level triggers
     *             CHANGE     a change in level triggers
     *             RISING     a rising edge of a level triggers
     *             FALLING    a falling edge of a level triggers
     *
     * In all but the IDLE sleep modes only LOW can be used.
     */
  // use interrupt 0 (pin 2) and run function
  // wakeUpNow when pin 2 gets LOW
  attachInterrupt(digitalPinToInterrupt(wakePin), wakeUpNow, FALLING);
  // here the device is actually put to sleep!!
  // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
  // first thing after waking from sleep: disable sleep...
  sleep_mode();
  sleep_disable();
  // disables interrupt 0 on pin 2 so the
  // wakeUpNow code will not be executed
  // during normal running time.
  detachInterrupt(digitalPinToInterrupt(wakePin));
}

void loop()
{
 
  // sleep function called here
  sleepNow();
  bool bored = false;
  // Serial.println("sleep now");
  // delay(100);
  if (count > 1)
  {
    Serial.println("trigger pump");
    // trigger output 5v to pump
    digitalWrite(pumpPin, HIGH);
    delay(300);
    digitalWrite(pumpPin, LOW);
    // trong vong 2s tiep theo khong nhan tin hieu
  
    // wait for pin become low while user keep hand on sensor
  }
  delay(2000);
  triggered=false;
  // while (!bored)
  // {
  //   Serial.println("wait sensor off");
  //   delay(100);
  //   bored = digitalRead(wakePin) == HIGH;
  // }
}

Credits

tungbuivn
9 projects • 1 follower

Comments