This article was automatically translated from Japanese using AI. The Japanese version is the authoritative version.
Introduction
There are times when you want to output a continuous pulse wave with an Arduino: blinking an LED, generating a sound, using it as a timer, and so on.
It comes up often and seems simple at first glance, but once you dig into it you find it is surprisingly deep.
In this post, I introduce several ways to output a continuous pulse wave with an Arduino.
Changing the timing with delay
The simplest and easiest approach is to switch the output ON and OFF using the delay function.
//pinはピン番号
void loop(){
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
delay(1000);
}
In the program above, the output is toggled between HIGH and LOW.
Since delay is specified in milliseconds, delay(1000) waits for one second.
In other words, this program turns the output on and off at 1 Hz.
However, using the delay() function to set the frequency has a number of drawbacks.
With this approach, changing the output duration—for example, outputting a 60 Hz signal for five seconds—requires a for loop, which is inconvenient.
That said, because it is so simple, it is a good choice when you just want to try something out.
Using tone()
The tone() function is commonly used to generate buzzer sounds and the like.
Official Arduino reference
This function lets you specify the frequency and the duration.
So, unlike the delay approach, you can specify the frequency directly without calculating it.
//pinはピン番号
void loop() {
tone(pin,60);
}
You can write it as tone(pin, frequency) or tone(pin, frequency, duration).
The duration is given in milliseconds, so it is written the same way as delay.
The limitation of this function is that you cannot specify frequencies of 31 Hz or below.
In other words, you cannot generate an output such as 1 Hz.
For frequencies above 31 Hz, such as audio tones, it makes specifying the frequency extremely easy, and the code is far shorter and more accurate than using the delay function.
Using PWM output and changing its frequency
Using PWM output offers the greatest flexibility—and it is also why this topic gets so deep.
PWM stands for pulse width modulation.
For details, see Wikipedia.
The term alone does not tell you much, but simply put, PWM is a way of modulating an output by changing the duty ratio.
The official Arduino explanation is here.
Normally, you would set the brightness of an LED by changing the current. But if the current is fixed and you still want to change the brightness, you can blink the LED at a very high frequency (30–60 Hz is said to be the point where people can no longer perceive the flicker).
Normally the ON and OFF periods are 1:1 (a duty ratio of 50%), but what happens if you make it 4:1 (80% duty) or 1:4 (20% duty)?
The former will look bright, and the latter will look dim.
Modulating the output by changing the pulse width in this way is what PWM output is all about.
On the Arduino you can not only generate this output but also change the frequency of the PWM output.
The idea here is that by tweaking the register settings—essentially the low-level guts of the Arduino—you can change the PWM output frequency.
Incidentally, the default output frequency is 490 Hz, or 980 Hz on some pins.
I will explain how to do this in detail in a future post.
Searching for “PWM Arduino frequency change” turns up plenty of explanations.
After reading them about four times, it starts to make sense.
Basically, I suggest looking through these methods to find the one that best fits your own goal.
