817|4

450

帖子

1

TA的资源

一粒金砂(高级)

楼主
 

【Follow me第二季第2期】Arduion UR4 作业提交 [复制链接]

  本帖最后由 我的学号 于 2024-11-1 01:54 编辑

一. 视频展示

1.1 物料展示

1 Arduino UR4 wifi   
2

ST25DV16 NFC 模块

 
3 Raspberry PICOW  

 

1.2 视频演示

提交视频

 

二. 任务实现详情

2.1 入门任务(必做):搭建环境并开启第一步Blink / 串口打印Hello EEWorld!

2.1.1 物料准备&设计思路

Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线 

硬件分析:【Follow me第二季第2期】1.初识 Arduino R4

 

2.1.2 软件流程

 

 

2.1.3 代码片段

  • void setup()
  • {
  • // put your setup code here, to run once:
  • pinMode(LED_BUILTIN, OUTPUT); //LED IO 输出模式
  • Serial.begin(9600); //串口波特率9600
  • }
  • void loop()
  • {
  • // put your main code here, to run repeatedly:
  • digitalWrite(LED_BUILTIN, HIGH); //LED IO 输出高电平
  • delay(1000); //延时 1s
  • digitalWrite(LED_BUILTIN,LOW); //LED IO 输出低电平
  • delay(1000); //延时 1s
  • Serial.println("Hello Wrold!"); //串口发送数据
  • }

任务帖详解:

【Follow me第二季第2期】2.入门任务之点灯和串口通信

 

 

2.2 基础任务(必做):驱动12x8点阵LED;用DAC生成正弦波;用OPAMP放大DAC信号;用ADC采集并且打印数据到串口等其他接口可上传到上位机显示曲线

2.2.1 物料准备&设计思路

Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线 /运放反馈线路

 

2.2.2 软件流程

 

 

2.2.3 代码片段

  • #include "Arduino_LED_Matrix.h"
  • #include "analogWave.h"
  • #include "OPAMP.h"
  • //模块对象定义
  • ArduinoLEDMatrix LedMatrix;
  • analogWave Wave(DAC);
  • int WaveFre =10;
  • int i32AdcReadResult =0;
  • //数组定义
  • unsigned long u32Heart1frame[] = //大心图案
  • {
  • 0x3184a484,
  • 0x24042081,
  • 0x100a0040
  • };
  • unsigned long u32Heart2frame[] = //小心图案
  • {
  • 0x0001b024,
  • 0x81100a00,
  • 0x40000000
  • };
  • void setup()
  • {
  • // put your setup code here, to run once:
  • pinMode(LED_BUILTIN, OUTPUT);
  • Serial.begin(115200);
  • LedMatrix.begin();
  • Wave.sine(WaveFre);
  • Wave.start();
  • OPAMP.begin(OPAMP_SPEED_HIGHSPEED);
  • analogReadResolution(14);
  • }
  • void loop()
  • {
  • // put your main code here, to run repeatedly:
  • LedMatrix.loadFrame(u32Heart1frame);
  • i32AdcReadResult =analogRead(A4);
  • Serial.print("ADC Result is:"); //数据上传提示
  • Serial.print(String(i32AdcReadResult)); //转换为字符类型
  • Serial.print("\n"); //数据换行
  • delay(1); //延时等待1ms
  • }

任务帖详解:

【Follow me第二季第2期】3.基础任务之 LED 矩阵

【Follow me第二季第2期】4.基础任务之 DAC/OPAM/ADC

 

2.3 进阶任务(必做):通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)

2.3.1 物料准备&设计思路

装有 HA 的上位机/Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线

 

2.3.2 软件流程

 

 

