使用 Arduino 串行绘图仪可视化实时数据

news2024/9/24 9:24:58

使用 Arduino 串行绘图仪可视化实时数据

Using The Arduino Serial Plotter To Visualize Real Time Data
参考:

  1. Arduino Docs: Using the Serial Plotter Tool (IDE v2)
    Arduino Docs:使用串行绘图仪工具 (IDE v2)

  2. The ADC-10-F103C serial plotter source code has been moved to its own repository. A click-to-run EXE can be downloaded from its releases page.
    ADC-10-F103C 串行绘图仪源代码已移至其自己的存储库中。可以从其发布页面下载一键运行的 EXE。
    Using The Arduino Serial Plotter To Visualize Real Time Data - Woolsey Workshop

  3. Arduino 串口绘图库

  4. python 实现arduino串口数据绘图

Introduction

本教程将教您如何使用 Arduino 串行绘图仪显示波形和实时数据。 希望您对电子学和编程有基本的了解,并对 Arduino 平台有一定的熟悉程度。

The resources created for this tutorial are available on GitHub for your reference.

What Is Needed

  • Arduino IDE
  • Arduino Uno WiFi R2

Displaying Generated Waveforms

This section will describe how to generate and display waveforms using the Serial Plotter.

Open the Arduino IDE and create a new sketch named FunctionGenerator with the code shown below.

本节将介绍如何使用串行绘图仪生成和显示波形。 打开 Arduino IDE,创建一个名为 FunctionGenerator 的新草图,代码如下所示。

void setup() {
   Serial.begin(9600);  // initialize serial bus (Serial Plotter)
}

void loop() {
   // Uncomment one of the wave types below to display in Serial Plotter.
   // Serial Plotter window may need to be closed and reopened between runs
   // to remove old plot data.
   // plotCombinedWaves();
   // plotSawtoothWave();
   plotSineWave();
   // plotSquareWave();
   // plotTriangleWave();
}

void plotCombinedWaves() {
   for (float i = 0.0; i <= 2 * PI; i += 0.1) {
      Serial.print(cos(i));  Serial.print(" ");  // cosine wave
      Serial.print(sin(i));  Serial.print(" ");  // sine wave
      Serial.println(i <= PI ? -1.5 : 1.5);      // square wave
   }
}

void plotSawtoothWave() {
   for (int i = 0; i <= 100; i++) {
      Serial.println(i);
   }
}

void plotSineWave() {
   for (float i = 0.0; i <= 2 * PI; i += 0.1) {
      Serial.println(sin(i));
   }
}

void plotSquareWave() {
   for (int i = 0; i <= 100; i++) {
      Serial.println(0);
   }
   for (int i = 0; i <= 100; i++) {
      Serial.println(100);
   }
}

void plotTriangleWave() {
   for (int i = 0; i <= 100; i++) {
      Serial.println(i);
   }
   for (int i = 100; i >= 0; i--) {
      Serial.println(i);
   }
}

The code should be fairly straightforward, but if there is something that needs further explanation, please let me know in the comment section and I will try to answer your question.

Open the Serial Plotter window (Tools > Serial Plotter) and make sure the baud rate is set to 9600. This is where the generated waveforms will be displayed.

Upload (Sketch > Upload) the sketch to the board and you should see a sine wave being displayed in the Serial Plotter window as shown below.

