Project 51 - Smart Trash Can

Project 51 - Smart Trash Can

Project requirements

When people approach, the lid of the trash can is opened and garbage is put in. When people leave, the lid of the trash can is automatically closed.
And the screen displays the distance and the status of the trash can switch.

Project materials (actual pictures can be viewed on Baidu)

A trash can,
several Dupont lines,
a sg90 servo, an
ultrasonic ranging, a
Nokia5110LCD screen ( Nokia5110 usage and examples )
stc89c51 minimum system microcontroller, a
USB to TTL (can be ignored if the microcontroller has been integrated)

The above materials can be replaced and used according to their own module conditions, and they are not necessarily all the same. For example: ultrasonic waves can use other sensing devices, such as human body sensing modules, infrared modules, etc. Of course, other 51 development boards can be used, and they can also be added (deleted or modified). New device upgrade and landscaping project.

wiring

P1^0——sg90 servo signal line
P1^1——Ultrasonic Trig end
P1^2——Ultrasonic Echo end
The following Nokia5110 directly quotes the wiring of the Nokia5110 article

sbit SCLK = P2^5SCLK; // pin 2 header 5
sbit SDIN = P2^4; // pin 3 header 4
sbit LCD_DC = P2^3; // pin 4 header 3
sbit LCD_CE = P2^2; // pin 5 header 2
sbit LCD_RST = P2^1; // pin 9 header 1

Practical writing

If you haven’t designed the following code, you can take a look. These two articles use the Nokia5110
servo and examples . Ultrasound is too simple. You can find the timing diagram online . The propagation speed of sound corresponds to the ultrasonic wave I have below. You should understand it.

Part of the code (please send me the packaged code)

main.c

#include <reg51.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "nokia5110.h"
#include "ultrasonic.h"
#include "sg90.h"
#include "delay.h"

void judgment_angle(double dis)
{
    
    
	static char flag = 0;
	static unsigned int time;
	
	if(dis <= 5){
    
    
			flag = 1;
			time = 0;
//			sg90(3);				//90 angle
//			//DelayMS(1000);	
		}else{
    
    
			time++;
			if(time > 10){
    
    
				time = 0;
				flag = 0;
			}
		}
			
		if(flag) sg90(3);
		else sg90(1);
		
		if(flag)LCD_write_english_string(1,4," flag = ON  ");
		else LCD_write_english_string(1,4," flag = OFF  ");
}

int main()
{
    
    
	double dis;
	char buf[32];

	

	LCD_init();
	LCD_clear();
	sg90Init();
	InitUltrasonic();
//	LCD_write_chinese_string(0,0,12,7,0,0);
//	LCD_write_chinese_string(0,2,12,7,0,0);
//	LCD_write_chinese_string(0,4,12,7,0,0);
	
	LCD_write_english_string(1,0,"--ShunGe51--");
	LCD_write_english_string(1,1,"************");
//	LCD_write_english_string(1,2,"distance is ");
	LCD_write_english_string(1,3,"************");
//	LCD_write_english_string(1,4," flag = off ");
	while(1)
	{
    
    
		dis = GetDistance();						//get ultrasonic distance
		sprintf(buf,"dis= %02.2fcm ",dis);			
		LCD_write_english_string(1,2,buf);			//wirite for nokia5110
		judgment_angle(dis);						
		DelayMS(100);
	}
	
}

ultrasonic.c

#include <reg51.h>
#include "ultrasonic.h"
#include <delay.h>

#include "nokia5110.h"

sbit Trig = P1^1;
sbit Echo = P1^2;


void InitUltrasonic()
{
    
    
	//1. 配置定时器0工作模式位16位计时
	TMOD &= 0xF0; //设置定时器模式
	TMOD |= 0x01;
	
	TH0 = 0;
	TL0 = 0;
	
}

double GetDistance()
{
    
    
	double distance = 0;
	double time = 0;
	Trig = 0;
	Trig = 1;
	Delay10us();
	Trig = 0;
	while(!Echo);		//波发出的一瞬间开启定时
	TR0 = 1;
	while(Echo);		
	TR0 = 0;
	
	time = (TH0 * 256 + TL0)	* 1.085;		//单位us
	// 距离 = 速度 (340m/s)* 时间/2
	distance = time * 0.017;
	TH0 = 0;
	TL0 = 0;
	return distance;
}

Effect

Please add image description
Please add image description
Please add image description
Please add image description

video

uploading……

Finish

You are welcome to ask any questions and make progress together.

Guess you like

Origin blog.csdn.net/qq_52749711/article/details/132249377