2.3.3 代码片段

  • #include <WiFiS3.h>
  • #include <ArduinoHA.h>
  • #include "arduino_secrets.h"
  • #include "analogWave.h"
  • //RGB PIN 定义
  • #define LED_RGB_G D5
  • #define LED_RGB_R D6
  • #define LED_RGB_B D9
  • ///////please enter your sensitive data in the Secret tab/arduino_secrets.h
  • char ssid[] = SECRET_SSID; // your network SSID (name)
  • char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
  • int status = WL_IDLE_STATUS; // the WiFi radio's status
  • WiFiClient client;
  • HADevice device(MQTT_CLIENT_ID);
  • HAMqtt mqtt(client, device);
  • HALight light("MyRgbLight", HALight::RGBFeature);
  • byte u8RgbSwitch =0;
  • void setup()
  • {
  • //RGB 初始化
  • pinMode(LED_RGB_R, OUTPUT);
  • pinMode(LED_RGB_G, OUTPUT);
  • pinMode(LED_RGB_B, OUTPUT);
  • analogWrite(LED_RGB_R, 260);
  • analogWrite(LED_RGB_G, 260);
  • analogWrite(LED_RGB_B, 260);
  • //Initialize serial and wait for port to open:
  • Serial.begin(9600);
  • while (!Serial) {
  • ; // wait for serial port to connect. Needed for native USB port only
  • }
  • // check for the WiFi module:
  • if (WiFi.status() == WL_NO_MODULE)
  • {
  • Serial.println("Communication with WiFi module failed!");
  • // don't continue
  • while (true);
  • }
  • // attempt to connect to WiFi network:
  • while (status != WL_CONNECTED) {
  • Serial.print("Attempting to connect to WPA SSID: ");
  • Serial.println(ssid);
  • // Connect to WPA/WPA2 network:
  • status = WiFi.begin(ssid, pass);
  • // wait 10 seconds for connection:
  • delay(10000);
  • }
  • // you're connected now, so print out the data:
  • Serial.print("You're connected to the network");
  • device.setName("Arduino Ur4");
  • light.setName("Bathroom");
  • // handle light states
  • light.onStateCommand(onStateCommand);
  • light.onRGBColorCommand(onRGBColorCommand); // optional
  • Serial.println("\nStart connecting to MQTT server");
  • if (!mqtt.begin(MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD))
  • {
  • Serial.print("Connection falied");
  • Serial.print(mqtt.getState());
  • Serial.println("Try again in 5 seconds");
  • delay(5000);
  • }
  • }
  • void loop() {
  • // check the network connection once every 10 seconds:
  • mqtt.loop();
  • }
  • //RGB 相关函数
  • void onStateCommand(bool state, HALight* sender)
  • {
  • Serial.print("State: ");
  • Serial.println(state);
  • if(1 ==state) //RGB 开启
  • {
  • u8RgbSwitch =1;
  • }
  • else //RGB 关闭
  • {
  • u8RgbSwitch =0;
  • }
  • sender->setState(state); // report state back to the Home Assistant
  • }
  • void onBrightnessCommand(uint8_t brightness, HALight* sender)
  • {
  • Serial.print("Brightness: ");
  • Serial.println(brightness);
  • sender->setBrightness(brightness); // report brightness back to the Home Assistant
  • }
  • void onRGBColorCommand(HALight::RGBColor color, HALight* sender)
  • {
  • Serial.print("Red: ");
  • Serial.println(color.red);
  • Serial.print("Green: ");
  • Serial.println(color.green);
  • Serial.print("Blue: ");
  • Serial.println(color.blue);
  • if(1 ==u8RgbSwitch)
  • {
  • analogWrite(LED_RGB_R, (color.red*2));
  • analogWrite(LED_RGB_G, (color.green*2));
  • analogWrite(LED_RGB_B, (color.blue*2));
  • }
  • else
  • {
  • analogWrite(LED_RGB_R, 260);
  • analogWrite(LED_RGB_G, 260);
  • analogWrite(LED_RGB_B, 260);
  • }
  • sender->setRGBColor(color); // report color back to the Home Assistant
  • }

 

2.3.4 任务帖详解

Follow me第二季第2期】5.进阶任务之 树莓派 HA+Windows+UR4 控制 RGB

 

2.4 扩展任务1: 外拓板实现 NTP 实时时钟

2.4.1 硬件展示

考虑到 UR4 板载的资源不算多,为充分体验其功能设计打样并手焊了一块拓展板, 结构如下:

 

 

拓展功能有:

添加三个 PWM 引脚控制 RGB; 这里选的是共阳的 RGB

 

三个按键验证 IO 输入;需要注意 UR4 的最大输入电流 8mA; 使用时将 IO 设置为内部下拉低电平,按键按下后得到高电平

板子到手后发现防抖的电容C2 C3 C4 属于画蛇添足,可以删除

 

使用 NORFLASH 验证 SPI 功能;GD 的这款FLASH 供电 无法用 5V 供电,利用 NMOS 做了电平转换

 

digikey 上的传感器模块价格比较高,添加AHT20 验证 IIC 功能;该封装不好焊

 

无屏不成板,添加了一个 OLED 接口,这里和 温湿度传感器使用的是同一条 IIC 总线

 

针对 DAC 功能,设计了一个功放线路;SC8002B 的PIN1 在低电平时芯片工作,于是用 NMOS 做了一个开关,逻辑却是反的

