How To Generate Square Wave Using Arduino

Any AVR microcontroller based board which follows the standard Arduino schematic and is flashed with the Arduino bootloader can be called an Arduino board. The Arduino is refered to as open source hardware and the Arduino IDE is also open source and anybody can contribute their libraries to the Arduino. All arduino boards should be compatible with the Arduino IDE which can be used to program the Arduino boards.Since the Arduino board can act as a stand-alone system it should have capabilities to take inputs process the input and then generate a corresponding output. It is through these inputs and outputs that the Arduino as a system can communicate with the environment.The Arduino boards can communicate with other devices using digital input/output analog input/output standard communication ports like USART, IIC, and USB etc.

A microcontroller based system sometimes controls other devices by simply generating pulses of certain frequencies and duty cycle. This normally happens when the connected device has no processors inside it like in the case of LED display system or motor driving system using PWM based circuits or any kind of switching system. This particular project demonstrates how simple is to code the Arduino board to generate the square waves of any required frequency.

The working of this project is explained based on the Arduino pro-mini board and the IDE version 1.0.3 for windows. The advantage of this board is that it comes in very small in size; any kind of connectors can be soldered on its periphery according to our requirements. It is very breadboard friendly and occupies very less space of a typical breadboard.
Fig. 2: Typical Arduino Pro-Mini Board
The image of the arduino pro-mini board and the arduino IDE are shown below;
Fig. 3: Arduino IDE Software Window
Since the arduino pro-mini board has no circuitary for interfacing it with the serial port or the USB port of the PC, an external USB to TTL converter board is required to connect it with the PC. This hardware helps in programming the arduino board and also helps in the serial communication with the USB port of the PC.
Fig. 4: External USB to TTL converter board for programming Arduino and serial communication
It is assumed that the reader has gone through the project how to get started with the arduino and tried out all the things discussed there.The Arduino IDE provides two functions namely tone() and noTone() for start generating a square wave at a particular frequency and to stop the square wave respectively. The details of the functions are discussed in the following section;
–   tone()
The function tone is used to generate a square wave at the required, with a required frequency and also for a required period of time. The function basically has three parameters of which the first one indicates the pin number at which the wave can be generated, the second one is the frequency of the square wave and the third parameter is the time duration until which the wave should continue. The prototype of the function is given as follows;
tone ( pin_number, frequency, duration );
      –   pin_number
    The parameter pin_numbershould be provided with the number of the pin at which the square wave is required to be generated. As an example to generate the square wave in a pin number 8 the first parameter provided in the function call should be 8.
      –   frequency
     The second parameter of the function is the frequency which should be provided with the required frequency in Hertz. As an example to generate a square wave of 1 KHz the second parameter in the function call should be equal to 1000.
      –   duration
     When the square wave need to be generated only for a particular duration then the required duration in milliseconds should be provided as the third parameter. If the square wave need to be generated until it is stopped by the noTone() function then the third parameter can be avoided in a function call. As an example to generate a square wave at a pin number 8, with a frequency 1KHz and for a duration 5 seconds the following statement can be used.
tone ( 8, 1000, 5000 );
When the wave is required to present at the particular pin until it is stopped by the noTone() function call the following statement can be used;
tone ( 8, 1000 );
–   noTone()
The function noTone can be used to stop the square wave exist in the pin number at which it has been initiated by the tone() function call. The function has a parameter which is the pin number where the wave has to be stopped. As an example the function can be used to stop the wave generated at the pin number 8 as shown in the following;
noTone(8);
How To Generate Square Wave Using Arduino Schematic
THE CODE
The code written for this particular project initializes the LCD in 4 bit mode and displays some initial text in it with the help of functions provided by the library <LiquidCrystal.h>. The functions which can be used to access the LCD are already discussed in previous projects on how to interface an LCD, how to display sensor value on LCD, how to connect the LCD with the PC and how to make an LCD scrolling display.
The code then generates three different frequencies 10Hz, 100Hz and 1Khz using the function tone() for a duration of 10 seconds. The duration is actually generated with the help of delay() function and the wave is stopped using the function noTone() after the delay() returns. The code also glows an LED connected to the pin number 8 using the functions pinMode() and digitalWrite(). The details of the functions pinMode(), digitalWrite() and delay() are already discussed in the previous projects on how to get started with the Arduino board and how to use the digital input and output of the Arduino board.
When the coding is finished one can verify and upload the code to the Arduino board as explained in the project how to get started with the Arduino. The waveform can be observed using a CRO which is connected to the pin number 8.
Fig. 5: Output Square Waveform on CRO with circuit set up on breadboard

Project Source Code

###




/*============================ EG LABS ===================================//

 

 Demonstration on how to generate square wave using Arduino

 

 The circuit:

  The circuit:

 * LCD RS pin to digital pin 12

 * LCD Enable pin to digital pin 11

 * LCD D4 pin to digital pin 5

 * LCD D5 pin to digital pin 4

 * LCD D6 pin to digital pin 3

 * LCD D7 pin to digital pin 2

 * LCD R/W pin to ground

 * 10K resistor:

 * ends to +5V and ground

 * wiper to LCD pin 3

 * LED anode attached to digital output 6

 * LED cathode attached to ground through a 1K resistor

 

//============================ EG LABS ===================================*/


// include the library code:

#include <LiquidCrystal.h>


// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


// give the LED pin a name:

int led = 6;


void setup() 

{

  // set up the LCD's number of columns and rows: 

  lcd.begin(16, 2);

  // Print a message to the LCD.

  lcd.print("ENGINEERS GARAGE");

  lcd.setCursor(0,1);

  lcd.print("PULSE GENERATION");

  // initialize the digital pin as an output.

  pinMode(led, OUTPUT);   

}


void loop()

{

  digitalWrite(led, HIGH); 

  tone(8, 10); // generate square wave at 10Hz

  delay(10000);

  digitalWrite(led, LOW);

  noTone(8); // stop the wave

  delay(3000); 

  

  digitalWrite(led, HIGH); 

  tone(8, 100); // generate square wave at 100 HZ

  delay(10000);

  digitalWrite(led, LOW);

  noTone(8); // stop the wave

  delay(3000);  

  

  digitalWrite(led, HIGH); 

  tone(8, 1000); // generate square wave at 1Khz

  delay(10000);

  digitalWrite(led, LOW);

  noTone(8); // stop the wave

  delay(3000); 

}

###

Circuit Diagrams

Project Components

Project Video

Source: How To Generate Square Wave Using Arduino


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top