Project showcase
 Arduino Toolboxes: PWM Frequency Meter

Arduino Toolboxes: PWM Frequency Meter © GPL3+

A simple but beautifully accurate PWM frequency meter.

  • 4,235 views
  • 4 comments
  • 19 respects

Components and supplies

Apps and online services

About this project

Let's make simple things look beautiful. Just because the LCD shows frequency doesn't mean the numbers and text need to be of a plain, boring font.

Ideation

Most of us, especially me, while working with electronics need something to tell us what the frequency of a square wave or PWM signal in a board is. This is especially true when you are fixing it (those who work with electronics/hardware maintenance will understand exactly how important this is).

Project

In this project we used a digital clock, with 2 fonts for numbers that were created for such a clock.

This project uses interrupts to get accurate time reading. It also used the Math.h Library for inf case, and TimerOne Library for display values every second for a more stable reading on the display.

We used INT0 (Pin 2 In Arduino) as an input interrupt. As a test, I used PWM Pin in the Arduino as input.

Enjoy :)

Download Code With TimerOne Library.

Code

fm2C/C++
#include <LiquidCrystal.h>
#include "TimerOne.h"
#include <Math.h>
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//time defines
unsigned long T1 = 0;
unsigned long T2 = 0;
unsigned long T3 = 0;
unsigned long Told = 1;
// define a bool value to know when the cycle is done and need to calculate
bool Cycle = false;
int reciveddata = 128;
void setup()
{
  //initiate timer1 with 1 sec delay
  Timer1.initialize(1000000);
  //add the function that will be run every 1 sec
  Timer1.attachInterrupt(thedisplayfunction);
  // enable the interrupt in rising endge
  attachInterrupt(0, isr, RISING);
  // Initialize LCD
  lcdcharinitiate();
  lcd.begin(16 , 2);
  // generate pwm signal for testing
  analogWrite(11, reciveddata);
}
void loop()
{
  //we dont need to do anything here
  //i dont like using loop and i avoid it when i can
}
void isr()
{
  // get the current time
  unsigned long now = micros();
  if (Cycle)
  {
    // if the cycle is done the get the total time
    T2 = now;
    Cycle = !Cycle;
    T3 = T2 - T1;
  }
  else
  {
    // if the cycle is not done then calculate the start of the cycle time
    T1 = now;
    Cycle = !Cycle;
  }
}
void thedisplayfunction()
{
  if (T3 != Told)
  {
    lcd.clear();
    lcdDot(10);
    lcd.setCursor(14, 1);
    lcd.print("Hz");
    double theRealValue = 1 / (((double)T3) / 1000000);
    if (isinf(theRealValue))
    {
      theRealValue = 0;
    }
    String theStringValue = String(theRealValue);
    int theStringLenght = theStringValue.length();
    int p = 13;
    for (int i = theStringLenght ; i >= 0 ; i--)
    {
      int thenum = (int)theStringValue[i] - 48;
      numberprinter(thenum, p);
      p--;
    }
    Told = T3;
  }
}
//find the needed characer and added it to the needed position
void numberprinter(int num , int pos)
{
  if (num == 0)
  {
    lcdnumber0(pos);
  }
  if (num == 1)
  {
    lcdnumber1(pos);
  }
  if (num == 2)
  {
    lcdnumber2(pos);
  }
  if (num == 3)
  {
    lcdnumber3(pos);
  }
  if (num == 4)
  {
    lcdnumber4(pos);
  }
  if (num == 5)
  {
    lcdnumber5(pos);
  }
  if (num == 6)
  {
    lcdnumber6(pos);
  }
  if (num == 7)
  {
    lcdnumber7(pos);
  }
  if (num == 8)
  {
    lcdnumber8(pos);
  }
  if (num == 9)
  {
    lcdnumber9(pos);
  }
}
//initiate the lcd custom charcters
void lcdcharinitiate()
{
  //number 0 charaters 2x2 decimals
  byte C0[8] = {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02};
  byte C1[8] = {0x0E, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x0E};
  byte C2[8] = {0x0E, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0E};
  byte C3[8] = {0x0E, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0E};
  byte C5[8] = {0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0E};
  byte C4[8] = {0x0E, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A};
  byte C6[8] = {0x0E, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02};
  byte C7[8] = {0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x1F, 0x1F};

  lcd.createChar(0 , C0);
  lcd.createChar(1 , C1);
  lcd.createChar(2 , C2);
  lcd.createChar(3 , C3);
  lcd.createChar(4 , C4);
  lcd.createChar(5 , C5);
  lcd.createChar(6 , C6);
  lcd.createChar(7 , C7);
  lcd.begin(16 , 2);
}
//lcd draw character functions
void lcdnumber0(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(4));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(5));
}
void lcdnumber1(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(0));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(0));
}
void lcdnumber2(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(1));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(2));
}
void lcdnumber3(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(1));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(1));
}
void lcdnumber4(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(5));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(6));
}
void lcdnumber5(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(2));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(1));
}
void lcdnumber6(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(2));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(3));
}
void lcdnumber7(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(6));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(0));
}
void lcdnumber8(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(3));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(3));
}
void lcdnumber9(int startposition)
{
  lcd.setCursor(startposition + 0, 0);
  lcd.write(byte(3));
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(1));
}

void lcdDot(int startposition)
{
  lcd.setCursor(startposition + 0, 1);
  lcd.write(byte(165));
}

Comments

Similar projects you might like

Arduino Toolboxes : Analog Level Visualizer

Project showcase by Arduino World

  • 11,508 views
  • 15 comments
  • 31 respects

Duty Cycle Calculator and Frequency Meter

Project tutorial by Boaz Lawnce

  • 1,250 views
  • 0 comments
  • 2 respects

DIY Distance Meter with Arduino and a Nokia 5110 Display

Project tutorial by Nick Koumaris

  • 6,369 views
  • 0 comments
  • 16 respects

Temperature and humidity meter (iot)

Project showcase by 윤원호 and gledel

  • 5,687 views
  • 1 comment
  • 15 respects

Simple Capacitance Meter

Project showcase by brzi

  • 3,519 views
  • 3 comments
  • 24 respects

PWM Demonstration Board (Using Arduino)

Project showcase by lmsousa

  • 1,476 views
  • 0 comments
  • 4 respects
Add projectSign up / Login