Skip to Content
《Arduino 烹饪书》第三版
book

《Arduino 烹饪书》第三版

by Michael Margolis, Brian Jepson, Nicholas Robert Weldin
May 2025
Beginner to intermediate
798 pages
10h 10m
Chinese
O'Reilly Media, Inc.
Content preview from 《Arduino 烹饪书》第三版

简单的数字和模拟输入

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

5.0 引言

Arduino 感知数字和模拟输入的能力使其能够对您和您周围的世界做出响应。本章将介绍用于监控和响应这些输入的技术:检测按下开关的时间、读取数字键盘的输入以及读取一定范围的电压值。

本章介绍 Arduino 可以感应数字模拟输入的引脚。数字输入引脚可检测引脚上是否存在电压。模拟输入引脚测量引脚上的电压范围。

图 5-1显示了 Arduino Uno 上的引脚排列。许多 Arduino 兼容板(包括 Adafruit Metro 系列和 SparkFun)都采用了这种引脚排列方式。请参阅官方电路板列表,其中链接了每种电路板的连接信息。如果您的电路板不在列表中,请查看电路板供应商的网站,了解连接信息。

Arduino 检测数字输入的函数是digitalRead ,它会告诉您的草图,某个引脚上的电压是HIGH 还是LOW 。对于 Uno 等电路板,HIGH 为 3 至 5 伏(基于 ARM 的电路板和任何其他 3.3V 电路板为 2 至 3.3 伏),LOW 为 0 伏。Arduino 配置 一个引脚用于读取输入的函数是pinMode(pin, INPUT).

在采用 Uno 式引脚布局的电路板上(包括 Arduino Leonardo、Adafruit Metro 的几款电路板和 SparkFun RedBoard),共有 14 个数字引脚(编号为 0 至 13),如图 5-1 顶部所示。在 Uno 和 100% 兼容板(通常是基于 ATmega328 的板)上,引脚 0 和 1(标记为 RX 和 TX)用于 USB 串行连接,应避免用于其他用途。有关串行连接的详细信息,请参见第 4 章

Arduino Uno 电路板上的数字和模拟引脚

Arduino 具有可用于引用许多引脚的逻辑名称。表 5-1中的常量可用于所有需要引脚编号的函数。您很可能会遇到使用实际引脚编号的示例代码。但鉴于 Arduino 和 Arduino 兼容板的广泛多样性,您应避免使用数字引脚编号,而应使用这些常量。例如,在 Arduino Uno 上,A0 是第 14 针,但在 MKR WiFi 1010 上是第 15 针,在 Arduino Mega 上是第 54 针。

用于 Uno 风格布局的引脚常量
恒定 针脚 恒定 针脚

A0

模拟输入 0 LED_BUILTIN 机载 LED

A1

模拟输入 1 SDA I2C 数据

A2

模拟输入 SCL I2C 时钟

A3

模拟输入 SS SPI 选择

A4

模拟输入 MOSI SPI 输入

A5

模拟输入 MISO SPI 输出
SCK SPI 时钟
提示

如果需要更多的数字引脚,可以将模拟引脚用作数字引脚(这样做时,可以用符号名称来表示,例如pinMode(A0, INPUT); )。

Mega 和 Due 等电路板有更多的数字和模拟引脚。数字引脚 0 至 13 和模拟引脚 0 至 5 的位置与标准电路板相同,因此可以安装为标准电路板设计的硬件屏蔽。与标准电路板一样,您可以将模拟引脚用作数字引脚,但对于 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

技术主管的进阶之路 (Chinese Edition)

技术主管的进阶之路 (Chinese Edition)

Anemari Fiser

Publisher Resources

ISBN: 9798341659582