步骤1:所需的电气组件
这是电气零件的列表一个人需要完成这个项目-br-arduino板
-跳线
-rgb led(我使用的是公共阴极,但单个led也可以工作)
-3电阻。
-面包板
-hc-sro4。
所有这些都连接起来
步骤2:连接
从arduino + 5v向面包板提供+ 5v并执行相同的操作为地面。
将hc-sr04安装在试验板上。请记住,在数字电子产品类别中,+规则垂直针脚在板上横档连接在一起,而水平针脚在电源横档上连接在一起。
将传感器连接到gnd和+ 5v。
连接回波针将hc-sr04的引脚连接到arduino的引脚5,并通过trig引脚将其连接到arduino板的引脚6。
安装面包板上的led。将电阻连接到rgb引脚,分别将它们分别连接到arduino的引脚7,8和9,并将阴极接地。
步骤3:代码
连接后,编写并上传以下代码。
************************* ************************************************** *************
#define trigpin 6
#define echopin 5
#define r 7
#define g 8
#define b 9
void setup()
{ //serial monitor and pin setup.
serial.begin(9600);
pinmode(trigpin,output); //set trigpin as output
pinmode(echopin,input);//set echopin as input
pinmode(r,output);// set r,g and b as outputs
pinmode(g,output);
pinmode(b,output);
// put your setup code here, to run once:
}
void loop()
{
//the trigpin sends out a signal, which bounces off an obstacle and comes back, the
//echopin recieves this signal and gives out +5v setting the arduino pin on which it is connected to high.
//distance= time*speed, but this distnce is divided by 2 because signal sent out returns
//so distance= (the time it takes for the signal to leave and return)/2.
//i.e if the time is 6s the distance = (6s/2) = 3m or cm.
int duration, distance;//declare distance and duration as integers
digitalwrite(trigpin,high);// trigin send out signal
_delay_ms(1000);//coninously for 1000ms
digitalwrite(trigpin, low);// then goes low
duration=pulsein(echopin,high); // duration is the pulsein to the echopin
distance=(duration/2)/29.1; // the 29.1 is used to convert the distnce to cm, the value varies for other units.
if(distance 》 0 && distance 《= 20){//distcance is greater than 0 and less than 20cm
digitalwrite(g,low);//green led is off
digitalwrite(b,low);//blue led is off
_delay_ms(500);//delay
digitalwrite(r,high);//red led is on
_delay_ms(500);
}
else if(distance 》 20 && distance 《= 80){//distcance is greater than 20 and less than 80cm
digitalwrite(r,low);//red led is off
digitalwrite(g,low);//green led is off
_delay_ms(500);
digitalwrite(b,high);//blue led is on
}
else if(distance 》 80 && distance 《= 120 ){//distcance is greater than 80 and less than 120cm
digitalwrite(r,low);//red led is off
digitalwrite(b,low);//blue led is off
_delay_ms(500);
digitalwrite(g,high);//green led is on
}
serial.print(“cm”);
serial.println(distance);//print values on serial monitor
_delay_ms(100);
}
// put your main code here, to run repeatedly:
步骤4:测试
我公寓的地板上有瓷砖,您可以看到距我的橱柜和冰箱的距离。 led呈绿色,当距离减半时变为蓝色,而当传感器确实靠近时变为红色。我希望你喜欢这个。我打算做类似的事情,但我将使用mq-2烟雾传感器来更改led的颜色。
旋转磁力机械模拟时涉及的一些概念
Imagination中国区经理刘国军:是什么原因转投半导体IP厂商?
光显投影模块散热,非硅导热凝胶打破国外垄断
华为荣耀9和小米6谁才是3000元内的最强?华为荣耀9和小米6全方位区别对比评测
5G赋能智慧医疗前景是美好的但是具体落地还有很多困难
超声波距离传感器的制作
正确处理有助于充分利用锂离子电池
星光摄像头和黑光摄像头有什么区别
SY6288CAAC 低损耗配电开关
环境电器智能化解决方案 AIoT开发平台 机智云AIoT云服务
Argus II仿生眼,帮助盲人重见光明!
发布会来袭,一加 7 Pro将搭载更好的屏幕和更激进的外观设计
OpenHarmony技术峰会展区:生态成果精彩亮相
一体化步进电机_用stm32的一体式步进电机
可控硅(SCR)基本概念
pcb立碑什么意思 如何防止pcb立碑现象发生
威迈斯:独到研发模式 为发展注入强有力的定心剂
威刚发布15.6 英寸的超极本XENIA Xe :搭载英特尔 11 代酷睿和自家 PCIe 4.0 SSD
AMD、Intel处理器搞“核战”
谷歌向现实低头,关闭Android Things系统