Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Hackster is hosting Impact Spotlights: Smart Home. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Smart Home. Stream on Thursday!
sagar saini
Published © GPL3+

Arduino Based Square Pulse Generator (1Mhz)

Timer one is used in this code and using this the Arduino crystal clock is divided into various small parts microseconds are used for freq.

IntermediateFull instructions provided2 hours7,751
Arduino Based Square Pulse Generator (1Mhz)

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
OLED Expansion
Onion Corporation OLED Expansion
×1

Software apps and online services

Arduino IDE
Arduino IDE
EasyEDA
JLCPCB EasyEDA

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Custom parts and enclosures

GERBER FILES

Schematics

CIRCUIT DIAGRAM

Code

CODE FOR I2C 16X2

Arduino
#include <TimerOne.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display 
unsigned long t=1000,f,k=512;// default 1000 s (1000 Hz), meander, pulse 

byte k1,kn,kn1,kn2;
int drive,drive0;

void setup()
{ 
  lcd.init();                      // initialize the lcd 
  lcd.init();
  lcd.backlight();
  pinMode(9, OUTPUT);
  pinMode(7,INPUT);
  pinMode(4,INPUT); 
  pinMode(2,INPUT);
}
void loop()
{
  Timer1.initialize(t); // period    
  Timer1.pwm(9, k); // k - fill factor 0-1023. 
  kn=digitalRead(7); 
  kn1=digitalRead(4); 
  kn2=digitalRead(2);

  if(kn==HIGH){ // decreasing the period 
    drive++;
    if(drive<30){ 
      t=t-1;  
    }
    // if the button is held for a long time, the correction of the pulse 


    else if(drive>30 && drive<60 ){ 
      t=t-10; 
    }
    else if(drive>=60 && drive<100){
      t=t-100;
    }
    else if(drive>=100){
      t=t-1000;
    }
  }
  else{
    drive=0;
  }

  if(kn1==HIGH){// adding a period 
    drive0++;
    if(drive0<30){
      t=t+1; 
      // if the button is held for a long time, the correction of the 


    }
    else if(drive0>30 && drive0<60 ){ 
      t=t+10; 
    }
    else if(drive0>=60 && drive0<100){
      t=t+100;
    }
    else if(drive0>=100){
      t=t+1000;
    }
  } 
  else{
    drive0=0;
  }
  if(t==0 || t>300000){ // limiting the pulse duration to the minimum, if 


    t=1;
  }
  if(t>200000 && t<300000){ // limiting the pulse duration to the 




    t=200000;
  } 
  f=1000000/t; // calculate the frequency 
  k1=k*100/1024; // calculate% fill factor

  if(kn2==HIGH){// button for adjusting the fill factor (in a circle from 


    k=k+16;// step 16 out of 1024 (you can do 8 for smoother adjustment)
  }
  if(k==1024){
    k=0;
  }
// displaying information on the indicator
  lcd.setCursor(0,0); 
  lcd.print("T=");
  lcd.print(t);
  lcd.print(" us");
  lcd.setCursor(12,0);
  lcd.print(k1);
  lcd.print(" %");
  lcd.setCursor(0,1); 
  lcd.print("F=");
  lcd.print(f);
  lcd.print(" Hz");
  delay(300);
  lcd.setCursor(0,0); 
  lcd.print("                ");
  lcd.setCursor(0,1); 
  lcd.print("                ");
}

CODE FOR OLED

Arduino
#include <TimerOne.h>
#include <Wire.h> 
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
unsigned long t=1000,f,k=512;// default 1000 s (1000 Hz), meander, pulse 

byte k1,kn,kn1,kn2;
int drive,drive0;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup()
{ 
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  pinMode(9, OUTPUT);
  pinMode(7,INPUT);
  pinMode(4,INPUT); 
  pinMode(2,INPUT);
}
void loop()
{
  Timer1.initialize(t); // period    
  Timer1.pwm(9, k); // k - fill factor 0-1023. 
  kn=digitalRead(7); 
  kn1=digitalRead(4); 
  kn2=digitalRead(2);

  if(kn==HIGH){ // decreasing the period 
    drive++;
    if(drive<30){ 
      t=t-1;  
    }
    // if the button is held for a long time, the correction of the pulse 


    else if(drive>30 && drive<60 ){ 
      t=t-10; 
    }
    else if(drive>=60 && drive<100){
      t=t-100;
    }
    else if(drive>=100){
      t=t-1000;
    }
  }
  else{
    drive=0;
  }

  if(kn1==HIGH){// adding a period 
    drive0++;
    if(drive0<30){
      t=t+1; 
      // if the button is held for a long time, the correction of the 


    }
    else if(drive0>30 && drive0<60 ){ 
      t=t+10; 
    }
    else if(drive0>=60 && drive0<100){
      t=t+100;
    }
    else if(drive0>=100){
      t=t+1000;
    }
  } 
  else{
    drive0=0;
  }
  if(t==0 || t>300000){ // limiting the pulse duration to the minimum, if 


    t=1;
  }
  if(t>200000 && t<300000){ // limiting the pulse duration to the 




    t=200000;
  } 
  f=1000000/t; // calculate the frequency 
  k1=k*100/1024; // calculate% fill factor

  if(kn2==HIGH){// button for adjusting the fill factor (in a circle from 


    k=k+16;// step 16 out of 1024 (you can do 8 for smoother adjustment)
  }
  if(k==1024){
    k=0;
  }
// displaying information on the indicator
  display.clearDisplay();
  display.setTextSize(1);             
  display.setTextColor(SSD1306_WHITE);       
  display.setCursor(30,0);             
  display.print("SQUARE WAVE");
  
  display.setCursor(0,20);             
  display.print("Freq: ");
  display.setCursor(30,20);             
  display.print(f);
  display.setCursor(0,40);             
  display.print("DUTY: ");
  display.setCursor(30,40);             
  display.print(k1);
  display.display();

}

Credits

sagar saini
81 projects • 90 followers
I am Sagar Saini an electronic hardware enthusiast
Contact

Comments

Please log in or sign up to comment.