Arduino Square Wave Generator

This tutorial will discuss generating a square wave using the digitalWrite() function in Arduino.

Arduino Square Wave Generator

A square wave consists of a maximum and a minimum value, and the transition between these values is instantaneous. The maximum and minimum values duration is the same in a square wave.

For example, a square wave can have a minimum value of 0 and a maximum value of 1 with a time duration of 1 second for each value. A square wave is shown in the below diagram.

Arduino Square Wave

A square wave also has a specific frequency that specifies the number of cycles a square wave will have in a one-second duration. A frequency value of 60 Hz means that the wave is completing 60 cycles in one second.

We can generate a square wave in Arduino using the digitalWrite() function.

Syntax:

digitalWrite(pin_num, value)

The above syntax sets the given PIN specified by the pin_num variable to HIGH or LOW specified by the value variable. In Arduino, the LOW pin status means 0 volts, and the HIGH pin status means 5 volts or 3.3 volts, depending on the Arduino board.

To generate a square wave, we have to set the value of a digital pin to LOW, and after some delay, we have to set the value of the digital pin to HIGH. Before setting the value of a digital pin, we have to set the mode for the pin-like INPUT or OUTPUT because there is a pull-up resistor present with each digital pin which will lower the voltage value if we don’t set the mode of a digital pin.

We can use the pinMode() function to set the mode for a pin. We have to pass the PIN as the first argument and pin mode like OUTPUT or INPUT as the second argument inside the pinMode() function to set the mode of a digital pin.

We also have to add a delay after setting the value of a digital pin, and we can use the delay() or delayMicroseconds() function to add a delay in the Arduino code. The delay() function will set the given delay in milliseconds, and the delayMicroseconds() function will set the given delay in microseconds.

Let’s generate a square wave with a 10 Hz frequency in Arduino.

Code:

int f_hz = 10;

double delay_time = 1000 / (f_hz * 2);

void setup()
{
 pinMode(11, OUTPUT);
}
void loop()
{
 digitalWrite(11, LOW);
 delay(delay_time);

 digitalWrite(11, HIGH);
 delay(delay_time);
}

We calculated the delay time in the above code using the 1/f formula, where f is the square wave frequency. We multiplied the time period by 1000 because we used the delay() function, which sets the delay in milliseconds and to convert the time from seconds to milliseconds, we have multiplied the time period by 1000.

To check the output waveform and frequency, we have to use an oscilloscope, or we can use an LED with pin 11, which will blink if the frequency is low. To use the oscilloscope, we have to connect the positive terminal of the oscilloscope with pin 11 and the negative terminal to the ground of the Arduino.

We have also divided the time period with 2 because we have two pulses in a single cycle, and we will set the delay value equal to half of the total time period after setting the digital pin to LOW and the other half time period will be used to set the delay after the setting the digital pin to HIGH. We can see in the above code we have used the delay() function twice.

The data type of the input value of the delay() function is unsigned long, and if we want to add a delay that is less than 1 millisecond, we have to use the delayMicroseconds() function which can add delay in microseconds. We can generate high-frequency square waves using the delayMicroseconds() function.

Note that the delay() and delayMicroseconds() functions do not support floating-point numbers, so we have to set a frequency value that should not generate the time period as a floating-point number. Adding delay in the Arduino code will also halt the other operation of Arduino because Arduino will not move to the next line of code until the delay time is finished, but PWM and interrupts will continue to work.

If we want to generate the square wave and perform some other task simultaneously, we can use the millis() function, which will return the time in milliseconds since the program started running. We can use the millis() function and the if statement to check the elapsed time, and if the elapsed time is equal to or greater than the time period, we will change the status of the digital pin.

Check this site for more details about the millis() function, and this site for delayMicroseconds() function. Click this link for details about the delay() function, and this link for the digitalWrite() function.

We can also use the analogWrite() function to create a square wave, but we cannot define its frequency. The frequency is already defined, mostly 499 Hz, or in the case of some pins, the frequency is 1000 Hz.

We must pass the PIN as the first argument and the wave’s duty cycle, which should be 127, as the second argument inside the analogWrite() function to generate a square wave. We have to pass 127 as the second argument because it will set the duty cycle to half, which is necessary to generate a square wave.

Click this link for frequency details of PWM pins and for more information about the analogWrite() function.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.