D7 给高电平 -->MOS 导通 --> 8002B PIN1 为低电平  

 

针对 ADC 功能,设计了咪头+运放采样

 

模块线路在网上都可以找到借鉴,物料在淘宝也能买到;原理图电阻电容基本是0805,可以根据手上物料进行修改

绘制PCB 板时需要特别数以尺寸和插针位置;我采用的方法是从官网下载板子的CAD文件,导入立创 EDA 后复制其机械层,得到外框

连接件则使用了四个单排 2.54 插针,其位置可以通过单击CAD 插座首PIN得到XY 坐标

考虑到 LED 灯矩阵显示,拓展板上对应位置做了挖槽处理, 旁边的 UART 接口也一并留空

板子到手后才发现电源插座有干涉,同样挖槽

验证拓展板位置是否正确, 可以打印在 A4 纸上进行对比;确认无误后打板

 

 

 

3D 图效果:

 

 

实物图如下所示:

 

Arduino UR4 拓展板 SCH.pdf (471.98 KB, 下载次数: 0)

 

2.4.2 软件流程:

 

2.4.3 主要代码片段

  • //////////////////////////////////////////////////////////
  • //函数名称:TimerInterruptHandle
  • //函数功能:中断处理函数
  • //传入参数:无
  • //传出参数:无
  • //特别说明:1s 一次中断周期
  • //////////////////////////////////////////////////////////
  • void TimerInterruptHandle()
  • {
  • bKey1Status =digitalRead(INPUT_KEY1); //获取按键 1 状态
  • bKey2Status =digitalRead(INPUT_KEY2); //获取按键 2 状态
  • bKey3Status =digitalRead(INPUT_KEY3); //获取按键 3 状态
  • if(0 ==u8LedBlinkCnt)
  • {
  • digitalWrite(LED_BUILTIN, HIGH);
  • RgbSetColor(0,0,255);
  • LedMatrix.loadFrame(u32Heart1frame);
  • u8LedBlinkCnt =1;
  • }
  • else
  • {
  • digitalWrite(LED_BUILTIN, LOW);
  • RgbSetColor(0,255,0);
  • LedMatrix.loadFrame(u32Heart2frame);
  • u8LedBlinkCnt =0;
  • }
  • }
  • void loop()
  • {
  • OledDispUpdate(&t_NTP_Time);
  • Aht20Sensor.getEvent(&Ath20HumidityStruct, &Ath20TemperatureStruct); // 用时1s
  • NtpClientTimeUpdate(&t_NTP_Time);
  • }

显示效果:

播放器加载失败: 未检测到Flash Player,请到安装
NTP 时钟

 

2.5 扩展任务2: ST25DV16 NFC 模块的使用

2.5.1 物料准备

Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线 / ST25DV16 NFC 模块

 

2.5.2 软件流程

 

 

