What Will I Learn?
实践使用Arduino的串口
实现用指令控制LED的亮灭
Requirements
Arduino UNO
Arduino IDE开发环境
串口调试助手
Difficulty
基础
Tutorial Contents
一、实践使用Arduino的串口
打开arduino.exe开发环境
对Arduino串口进行初始化
void setup() { // put your setup code here, to run once: Serial.begin(9600); }
当串口有数据接收到时,获取储存在缓冲区的串口数据
if(Serial.available()) { rec = Serial.read(); Serial.print(rec); }
向串口发送数据
打印数据显示
对获取到的据进行判断
if(Serial.available()) { rec = Serial.read(); if(rec == '1') Serial.print("this is the number 1"); else if(rec == '2') Serial.print("this is the number 2"); }
二、实现电脑发送指令控制LED的亮灭
需求分析:结合步骤一,可以成功的获取串口发送的数据。当获取到串口数据的时候,加以判断
- 若指令为“on”则提供引脚以高电平,也就是点亮LED
- 若指令为“off”则提供引脚以低电平,也就是熄灭LED
连接线路
Arduino LED D13 长脚 GND 短脚 整合代码
char rec; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(13,OUTPUT); digitalWrite(13,LOW); } void loop() { // put your main code here, to run repeatedly: if(Serial.available()) { rec = Serial.read(); Serial.print(rec); if(rec == 'on') { digitalWrite(13,HIGH); } else if (rec == 'off') { digitalWrite(13,LOW); } } }
运行测试
Curriculum
- Arduino中文教程---认识Arduino的串口并编写程序操作串口
- Arduino中文教程---认识HC-SR501模块并制作人体感应LED
- Arduino中文教程---认识模拟输入并制作声控电灯
- Arduino中文教程---DIY一个按钮电灯
- Arduino中文教程---初识Arduino并点亮第一个LED
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
hi good morning,thank you
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @cha0s0000 I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit