JIYIK CN >

Current Location:Home > Learning > PROGRAM > C++ >

Arduino Square Wave Generator

Author:JIYIK Last Updated:2025/04/16 Views:

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


Arduino Square Wave Generator

A square wave consists of maximum and minimum values, and the transitions between these values ​​are instantaneous. The maximum and minimum values ​​in a square wave have the same duration.

For example, the minimum value of a square wave is 0, the maximum value is 1, and the duration of each value is 1 second. The square wave is shown in the figure below.

A square wave also has a specific frequency, which specifies the number of cycles the square wave has in one second. A frequency value of 60 Hz means that the wave completes 60 cycles in one second.

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

grammar:

digitalWrite(pin_num, value)

The above syntax pin_numsets the given PIN specified by the variable to valuethe PIN specified by the variable HIGH. LOWIn Arduino, LOWthe PIN state represents 0 volts, while HIGHthe PIN state represents either 5 volts or 3.3 volts, depending on the Arduino board.

To generate a square wave we have to set the value of the 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 the digital pin we have to set the mode for the pin like INPUTor OUTPUTbecause every digital pin has a pull-up resistor which will drop the voltage value if we don't set the mode of the digital pin.

We can pinMode()set the mode of a pin using the setMode function. We have to pass the pin as the first argument and the pin mode like OUTPUTor as the second argument inside the setMode function to set the mode of the digital pin.INPUTpinMode()

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

Let us generate a square wave with a frequency of 10 Hz 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);
}

1/fWe calculated the delay time in the above code using the formula, where fis the square wave frequency. We multiplied the time period by 1000 because we used delay()the function, which sets the delay in milliseconds, and to convert the time from seconds to milliseconds, we 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 an oscilloscope we have to connect the positive terminal of the oscilloscope with pin 11 and the negative terminal to the ground of Arduino.

We also divide the time period by 2 because we have two pulses in one cycle and we set the delay value to be LOWequal to half of the total time period after setting the digital pin to , while the other half of the time period will be used to set the delay after setting the digital pin to HIGH. We can see in the code above that we use delay()the function twice.

delay()The data type of the input value of the function is unsigned``longIf we want to add a delay less than 1 millisecond, we must use delayMicroseconds()the function to add delay in microseconds. We can use delayMicroseconds()the function to generate a high frequency square wave.

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 a delay in the Arduino code will also stop the other operations of the Arduino, as the Arduino will not move to the next line of code until the delay time is over, but the PWM and interrupts will continue to work.

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

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

We can also use analogWrite()the 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, 1000 Hz.

We have to pass the PIN as the first argument and the duty cycle of the waveform should be 127 as analogWrite()the second argument in the function to generate a square wave. We have to pass 127 as the second argument because it sets the duty cycle to half, which is required to generate a square wave.

Click this link to get the frequency details of the PWM pins and analogWrite()more information on the functions.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Stopping a loop in Arduino

Publish Date:2025/04/16 Views:150 Category:C++

This tutorial will discuss the methods to stop a loop in Arduino. There are two types of loops in Arduino: one is the void loop() provided by default and the other is created by the user in it. The loop created by the user can be ended easi

Arduino prints to console

Publish Date:2025/04/16 Views:68 Category:C++

This tutorial will discuss printing text or variables on the console using the Arduino IDE's serial monitor. Arduino using the serial monitor to print to the console The Arduino IDE has a console at the bottom, but we cannot print anything

Arduino Array Length

Publish Date:2025/04/16 Views:181 Category:C++

Arrays are fundamental data structures in programming, and in Arduino, they play a key role when storing and manipulating data. Often, you'll find yourself needing to know the size or length of an array, especially when working on complex p

Arduino 2D Array

Publish Date:2025/04/16 Views:108 Category:C++

In this tutorial, we will discuss about 2D arrays in Arduino. We will discuss how to initialize a 2D array and use it to store data. 2D Array Initialization in Arduino Two-dimensional array initialization is very similar to one-dimensional

Printing Character Array in Arduino

Publish Date:2025/04/16 Views:59 Category:C++

This tutorial will discuss printing character array using loop in Arduino. Serial.println() Define int and print character arrays in Arduino In Arduino, if we int initialize an array using keyword, we have to use a loop to print its element

Arduino mills() function

Publish Date:2025/04/16 Views:156 Category:C++

This tutorial will discuss millis() the use of functions in different applications in Arduino. This tutorial will also discuss some examples to better understand millis() the functions. millis() Checking the elapsed time in Arduino using th

Arduino 方波发生器

Publish Date:2024/03/13 Views:157 Category:C++

可以使用 Arduino 中的 digitalWrite() 函数生成方波。

在C中将整数转换为字符

Publish Date:2024/01/03 Views:135 Category:C语言

本教程介绍了在C中将整数转换为字符的不同方法。在C编程语言中,将整数转换为字符在各种情况下都很重要。在C中,字符是以ASCII值表示的,因此转换过程相对简单。

如何在 C++ 中实现毫秒级的睡眠

Publish Date:2024/01/02 Views:238 Category:C++

本文介绍了在 C++ 中使用不同方法暂停程序执行,实现睡眠的方法。本文介绍了在 C++ 中睡眠毫秒的方法。使用 std::this_thread::sleep_for 方法在 C++ 中睡眠

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial