Alarm clock which splashes water on your face
Tags: python, raspberrypi, beginners, tutorial
Published: 2023-12-24 06:45:44


This was my first ever project with a Raspberry Pi. This isn't a complicated project, and anyone with a little bit of coding knowledge can achieve it. I am just messing with random tools to make a cool little project. Here are all the components I used in this project:

- [DC 5V Micro Submersible Water Pump
](https://www.amazon.com/dp/B08PBQ1N1G?psc=1&ref=ppx_yo2ov_dt_b_product_details)
- Silicon Pipe - Came with the pump ^^^
- [5v Relay Module](https://www.amazon.com/dp/B00VRUAHLE?psc=1&ref=ppx_yo2ov_dt_b_product_details)
- [Raspberry Pi 3b+ ](https://www.amazon.com/ELEMENT-Element14-Raspberry-Pi-Motherboard/dp/B07P4LSDYV/ref=sr_1_9?crid=ER1J5KZAZDK6&keywords=rpi+3b%2B&qid=1703399619&sprefix=rpi+3b%2B%2Caps%2C143&sr=8-9#customerReviews)( you can use almost any kind of RPI ) 
- [9V battery](https://www.amazon.com/Amazon-Basics-Performance-All-Purpose-Batteries/dp/B00MH4QM1S/ref=sr_1_1_ffob_sspa?crid=2USYRYQ9G77SA&keywords=9V+battery&qid=1703399666&rdc=1&sprefix=9v+battery%2Caps%2C415&sr=8-1-spons&sp_csd=d2lkZ2V0TmFtZT1zcF9hdGY&psc=1)
- [9V Battery Clip
](https://www.amazon.com/dp/B01IFP0N3U?psc=1&ref=ppx_yo2ov_dt_b_product_details)
- [Dupont Wire](https://www.amazon.com/dp/B01EV70C78?psc=1&ref=ppx_yo2ov_dt_b_product_details) (female-female and male-female) 
- [Electric Tape](https://www.amazon.com/Wapodeai-Electrical-Temperature-Resistance-Waterproof/dp/B07ZWC2VLX/ref=sr_1_4?keywords=Electric+Tape&qid=1703399739&s=industrial&sr=1-4)

**IMPORTANT**

_Before we get started, I want to explain how each component will work with each other._

_RPI (Raspberry Pi) is used to power the relay switch and to tell the relay switch when to turn on and off. The relay switch will be in the middle of the connection between the motor and the battery and turn on at a specific time, which will be provided by our code in RPI._


**_LET's GET STARTED:_** 

**PART A:** Connecting the RPI to the relay switch
Firstly, I attached the ground wire from the RPI (GPIO Pin 6) to the GND of the relay. Then I connected the 5V wire (GPIO pin 2) to the VCC pin of the relay. Finally, I connected the GPIO pin 40 to the SIGNAl on the relay.
- GND is used to complete the circuit. ( IF YOU DON'T UNDERSTAND THIS YOU MIGHT WANT TO DO SOME RESEARCH: [How to use a relay](https://www.youtube.com/watch?v=5NxVmg8ZFEc&ab_channel=bytesizedengineering) ) 
- VCC is used to power the relay switch
- SIGNAL is used to pass our code

![idk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5uffmiy1nwpmi066dur.JPG)

**PART B:** Understanding the motor and battery connection

In the image below you can see that the black wires from both the motor and battery are connected, completing the circuit’s ground connection. Connecting the red wires provides power to operate the motor.

![idk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ow6genkp92si53rvjt67.JPG)

**PART C:** Adding relay switch between the battery and motor connection

Now think of a relay switch as just a switch between the 2 red wires connecting the battery and the motor. I want the wires to only connect at a specific time and for a certain amount of time; that's why I use the relay switch. The relay switch has three output holes: common, normally open, and normally closed. Normally closed means that the circuit is normally complete. Normally open means that it's normally NOT closed. Common is just used to complete the circuit. I have connected our battery's red wire (power wire) to the open port because I want to control when the circuit is closed, and I have connected the red wire from the motor to the common port. I used electric tape to tape the two black wires. Now every time I turn on the relay switch, the two red wires connect, completing the circuit and turning the motor on.

COMPLETELY OPTIONAL: _In this picture, I used a male-to-female Dupont wire just because my wire for the motor was not long._ 

![idk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6pf3p969xqwemluojbx.JPG)

**PART D:** Connecting everything 

Now you hopefully understand the whole circuit:
- The RPI powers the relay switch and gives it the signal on when to turn on and off.
- When the relay switch turns on, it'll finish the circuit between the motor and the battery, activating the motor.
- The motor will be inside a cup of water, which will then take the water from that and output it through a hole on the top, where I connected a silicon pipe.

_ I attached an empty mechanical pencil at the point of output so the water would shoot out faster._

![idk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9wxnmu27h4hkae3p5vc.JPG)

![idk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/po0a1b85v76d043bez1b.JPG)

**Part E:** Coding!!!

Tbh, I am not the best at coding, but for this project, I invested a good amount of time coding and trying different things. First, I tried to just launch water on my face but realized that it wasn't the best because all the water would hit exactly one spot. **But** here's the code for that if you want to experiment with it. You will want to edit the time you want to wake up

```
import RPi.GPIO as GPIO
import time
import datetime
GPIO.setmode(GPIO.BCM)

relay_pin = 21

# Set the pin as output
GPIO.setup(relay_pin, GPIO.OUT)

def activate_relay():
    print("Activating relay switch...")
    GPIO.output(relay_pin, GPIO.HIGH)  # Turn on the relay
    time.sleep(10)  # Keep the relay on for 10 seconds
    GPIO.output(relay_pin, GPIO.LOW)  # Turn off the relay
    print("Relay switch deactivated.")

try:
    while True:
        now = datetime.datetime.now().time()
        print("Current Time:", now)  # Print the current time
        # CHANGE THE TIME ACCORDING TO YOUR PREFRENCE, THE CODE USES MILITARY TIME
        if now.hour == 23 and now.minute == 30:
            activate_relay()
            break  
        time.sleep(5)  

except KeyboardInterrupt:
    pass

finally:
    GPIO.cleanup()
```

I didn't like how the code above worked, so I changed it so it would burst the water out in pulses instead of a steady stream. I also made the code so that the pulse would differ every time. You will want to edit the time you want to wake up

```
import RPi.GPIO as GPIO
import time
import datetime
import random

GPIO.setmode(GPIO.BCM)
relay_pin = 21
GPIO.setup(relay_pin, GPIO.OUT)

def spray_water():
    for _ in range(30): # CHANGE ACCORDING TO YOUR PREFERENCE
        on_duration = random.uniform(0.2, 0.9)# CHANGE ACCORDING TO YOUR PREFERENCE
        off_duration = 0.3 # CHANGE ACCORDING TO YOUR PREFERENCE

        GPIO.output(relay_pin, GPIO.HIGH)
        time.sleep(on_duration)
        GPIO.output(relay_pin, GPIO.LOW)
        time.sleep(off_duration)
    print("Water spraying completed.")

try:
    # AGAIN, CHANGE THE TIME ACCORDING TO YOUR PREFRENCE, THE CODE USES MILITARY TIME
    target_time = datetime.time(20, 2)

    while True:
        now = datetime.datetime.now().time()
        print("Current Time:", now)

        if now.hour == target_time.hour and now.minute == target_time.minute:
            spray_water()
            break

        time.sleep(5)  # CHANGE ACCORDING TO YOUR PREFERENCE

except KeyboardInterrupt:
    pass

finally:
    GPIO.cleanup()
```
I [connected to my RPI using the VNC viewer](https://www.youtube.com/watch?v=NWBmYnNvN3A&t=383s&ab_channel=CarolineDunn) and ran the code in Thonny. 

**PART F:** Putting everything together

I took an empty box, made a couple of holes with a knife, put everything in the box, and got the wiring out of the holes. I kept the battery inside the box because I didn't want water to get on the battery or wires. 

![idk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7c3w6xcf641j0h803x7.JPG)

![idk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5d1bcqhysqpiwg2svn2.JPG)

![idk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtu22jp37tf7dtzera0i.JPG)

This project was inspired by [Alan Constantino 
](https://dev.to/alanjc/water-your-plant-using-a-raspberry-pi-and-python-2ddb#comment-1dcic)