2.5.3 代码片段

  • //头文件定义
  • #include "Arduino.h"
  • #include "Wire.h"
  • #include "Adafruit_GFX.h"
  • #include "CBTimer.h"
  • #include "Adafruit_AHTX0.h"
  • #include "Arduino_LED_Matrix.h"
  • #include "WiFiS3.h"
  • #include <SparkFun_ST25DV64KC_Arduino_Library.h>
  • //宏定义
  • //模块全局定义
  • CBTimer Timer;
  • ArduinoLEDMatrix LedMatrix;
  • SFE_ST25DV64KC_NDEF StmTag;
  • //全局变量定义
  • byte u8IsrTicker =0;
  • byte u8LedBlinkCnt =0;
  • byte u8LedMatrixCnt =0;
  • bool bModuleInitStatus =0;
  • //数组定义
  • unsigned long u32Heart1frame[] = //大心图案
  • {
  • 0x3184a484,
  • 0x24042081,
  • 0x100a0040
  • };
  • unsigned long u32Heart2frame[] = //小心图案
  • {
  • 0x0001b024,
  • 0x81100a00,
  • 0x40000000
  • };
  • uint8_t u8TagValue[8] ={0};
  • //////////////////////////////////////////////////////////
  • //函数名称:TimerInterruptHandle
  • //函数功能:中断处理函数
  • //传入参数:无
  • //传出参数:无
  • //特别说明:1s 一次中断周期
  • //////////////////////////////////////////////////////////
  • void TimerInterruptHandle()
  • {
  • if(0 ==u8LedBlinkCnt)
  • {
  • digitalWrite(LED_BUILTIN, HIGH);
  • LedMatrix.loadFrame(u32Heart1frame);
  • u8LedBlinkCnt =1;
  • }
  • else
  • {
  • digitalWrite(LED_BUILTIN, LOW);
  • LedMatrix.loadFrame(u32Heart2frame);
  • u8LedBlinkCnt =0;
  • }
  • }
  • void setup()
  • {
  • //LED 初始化
  • pinMode(LED_BUILTIN, OUTPUT); //LED 输出模式
  • //测试使用
  • pinMode(D2, OUTPUT);
  • //串口初始化
  • Serial.begin(9600); //设置串口波特率
  • //LED 矩阵初始化
  • LedMatrix.begin();
  • //Qwiic 初始化
  • Wire1.begin();
  • bModuleInitStatus =StmTag.begin(Wire1);
  • while(0 ==bModuleInitStatus)
  • {
  • Serial.println("St25dv init fail");
  • delay(100);
  • }
  • Serial.println("St25dv init success");
  • //获取标签 ID 号
  • if(StmTag.getDeviceUID(u8TagValue))
  • {
  • Serial.println("Device UID is:");
  • for(uint8_t i =0; i<8; i++)
  • {
  • if(u8TagValue[i] <0x0A)
  • {
  • Serial.print(F("0"));
  • }
  • Serial.print(u8TagValue[i], HEX);
  • Serial.print(" ");
  • }
  • Serial.println();
  • }
  • else
  • {
  • Serial.println("Could not read devive UID!");
  • }
  • //只在设置时执行
  • SetSt25dvUriInfo(&StmTag);
  • //定时器中断初始化
  • Timer.begin(1000, TimerInterruptHandle); //1000ms 周期运行一遍函数
  • Serial.println("Init Finish");
  • }
  • void loop()
  • {
  • }
  • //////////////////////////////////////////////////////////
  • //函数名称:SetSt25dvUriInfo
  • //函数功能:设置 ST25DV 的 URi 信息
  • //传入参数:
  • //传出参数:无
  • //特别说明:无
  • //////////////////////////////////////////////////////////
  • void SetSt25dvUriInfo(SFE_ST25DV64KC_NDEF *tag)
  • {
  • //清零区域1 256Byte 数据
  • uint8_t s_u8TagMemory[256];
  • memset(s_u8TagMemory, 0 ,256);
  • tag->writeEEPROM(0x0, s_u8TagMemory, 256);
  • //写入类型5 CC 文件信息
  • tag->writeCCFile8Byte();
  • //写入类型5 NDEF URI 信息
  • tag->writeNDEFURI("eeworld.com.cn",SFE_ST25DV_NDEF_URI_ID_CODE_HTTPS_WWW);
  • Serial.println("Uri info write success!");
  • }

 

2.5.4 任务帖详解

【Follow me第二季第2期】6.进阶任务之 ST25DV16 NFC 模块的使用

 

三 .可下载代码

【Follow me第二季第2期】Arduion UR4 作业提交代码

 

四. 心得体会

首先需要感谢EEWORLD 和 Digikey 举办了这么好的一个活动,通过本次活动学会了Arduino 的基本开发,HA 系统的搭建和交互,NTP 实时时钟的完成,这都是之前未研究过的课题,一路上遇到的困难不少,但问题得以解决时也很开心;也通过本次活动复习了画板打板的流程,受益匪浅;略遗憾的是本来计划实现 UR4 和 PICOW 的无线通信最终因时间关系没有完成。Anyway, 正如视频所说,玩板子是件开心的事情,期望后续多多举办类似活动,和大家共同学习成长!

最新回复

 balenaEtcher 这个工具很神奇,搞定就好   详情 回复 发表于 2024-10-9 07:28
点赞 关注
个人签名君应有语,渺万里层云,千山暮雪,知向谁边?
 
 

回复
举报

7032

帖子

0

TA的资源

五彩晶圆(高级)

沙发
 

 balenaEtcher 这个工具很神奇,搞定就好

 
 
 

回复

450

帖子

1

TA的资源

一粒金砂(高级)

板凳
 

进阶必做任务内容补充:

 

2.3 进阶任务(必做):通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)

2.3.1 物料准备&设计思路

装有 HA 的树莓派/Win10 主机/Arduino IDE /Arduino UR4 WIFI 开发板/ TYPE-C 连接线

 

2.3.2 软件流程

 

