Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How To Generate Square Wave Using Arduino- (Part 22/49)

By Ajish Alfred July 9, 2013

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.
The image of the arduino pro-mini board and the arduino IDE are shown below;
Typical Arduino Pro-Mini Board
Fig. 2: Typical Arduino Pro-Mini Board
 
 
 Arduino IDE Software Window
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.
 External USB To TTL Converter Board For Programming Arduino And Serial Communication
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);
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.
 
Output Square Waveform On CRO With Circuit Set Up On Breadboard
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

Circuit-Diagram-Generating-Square-Wave-Using-Arduino

Project Components

  • Arduino Pro Mini
  • LCD
  • LED
  • Resistor

Project Video


Filed Under: Arduino Projects
Tagged With: Arduino, square wave
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Industrial Relay Board Design for Motorcycle Use
  • Finding past posts on edaboard?
  • I think i have devised a new type of "super_transformer" for the Electricity grid?
  • What is the purpose of this relay?
  • mosfet driver problem in regeneration mode

RSS Electro-Tech-Online.com Discussions

  • Pic18f25q10 osccon1 settings swordfish basic
  • Sump pit water alarm - Kicad 9
  • Anyone jumped from Easyeda std to Easyeda pro?
  • turbo jet fan - feedback appreciated.
  • More fun with ws2812 this time XC8 and CLC

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • How IoT network topologies work
  • The top five AI startups to watch in 2025
  • STMicroelectronics unveils SoC based on secure MCU
  • Nexperia’s 48 V ESD diodes support higher data rates with ultra-low capacitance design
  • Taoglas releases Patriot antenna with 18 integrated elements covering 600 to 6000 MHz

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe