X
Home > Blog > STEM for Arduino > HOW to Build a Smart Trash Can (Manual Mode) with Kidspico

HOW to Build a Smart Trash Can (Manual Mode) with Kidspico

By r September 30th, 2024 79 views
In this project, we use a joystick and a servo to control the trash can lid.

Flow

4401

Assembly

line1

Required:

44_00

line1

Step 1

44_01

line1

Step 2

44_02

line1

Step 3

44_03

line1

Step 4

44_04

① Hold the lid still to make it align with the hole.

② Insert the rod.

ATTENTION: Keep the lid flatwise.

44_14

line1

Step 5

44_05

line1

Step 6

44_06

line1

Step 7

44_07

line1

Step 8

44_08

line1

Step 9

44_09

line1

Step 10

44_10

The servo need to be calibrated before using. Connect the servo to pin io19 on the board and link the board to computer via USB cable.

44_15

Open Servo_Calibration.py and click 1407.

'''
 * Filename    : Servo_Calibration
 * Thonny      : Thonny 4.1.4
 * Auther      : http//www.keyestudio.com
'''
from machine import Pin, PWM
import time

servo = PWM(Pin(19))
servo.freq(50)  #T = 1/f = 20ms

def angle(x):
    return int((((x + 45) * 1.8 / 270) + 0.6 )/ 20 *65535)

while True:   
    servo.duty_u16(angle(90))

After calibration, disconnect the board to computer and continue to mount.

ATTENTION: During mounting, please hold and keep the lid flatwise.

44_11

line1

Step 11

44_12

line1

Step 12

44_13

line1

Completed!

44_17

line1

Wiring Diagram

4402

Test Code

Open 4.4Smart bin_Manual mode.py and click 1407.

'''
 * Filename    : Smart bin_Manual mode
 * Thonny      : Thonny 4.1.4
 * Auther      : http//www.keyestudio.com
'''
from machine import Pin,PWM
import time
from ROCKER import rocker

servo = PWM(Pin(19))
servo.freq(50)  #T = 1/f = 20ms

def angle(x):
    return int((((x + 45) * 1.8 / 270) + 0.6 )/ 20 *65535)

scl = Pin(5) 
sda = Pin(4)
bus = 0
snsr = rocker (bus, scl, sda)

while True:
    x,y,z = snsr.readXYZ()
    print('y:',y)
    time.sleep(0.1)
    if 0 <= y < 490:
        servo.duty_u16(angle(-45))
    else:
        servo.duty_u16(angle(90))

Explanation

5top

  • <=

    Comparison operator: less than or equal to, it determines whether the left value is less than or equal to the right, and then True or False will be returned.

Conceive:

  1. Initialization: set the pins of IIC and servo, set the servo output frequency.

  2. Loop:

    ① read the value of joystick in y-axis and refresh it every 0.1 seconds, and print it on Shell.

    ② determine whether the value in y-axis is within 0 to 490 (with the servo being calibrated, the trash can lid is closed at this time).

    • True: the servo rotates to -45° to open the lid.

    • False: the servo rotates to 90° to close the lid.

5bottom

Test Result
4top
Upload code and push the joystick as follows, and the lid will open. If you release the joystick, the lid will close. Now you can control the trash can manually!
4403
4bottom
HOW to Achieve Light Adjustment with Kidspico
Previous
HOW to Achieve Light Adjustment with Kidspico
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us