We will construct an automatic trash can with obstacle avoidance sensor, active buzzer and servo.
Required:
Step 1 to 10 are the same as those in 4.4, so you can directly use the form in 4.4 here.
① Remove the joystick.
② mount from step 11.
Yet if you have not assemble the building blocks in 4.4, please start from step 1.
Step 1
Step 2
Step 3
Step 4
① Hold with your fingers.
② Insert the rod.
ATTENTION: keep the lid flatwise.
Step 5
Step 6
Step 7
Step 8
Step 9
Step 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.
Open Servo_Calibration.py and click .
'''
* 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.
Step 11
Step 12
Step 13
Step 14
Completed!
Open 4.5Smart bin_Automatic mode.py and click .
'''
* Filename : Smart bin_Automatic mode
* 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)
Obsensor = Pin(22,Pin.IN)
Buzzer = Pin(2,Pin.OUT)
while True:
Obsensor.value()
if Obsensor.value() == 0: #detects something
Buzzer.on() #buzzer alarms
servo.duty_u16(angle(-45)) #lid opens
else: #detects nothing
Buzzer.off() #buzzer stays quiet
servo.duty_u16(angle(90)) #lid closes
time.sleep(0.5)
Conceive:
Initialization: set the pins of obstacle avoidance sensor, buzzer and servo, set the servo output frequency.
Loop:
① Read the digital power level of obstacle avoidance sensor.
② Determine whether the read value equals 0.
If it is 0, i.e., the sensor detects obstacles, the buzzer will emit sound. At this time, the servo rotates to open the lid.
If it is not 0, i.e., there is no obstacle, the buzzer will not alarm and the servo rotates to close the lid.
After uploading code, we first need to adjust the obstacle avoidance sensor (please see chapter 3.5 for details).
After adjusting, place something in the sensing range in front of the obstacle avoidance sensor.
When the obstacle avoidance sensor detects it, the buzzer makes a sound, and the servo rotates to open the lid; When the thing leaves the sensing range, the sensor detects nothing, and the buzzer stops alarming, and the servo rotates to close the lid.