2.3.3 代码片段

  • #include <WiFiS3.h>
  • #include <ArduinoHA.h>
  • #include "arduino_secrets.h"
  • #include "analogWave.h"
  • //RGB PIN 定义
  • #define LED_RGB_G D5
  • #define LED_RGB_R D6
  • #define LED_RGB_B D9
  • ///////please enter your sensitive data in the Secret tab/arduino_secrets.h
  • char ssid[] = SECRET_SSID; // your network SSID (name)
  • char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
  • int status = WL_IDLE_STATUS; // the WiFi radio's status
  • WiFiClient client;
  • HADevice device(MQTT_CLIENT_ID);
  • HAMqtt mqtt(client, device);
  • HALight light("MyRgbLight", HALight::RGBFeature);
  • byte u8RgbSwitch =0;
  • void setup()
  • {
  • //RGB 初始化
  • pinMode(LED_RGB_R, OUTPUT);
  • pinMode(LED_RGB_G, OUTPUT);
  • pinMode(LED_RGB_B, OUTPUT);
  • analogWrite(LED_RGB_R, 260);
  • analogWrite(LED_RGB_G, 260);
  • analogWrite(LED_RGB_B, 260);
  • //Initialize serial and wait for port to open:
  • Serial.begin(9600);
  • while (!Serial) {
  • ; // wait for serial port to connect. Needed for native USB port only
  • }
  • // check for the WiFi module:
  • if (WiFi.status() == WL_NO_MODULE)
  • {
  • Serial.println("Communication with WiFi module failed!");
  • // don't continue
  • while (true);
  • }
  • // attempt to connect to WiFi network:
  • while (status != WL_CONNECTED) {
  • Serial.print("Attempting to connect to WPA SSID: ");
  • Serial.println(ssid);
  • // Connect to WPA/WPA2 network:
  • status = WiFi.begin(ssid, pass);
  • // wait 10 seconds for connection:
  • delay(10000);
  • }
  • // you're connected now, so print out the data:
  • Serial.print("You're connected to the network");
  • device.setName("Arduino Ur4");
  • light.setName("Bathroom");
  • // handle light states
  • light.onStateCommand(onStateCommand);
  • light.onRGBColorCommand(onRGBColorCommand); // optional
  • Serial.println("\nStart connecting to MQTT server");
  • if (!mqtt.begin(MQTT_SERVER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD))
  • {
  • Serial.print("Connection falied");
  • Serial.print(mqtt.getState());
  • Serial.println("Try again in 5 seconds");
  • delay(5000);
  • }
  • }
  • void loop() {
  • // check the network connection once every 10 seconds:
  • mqtt.loop();
  • }
  • //RGB 相关函数
  • void onStateCommand(bool state, HALight* sender)
  • {
  • Serial.print("State: ");
  • Serial.println(state);
  • if(1 ==state) //RGB 开启
  • {
  • u8RgbSwitch =1;
  • }
  • else //RGB 关闭
  • {
  • u8RgbSwitch =0;
  • }
  • sender->setState(state); // report state back to the Home Assistant
  • }
  • void onBrightnessCommand(uint8_t brightness, HALight* sender)
  • {
  • Serial.print("Brightness: ");
  • Serial.println(brightness);
  • sender->setBrightness(brightness); // report brightness back to the Home Assistant
  • }
  • void onRGBColorCommand(HALight::RGBColor color, HALight* sender)
  • {
  • Serial.print("Red: ");
  • Serial.println(color.red);
  • Serial.print("Green: ");
  • Serial.println(color.green);
  • Serial.print("Blue: ");
  • Serial.println(color.blue);
  • if(1 ==u8RgbSwitch)
  • {
  • analogWrite(LED_RGB_R, (color.red*2));
  • analogWrite(LED_RGB_G, (color.green*2));
  • analogWrite(LED_RGB_B, (color.blue*2));
  • }
  • else
  • {
  • analogWrite(LED_RGB_R, 260);
  • analogWrite(LED_RGB_G, 260);
  • analogWrite(LED_RGB_B, 260);
  • }
  • sender->setRGBColor(color); // report color back to the Home Assistant
  • }

任务帖详解:【Follow me第二季第2期】5.进阶任务之 树莓派 HA+Windows+UR4 控制 RGB

个人签名君应有语,渺万里层云,千山暮雪,知向谁边?
 
 
 

回复

450

帖子

1

TA的资源

一粒金砂(高级)

4
个人签名君应有语,渺万里层云,千山暮雪,知向谁边?
 
 
 

回复

450

帖子

1

TA的资源

一粒金砂(高级)

5
个人签名君应有语,渺万里层云,千山暮雪,知向谁边?
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
有奖直播:当AI遇见仿真,会有什么样的电子行业革新之路?
首场直播:Simcenter AI 赋能电子行业研发创新
直播时间:04月15日14:00-14:50

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网 13

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表