Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Teodor AnițoaeiIoan StanciuAlexandru LupusoruAlex Mirt
Published © GPL3+

Controlling fan speed by proximity

Control a fan by getting closer to the sensor.

BeginnerFull instructions provided2 hours251
Controlling fan speed by proximity

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
DC Motor, Miniature
DC Motor, Miniature
×1
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×8
Male/Male Jumper Wires
×3

Software apps and online services

Python 3.7
Bottle Web Server
Raspbian
Raspberry Pi Raspbian
PuTTY

Story

Read more

Schematics

Project Schematic

Code

Server and Pi Code

Python
from bottle import route, run, template, request
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

trig = 16
echo = 18

GPIO.setup(trig, GPIO.OUT)
GPIO.setup(echo, GPIO.IN)

GPIO.setmode(GPIO.BOARD)

motorspeedPin = 8

DIRA = 10
DIRB = 22

GPIO.setup(motorspeedPin, GPIO.OUT)
GPIO.setup(DIRA, GPIO.OUT)
GPIO.setup(DIRB, GPIO.OUT)

pwmPIN = GPIO.PWM(motorspeedPin, 100)

pwmPIN.start(0)


lastMotorPower = 70
isMotorEnabled = False

def turnOff():
        pwmPIN.ChangeDutyCycle(0)


def calculateDistance():
    GPIO.output(trig, GPIO.HIGH)

    time.sleep(0.00001)
    GPIO.output(trig, GPIO.LOW)

    start = time.time()
    stop = time.time()

    while GPIO.input(echo) == 0:
        start = time.time()

    while  GPIO.input(echo) == 1:
        stop = time.time()

    duration = stop - start

    distance = 34300/2 * duration

    if distance < 0.5 and distance > 400:
        return 0
    else:
        return distance

@route('/')
def index():
    return template('index.html')


@route('/start')
def start():
    global isMotorEnabled
    isMotorEnabled = True
    return 'ok'


@route('/stop')
def start():
    global isMotorEnabled
    isMotorEnabled = False
    return 'ok'


@route('/refresh')
def refresh():
    global lastMotorPower
    global isMotorEnabled
    
    if isMotorEnabled == True:
        dist = calculateDistance()
        motorPower = dist + 20
        motorPower = min(100, motorPower)

        if (abs(lastMotorPower - motorPower) > 5):
            lastMotorPower = motorPower
        
        print(motorPower, lastMotorPower)
        
        pwmPIN.ChangeDutyCycle(lastMotorPower)
        
        GPIO.output(DIRA, GPIO.HIGH)
        GPIO.output(DIRB, GPIO.LOW)

        return '%d' % lastMotorPower
    else:
        turnOff()
    
    return 0

try:
    run(host = '0.0.0.0', port = '5000')

except KeyboardInterrupt:
    pass


GPIO.cleanup()
turnOff()

Index Page

HTML
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css">
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <title>Hand Controlled Fan</title>
  </head>

  <body>
      <div class="container-fluid" style="font-size:10vh;color:red">
        <div class="row">
          <div class="col-sm-12 text-center">
            <h1 id="status">Motor is idle</h1>
          </div>
        </div>

        <div class="row">
          <div class="col-sm-12 text-center">
            <i id="button-start" class="fas fa-play"></i>
          </div>
        </div>

        <div class="row">
          <div class="col-sm-12 text-center">
            <i id="button-stop" class="fas fa-stop"></i>
          </div>
        </div>

        <div class="row" style="height:10vh;"> 
          <div class="col-sm-12 text-center">
            <h1 id="moto-speed">Moto speed</h1>
          </div>
        </div>

      </div>

      <script>

        $('#button-start').on('mousedown', function(){
          $('#status').html('Motor is working');
          $.ajax('/start');
        });

        $('#button-stop').on('mousedown', function(){
          $('#status').html('Motor is idle');
          $.ajax('/stop');
        });
        
        $(document).ready(function(){
          setInterval(refreshFunction,100);
        });

        function refreshFunction(){
          $.getJSON('/refresh', function(result){
            $('#moto-speed').html(result);
          })};

      </script>

  </body>
</html>

Credits

Teodor Anițoaei
1 project • 0 followers
Contact
Ioan Stanciu
1 project • 0 followers
Contact
Alexandru Lupusoru
1 project • 0 followers
Contact
Alex Mirt
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.