代码应该相当简单明了, 打开串行绘图仪窗口(工具 > 串行绘图仪),确保波特率设置为 9600。 将草图上传(草图 > 上传,你应该会看到正弦波显示在串行绘图仪窗口中,如下图所示。

Arduino Serial Plotter Sine WaveSine Wave In Serial Plotter Window

If we take a look at the sketch, we see that the plotSineWave() function is being called from within the loop() function. Display one of the other waveforms by commenting out the plotSineWave() function, uncommenting one of the other waveforms, and rerunning your sketch. Note, if you see data left over from the previous plot, you will need to close and re-open the Serial Plotter window to clear the screen.

Reviewing the plotSineWave() function, we see the data is simply being sent using the Serial.println() function that you are probably already familiar with for printing text and data to the Serial Monitor. As long as only numbers and whitespace are sent over the serial port in a particular format, the Serial Plotter can interpret that data to graph it visually. This format requires that a new line character be sent in between x-axis tick marks and that y-axis values are separated by whitespace such as a space or tab.

For instance, we would use the following general format to plot single variable data

如果我们看一下草图,就会发现 plotSineWave() 函数是在loop()函数中调用的。 注释掉 plotSineWave() 函数,取消注释其他波形之一,然后重新运行草图,即可显示其他图形。
请注意,如果您看到上一次绘制的残留数据,则需要关闭并重新打开串行绘图仪窗口以清除屏幕。
查看 plotSineWave() 函数,我们会发现数据只是使用 Serial.println() 函数发送的,您可能已经熟悉了将文本和数据打印到串行监视器的方法。 只要通过串行端口以特定格式发送的数据中只有数字和空白,串行绘图仪就能解释这些数据并绘制出可视化图形。 这种格式要求在 x 轴刻度线之间发送一个新行字符,y 轴值之间用空格或制表符等空白分隔。 例如,我们可以使用以下一般格式绘制单变量数据

Serial.println(data);

and the following for multivariable data.
以及以下针对多变量数据。

Serial.print(dataA);  
Serial.print(“ “);  
Serial.println(dataB);

Notice that only the last statement, println(), actually sends a new line. You can add more variables as long as you add the appropriate whitespace in between the print() statements for each of the variables. You may even choose to use a tab, print(“\t”), in between your variables to make the data easier to read while viewing the data directly within the Serial Monitor window.

Whereas all other wave functions display only a single variable plot, the plotCombinedWaves() function displays three different waveforms simultaneously. View this function for a better understanding of how to plot a multivariable graph. The plot for this is shown below.

请注意,只有最后一条 println() 语句实际上发送了一行新的内容。 只要在每个变量的print()语句之间添加适当的空白,就可以添加更多变量。 您甚至可以选择在变量之间使用制表符 print("\t"),以便在串行监视器窗口中直接查看数据时更容易读取数据。 其他所有波形函数都只显示一个变量图,而 plotCombinedWaves() 函数则同时显示三个不同的波形。 查看此函数可更好地了解如何绘制多变量图。 相关图表如下所示。

Arduino Serial Plotter Combined WaveformsCombined Waveforms In Serial Plotter Window

Displaying Real Time Data

Until now, we have only plotted data for waveforms we generated ourselves. Now let’s use the on-board IMU of the Arduino Uno WiFi R2 to plot actual real time motion data.

Create a new sketch named LSM6DS3_SerialPlotter with the code shown below. I won’t go into the details of how to access the IMU data, but if you are interested, please see our Accessing The IMU On The New Arduino Uno WiFi Rev2 tutorial for more information.

到目前为止,我们只绘制了自己生成的波形数据。 现在,让我们使用 Arduino Uno WiFi R2 的板载 IMU 绘制实际的实时运动数据。 用下图所示代码创建一个名为 LSM6DS3_SerialPlotter 的新草图。 我不会详细介绍如何访问 IMU 数据,但如果您感兴趣,请参阅我们的 "访问新 Arduino Uno WiFi Rev2 上的 IMU "教程,了解更多信息。

// Includes
#include <SparkFunLSM6DS3.h>
#include <SPI.h>

// Defines
#define PLOT_INTERVAL 100

// Global Variables
unsigned long previousTime = 0;

// Constructors
LSM6DS3 imu(SPI_MODE, SPIIMU_SS);  // set SPI mode and chip select for on-board IMU

void setup() {
   Serial.begin(9600);  // initialize serial bus (Serial Plotter)
   delay(1000);         // wait one second for IMU reset to complete
   imu.begin();         // initialize IMU
}

void loop() {
   unsigned long currentTime = millis();
   if (currentTime - previousTime >= PLOT_INTERVAL) {
      Serial.print(imu.readFloatAccelX());  Serial.print(" ");  // x-axis
      Serial.print(imu.readFloatAccelY());  Serial.print(" ");  // y-axis
      Serial.println(imu.readFloatAccelZ());                    // z-axis
      previousTime = currentTime;
   }
}

Within the loop() function, we are accessing and displaying all three axes of the IMU motion data every 100 ms. The plot of this data is shown below.

Arduino Serial Plotter IMUIMU Sensor Data In Serial Plotter Window

The blue squiggles at the beginning of the graph represent moving the board in the x direction. The red and green ones represent the y and z directions respectively. The mass of movement in all directions at the end of the graph represents moving and rotating the board in all kinds of directions.

图形开头的蓝色斜线代表棋盘在 x 方向上的移动。 红色和绿色方块分别代表 y 和 z 方向。 图形末端各个方向的运动质量表示棋盘在各个方向上的移动和旋转。

Summary

In this tutorial, we learned how to generate and display various types of waveforms using the Arduino IDE’s Serial Plotter. We also learned how to graph real time motion data from the Arduino Uno WiFi R2’s on-board IMU.

The Serial Plotter may be a simple tool, but it can be a powerful one allowing you to visualize and debug your program’s data and sensor measurements. As they say, “a picture is worth a thousand words”.

The final source code used for this tutorial is available on GitHub.

Thank you for joining me in this journey and I hope you enjoyed the experience. Please feel free to share your thoughts in the comments section below.

在本教程中,我们学习了如何使用 Arduino IDE 的串行绘图仪生成和显示各种类型的波形。 串行绘图仪可能只是一个简单的工具,但它的功能却十分强大,可以让您对程序数据和传感器测量结果进行可视化调试。 俗话说,“一图胜千言”。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1993411.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

8.1 迭代器的概念与使用:走进 Python 的迭代世界

欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;欢迎订阅相关专栏&#xff1a; 工&#x1f497;重&#x1f497;hao&#x1f497;&#xff1a;野老杂谈 ⭐️ 全网最全IT互联网公司面试宝典&#xff1a;收集整理全网各大IT互联网公司技术、项目、HR面试真题.…

jwt伪造身份组组组合拳艰难通关

前言 现在的攻防演练不再像以往那样一个漏洞直捣黄龙&#xff0c;而是需要各种组合拳才能信手沾来&#xff0c;但是有时候使尽浑身解数也不能诚心如意。 前期信息收集 首先是拿到靶标的清单 访问系统的界面&#xff0c;没有什么能利用的功能点 首先进行目录扫描&#xff0c;…

IO/作业/2024/8/8

1第一题 #include <sys/stat.h> #include <fcntl.h> #include <pthread.h> #include <semaphore.h> #include <wait.h> #include <signal.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/socket.h>…

如何禁用外来U盘,保护企业电脑不被“插”一刀?

在数字时代&#xff0c;U盘就像是随身携带的小型保险箱&#xff0c;但它也可能变成一个潜伏的危险品。想象一下&#xff0c;有一天你的同事无意间把一个带有病毒的U盘插进了你的电脑&#xff0c;结果你辛辛苦苦做出来的项目文件就那么瞬间被格式化了…… 为了避免这种情况的发…

CUTLASS 2.x CUTLASS 3.x Intro 学习笔记

CUTLASS GEMM模板中有大量可以调节和设置的模板参数&#xff0c;这些参数的设置会高度影响Kernel性能。这个分享将为大家介绍从2.x到3.x&#xff0c;CUTLASS kernel实现的变化&#xff0c;这些参数的原理和选择的最佳实践。Slides来自BiliBili NVIDIA英伟达频道 上传的《Tensor…

python-热杆上的蚂蚁(赛氪OJ)

[题目描述] 有一个不断升温的杆子&#xff0c;上面有若干个蚂蚁&#xff0c;蚂蚁们需要尽快爬出这个杆子&#xff0c;否则就会因为高温而被烧死。 这里假设每只蚂蚁行走的最大速度是 1cm/s 。 当一只蚂蚁走到杆的尽头时&#xff0c;就会立即从秆上掉落&#xff0c;从而逃离热杆…

两种企业总体业务流程架构模式的比较分析

在之前的关于企业业务流程规划的系列文章中&#xff0c;我们分别对企业业务流程规划的价值、原则&#xff0c;以及如何应用企业的业务流程架构等做了充分的阐述&#xff0c;今天我们将对两种常见的企业总体业务流程架构模式进行比较分析。 我们在辅导企业做业务流程规划和总体…

Python打开JSON/CSV文件的正确方式

前言 我们在使用python的过程中&#xff0c;经常需要它完成一些数据处理的工作&#xff0c;其中尤以json/csv文件为常见。今天&#xff0c;博主针对UnicodeDecodeError异常进行试验&#xff0c;因为这个是新手最容易犯错的地方。 Q&#xff1a;如何应对 UnicodeDecodeError 读…

(el-Time-Picker)操作(不使用 ts):Element-plus 中 TimePicker 组件的使用及输出想要时间格式需求的解决过程

Ⅰ、Element-plus 提供的 TimePicker 时间选择器组件与想要目标情况的对比&#xff1a; 1、Element-plus 提供 TimePicker 组件情况&#xff1a; 其一、Element-ui 自提供的 TimePicker 代码情况为(示例的代码)&#xff1a; // Element-plus 提供的组件代码: <template>…

七、1 ADC模数转换器介绍+有关知识点

目录 1、介绍 &#xff08;1&#xff09;ADC&#xff0c;模拟信号转换为数字信号 &#xff08;2&#xff09;DAC和PWM&#xff0c;数字信号转换为模拟信号 &#xff08;3&#xff09;ADC的两个关键参数 &#xff08;4&#xff09; &#xff08;5&#xff09; &#xff08…

深度学习代码运行RuntimeError:No such operator torchvision::nms解决方案

RuntimeError: No such operator torchvision::nms解决方案 跑代码的时候碰到了"RuntimeError: No such operator torchvision::nms"&#xff0c;找到的资料显示大多是"torch"和"torchvision"版本不匹配&#xff0c;让二者版本一致即可解决。但我…

抱抱脸自动下载模型地址

HuggingFace模型自动下载找保存地址 问题&#xff1a;OSError: Incorrect path_or_model_id: THUDM/cogvlm2-llama3-chat-19B/model.safetensors.index.json. Please provide either the path to a local folder or the repo_id of a model on the Hub. 解决&#xff1a;MODEL_…

多叉树的深度优先遍历(以电话号码的字母组合为例)

在我们的座机上&#xff0c;都有这种数字与字母对应的按键。 以此为例&#xff0c;讲解多叉树的深度优先遍历 问题 给定一个仅包含数字 2-9 的字符串&#xff0c;返回所有它能表示的字母组合。答案可以按 任意顺序 返回。 给出数字到字母的映射如下&#xff08;与电话按键相同…

奥运新项目带来新增长,小众运动攀岩相关商品成交额同比增长 160%

巴黎奥运会临近收官&#xff0c;中国健儿在乒乓球、网球、跳水、游泳等众多项目中表现出色&#xff0c;不仅吸引了全球目光&#xff0c;更在国内掀起了一股强劲的“奥运热”。抖音电商数据显示&#xff0c;7月20日-8月2日“热力先锋季”主题活动期间&#xff0c;抖音电商体育类…

“名字说我俩挺配的”:解锁姓名背后的神秘共鸣,带你玩转名字魔法!

引言&#xff1a;从姓名中挖掘灵魂共鸣 大家好&#xff0c;欢迎来到“姓名共鸣者”的世界&#xff01;不管你是想知道自己的名字和暗恋对象的名字有多配&#xff0c;还是好奇自己和某个历史人物之间的缘分&#xff0c;或者只是想找点乐子&#xff0c;“姓名共鸣者”都能满足你…

[qt] 数据库基本概念

一 数据和数据库 1.1 数据 数据可以是被计算机接受处理和处理的符号。可以有数字、文字、表格、图形、图像和声音等 1.2 数据库 顾名思义就是存放数据的仓库 1.2.1 特点 数据按照数据模型组织&#xff0c;是高度结构化的&#xff0c;可供多个用户共享并且具有一定的安全性…

视频怎么转换成mp3音频?视频转mp3音频的几个批量方法

视频怎么转换成mp3音频&#xff1f;在现代的工作场景中&#xff0c;多媒体文件的处理已经成为许多工作任务中不可或缺的一部分。特别是在处理视频和音频文件时&#xff0c;有时候需要将视频文件转换成MP3音频格式。这一操作不仅仅是简单的格式转换&#xff0c;更是为了适应不同…

微信小程序实现Canvas画板

这个小demo适用于 快递实名签收等业务逻辑 源码如下&#xff1a; js文件&#xff1a; Page({data: {ctx: "", // 保存 canvas 上下文pen: 5, // 画笔默认的宽度color: "#000", // 画笔默认的颜色},startX: 0, // 保存 X 坐标startY: 0, // 保存 Y 坐标o…

人在职场,格局越小,破事越多

人在职场&#xff0c;面对同样的琐碎、倾轧&#xff0c;有人处理得游刃有余&#xff0c;有人总是战战兢兢&#xff0c;表面上看起来&#xff0c;是能力水平的差别&#xff0c;归根究底&#xff0c;是格局的不同。 格局越小的人&#xff0c;眼里的破事越多&#xff1b;格局越大…

【限流与Sentinel超详细分析】

Sentinel 随着微服务的流行&#xff0c;服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式服务架构的流量控制组件&#xff0c;主要以流量为切入点&#xff0c;从流量控制、熔断降级、系统自适应保护等多个维度来保障微服务的稳定性。 1 Sentinel 基本概念 资源…