Tag: 自由に変える

  • How to Generate Continuous Pulse Waves with Arduino

    How to Generate Continuous Pulse Waves with Arduino

    Introduction

    There are times when you want to output a continuous pulse wave with an Arduino: blinking an LED, producing a sound, using it as a timer, and so on.

    It comes up often and seems simple at first, but the more you look into it, the deeper the topic gets.

    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 alternates between HIGH and LOW.
    Since delay is specified in milliseconds, delay(1000) waits for one second.

    In other words, it is a program that turns on and off at 1 Hz.

    However, using the delay() function to set a frequency has many drawbacks.
    With this approach, changing the output duration—say, outputting a 60 Hz signal for 5 seconds—requires a for loop, which is inconvenient.

    That said, because it is so simple, I recommend it when you just want to try something out quickly.

    Using tone()

    The tone() function is commonly used to generate buzzer sounds.
    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 having to calculate 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 problem with this function is that it cannot produce frequencies of 31 Hz or below.
    In other words, you cannot generate an output at, say, 1 Hz.

    For frequencies above 31 Hz, such as audio tones, it makes setting the frequency very 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 most flexibility—and it is also the reason this topic gets so deep.

    PWM stands for pulse width modulation.
    For details, see Wikipedia.

    The term alone does not tell you much, but in simple terms, 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 when the current is fixed and you still want to change the brightness, you blink the LED at a very high frequency (the flicker fusion threshold for humans is said to be around 30–60 Hz).

    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 looks bright, and the latter looks dim.

    Modulating the output by changing the pulse width in this way is what PWM output is.

    On the Arduino you can not only produce this output but also change the PWM frequency.
    The idea behind this method is that by changing register settings—that is, the underlying parts of the Arduino—you can change the PWM output frequency.
    By default, the 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 change frequency” turns up plenty of explanations.

    After reading through them about four times, it starts to make sense.

    Basically, why not try these approaches and find the one that fits your own purpose?