Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right
Article Directory
Preface
Through the WeMos D1 development board, SG90 steering gear, ultrasonic module, and buzzer, the trash can lid can be automatically opened and closed.
1. Project software and hardware platform and development environment
1. Hardware platform
Features
1. Based on ESP-8266EX 2.
Arduino compatible, use rduino IDE to program
3.11 x I/O pins
4.1 x ADC pins (input range 0-3.3V)
5. Onboard 5V 1A switching power supply (high input voltage 24V ) At
work:
Similar to the development of stm32 modules, the highly integrated
STM32 solution is also more economical and cheaper
2. Software platform
3. Install USB to serial port driver
4. Add support so that WeMos can be connected under ArduiNo
4.1 Check if the connection is complete
Two, drive the demonstration
1. Buzzer
//Basic control: the buzzer calls and does not call
void setup()
{ pinMode(D5, OUTPUT); //Set the pin as an output pin }
void loop()
{ digitalWrite(D5, HIGH); // output high level, the buzzer shuts up delay(1000); // you have to shut up within this second digitalWrite
2. Wemos serial communication
Serial port related functions:
simple communication between serial port and wemos:
serial port control buzzer code:
#define BEEP D5
void setup() { Serial.begin(115200); //Initialize the serial port and set the baud rate to 115200 pinMode(BEEP, OUTPUT); //Set the pin to the output pin digitalWrite(BEEP, HIGH); // Power on Don't let the buzzer ring }
void loop()
{ int cmd; if (Serial.available()> 0) {//Check whether there is data in the serial port cmd = Serial.read(); // Read serial port data Serial.println(cmd); if (cmd == 1) {//if the read data is 1 digitalWrite(BEEP, LOW); // the buzzer sounds } else { digitalWrite(BEEP, HIGH); // otherwise (read data is not 1) buzzer Not ringing } } }
3. SG90 steering gear
3.1 Servo parameters and their calling functions
3.2 Servo opening test
#include<Servo.h> //The header file is Servo
#define DUOPIN D5 //Name pin D5 as DUOPIN
Servo SG90; //Name the Servo function as SG90, which is C++ code.
void setup() { SG90.attach(DUOPIN);//Call the attach function, connect the servo, that is, connect the pin DUOPIN }
void loop() { SG90.write(0);//Control the steering gear to 0°position delay(1000);//Delay for 1 second SG90.write(90);//Control the steering gear to 90°position delay(1000); }
4. Ultrasonic ranging
HC-SR04 ultrasonic recognition module introduces the
realization of ultrasonic distance measurement programming (the distance is less than 10 triggers the buzzer to sound):
#define Echo D2
#define Trig D8
#define BEEP D5
long getTime()
{
digitalWrite(Trig,HIGH);
delayMicroseconds(10);
digitalWrite(Trig,LOW);
return pulseIn(Echo,HIGH);
}
void initChaoShengBo()
{
pinMode(Echo,INPUT);
pinMode(Trig,OUTPUT);
}
void initBeep()
{
pinMode(BEEP,OUTPUT);
digitalWrite(BEEP,HIGH);
}
void setup()
{
initChaoShengBo();
initBeep();
Serial.begin(115200);
}
void loop()
{
//获取距离
long dis;
dis = getTime()/58;
Serial.print(dis);
Serial.println(“cm”);
if(dis < 10)
{
digitalWrite(BEEP,LOW);
}
else
{
digitalWrite(BEEP,HIGH);
}
delay(500);
}
5. Buzzer
void setup() { pinMode(D5, OUTPUT); //Configure D5 as output port }
void loop() {//After power-on, the code in the loop function should be executed continuously. The core control code should be written into loop
//HIGH, LOW has passed the macro definition
digitalWrite(D5, HIGH); // output high level, bee Shut up the buzzer
delay(1000); //The unit is ms, you have to shut up within this second
digitalWrite(D5, LOW); //Output low level, the buzzer roars
delay(1000); //Roar One second (during the delay, the buzzer control pin is low level)
}
5.1 Control the buzzer's ringing and not ringing through the serial port
void setup() { Serial.begin(115200); //Serial communication, first set the baud rate pinMode(D5,OUTPUT); //Set D5 to the output state digitalWrite(D5,HIGH); //The other D5 outputs high Level, the buzzer does not sound once power is turned on }
void loop() {
if(Serial.available()>0){ //If the serial port has data
if(Serial.read()== 1){ //而且到来的数据是数字1
Serial.println("DEE!!!");
digitalWrite(D5,LOW); //D5输出低电平,让蜂鸣器响
}
else{
Serial.println("HUSH~");
digitalWrite(D5,HIGH);//D5输出低电平,让蜂鸣器不响
}
}
}
Three, code implementation
1. Related functions
2. Code integration
#include<Servo.h>
#define Trig D8
#define Echo D2
#define duoji D5
Servo myduoji;
long init_chaoshengbo()
{
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
}
long gettime()
{
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
return pulseIn(Echo, HIGH);
}
void setup()
{
init_chaoshengbo();
myduoji.attach(duoji);
pinMode(duoji, OUTPUT);
}
void loop()
{
long dis;
dis = gettime() / 58;
if (dis < 10)
{
myduoji.write(160);
}
else
{
myduoji.write(30);
}
}
to sum up
After the production is completed, it can be powered by a mobile power supply. When there is an object in the designated area in the front of the trash can, the lid will automatically open. At this time, the items can be put into the bucket. After a certain delay, the lid will automatically close. If there are always objects in the designated area at the front of the trash can, the lid will remain open.