Arduino Square Wave Generator

Generate PWM signal with Arduino, control the frequency and duty cycle with 2 sliders

Arduino Square Wave Generator

PWM signals are used in many systems. If you don't have a PWM generator in your lab, you can easily build one with an Arduino.

Materials needed:
- Arduino UNO (or similar) + USB cable for it
- DC adapter for Arduino
- BlueSMiRF module
- wires
- Android phone with RoboRemo app installed
- headphones / piezo buzzer / oscilloscope for testing

Introduction

A PWM (Pulse Width Modulation) signal is a type of digital signal that consists of periodic pulses (pin HIGH). When there is no pulse, the pin is LOW. These signals are widely used especially for controlling the speed of a DC motor. The Duty Cycle of a PWM signal is defined as the ratio between the pulse duration and the total period.

0% Duty = no pulses, pin always LOW.
50% duty = symmetrical square wave
100% duty = next pulse starts immediately after previous one, pin always HIGH.

Of course, HIGH and LOW states can be inverted, and then you get an inverted PWM signal.
In this project I use Arduino to generate a PWM signal. I set the frequency and duty cycle using 2 sliders in RoboRemo.
In the video I used PC Soundcard as oscilloscope, that's why the signal is not displayed as perfect square wave.

Steps:

1.
Upload the code to your Arduino:

// Arduino square wave generator controlled via Bluetooth
// using RoboRemo app
// www.roboremo.com

// TimerOne library: https://code.google.com/p/arduino-timerone/downloads/list

// Hardware setup:
// BT module   Arduino
// GND ------- GND
// VCC ------- 5V
// TX-O ------ pin0
// RX-I ------ pin1

// square wave output: Arduino pin 9

#define bluetooth Serial

#include "TimerOne.h"

char cmd[100];
int cmdIndex;

int duty, freq;

void debug(String st) {
  bluetooth.print("debug " + st + "\n");
}

void exeCmd() {
  
  if(cmd[0]=='d' && cmd[1]==' ') {
    duty = atoi(cmd+2);
    Timer1.pwm(9, duty*10.24, 1000000/freq); // 100*10.24 = 1024 (100% = max)
    debug((String)"duty cycle set to "+duty+" %");
  }
  
  if(cmd[0]=='f' && cmd[1]==' ') {
    freq = atoi(cmd+2);
    Timer1.pwm(9, duty*10.24, 1000000/freq);  // period[us] = 1000000 / freq[Hz]
    debug((String)"frequency set to "+freq+" Hz");
  }
  
}

void setup() {
  
  delay(500); // wait for bluetooth module to start

  bluetooth.begin(115200); // Bluetooth default baud is 115200
  cmdIndex = 0;
  
  Timer1.initialize();
  
  duty = 10; // 10%
  freq = 20; // 20 Hz
  Timer1.start();
  Timer1.pwm(9, duty*10.24, 1000000/freq);
}

void loop() {
  
  if(bluetooth.available()) {
    
    char c = (char)bluetooth.read();
    
    if(c=='\n') {
      cmd[cmdIndex] = 0;
      exeCmd();  // execute the command
      cmdIndex = 0; // reset the cmdIndex
    } else {      
      cmd[cmdIndex] = c;
      if(cmdIndex<99) cmdIndex++;
    }
  }
}

2.
Connect the Bluetooth module according to the hardware setup in the Arduino sketch.
In this example we use Hardware Serial, because The Software Serial library is conflicting with Servo library.
But Arduino UNO also uses Hardware Serial for programming, so when you program the Arduino, you need to disconnect the Bluetooth module, and to run the circuit, you need to disconnect the USB cable and connect the Arduino DC power supply.

3.
Connect your Arduino to the DC power supply.

4.
Download the RoboRemo interface file:

square_wave_generator.interface

Copy the interface file to your Android device, then import in RoboRemo using menu -> interface -> import.

5.
Enjoy :)