Arduino接口-week2-Lesson3

This series of articles are the study notes of "Interfacing with the Arduino", by Prof. Harris, Department of Computer Science, University of California, Irvine. This article is the notes of week 2, lessen 3.

3. Lesson 3

3.1 Lecture 3.1 -DemoFade Example

In this experiment we are going to see pulse with modulation in action. We're going to use the the example sketch called fade. So if you're looking in your Arduino IDE., you look under examples, the subcategory basics and then there's one sketch called fade. So pull that up.

3.2 Lecture 3.2 -Making Sounds

So there's a common function that I do in the library called tone, which also generates a square wave. A similar to analogWrite but different. 

3.2.2 tone()

  • tone() can generate a square wave with an arbitrary frequency
  • analogWrite() has a fixed frequency (490 Hz)
  • duty cycle is fixed at 50%
  • can be used to drive a speaker or buzzer
Tone, this function tone can generate a square wave with an arbitrary frequency. Now this is different than analogWrite because analogWrite has a fixed frequency. If you remember analogWrite generates a signal, a square wave that's 490 Hz, but tone can change the frequency and so it can be used for different purposes. Now tone cannot change a duty cycle, Right? With analogWrite you specify the duty cycle, tone has a fixed 50% duty cycle. But tone can change the frequency.
  • two or three arguments
    • Pin number
    • Frequency, in Hz
    • Duration in ms (optional)
Now, tone takes either two or three arguments. The first argument is a pin number, the pin that you wanna generate the tone on, that you wanna send this square wave to. The second argument is the frequency in hertz that you want for the signal. So whatever frequency you want,
you just send that as the second argument. And then the third argument, which is optional, is the duration in milliseconds. So how long do you want that tone to play? That could be your third argument. If you don't have a third argument, it'll just play, until you call another function called no tone, which will kill it.

3.2.2 Square Wave vs. Sine Wave

(1) Square waves sound bad: many high-frequency components
we're sending a square wave to make a sound. So, tone generates a square wave, right? It has to make a square wave because this is a digital component and it's only zeroes and ones. So, the wave has to be a square wave, but a square wave generally sounds bad. So, when you take a square wave, send it to a speaker, it's gonna sound bad. Reason why is because the square wave basically has sharp edges in it, okay? So you've got zero and then suddenly it's one. And instantaneously it's going from zero to one. That's extremely, that's infinite frequency if we were to do that. 
Where a sine wave, if you would hear that, a pure sine wave is one frequency. It's a pure tone.

Square wave                                     Sine wave

(2) Square wave is the best we can do with digital outputs
It might be the same frequency, but it sounds like a buzz. It has annoying high-frequency components. But that is the best we can do, because our Arduino is digital and it's just going to generate square waves. So with digital output square waves is the best that we can do. Although sine wave would sound much nicer. And if you really want to do this. If you really want to make music with an Arduino, and make it sound pleasing you would use a shield.

3.2.3Piezoelement as a buzzer

Now for the speaker we're gonna use a piezo element as a buzzer. These are very simple sound making devices, they look like that, you should have one like this in your kit. And here it's symbol.It's got two terminals, right? It's got two which are connected to signal and ground. So one side's connected to ground, the other side is connected to the signal, the say square wave, for instance, right? And it produces a click, essentially a click when the rising edge is applied. Or a sound, some kind of sound, which we'll call a click when the rising edge is applied. So if you have it clicking at a particular frequency, you can change the tone that's generated.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ARDUINO mq-2烟雾传感器是一种基于MQ-2传感器的烟雾传感器模块。它具有高灵敏度,可以检测可燃气体和烟雾等气体。该传感器模块提供了两种输出方式。一种是数字量输出,通过设定浓度阈值,当检测到环境气体浓度超过阈值时,通过数字引脚输出低电平。另一种是模拟量输出,浓度越高,模块的AO引脚输出的电压值越高。需要注意的是,传感器通电后需要预热约20秒,测量的数据才会稳定。传感器工作时需要内部电热丝加热,所以发热是正常现象。传感器的模拟信号经过LM393高精度比较器进行数字化处理,同时配备一个小电位器,可以调整传感器的灵敏度和检测到的气体浓度。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [Arduino提高篇11—烟雾传感器MQ-2](https://blog.csdn.net/TonyIOT/article/details/103380128)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [arduino 读取模拟电压_MQ2气体/烟雾传感器如何工作及其与Arduino接口](https://blog.csdn.net/gaoxiang19820514/article/details/125972967)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值