Circuit Gear, Arduino and Counting Pulses


 

Square Wave Generated by Circuit Gear

Square Wave Generated by Circuit Gear

Arduino has a convenient function called pulseIn that you can use to count the HIGH or the LOW of a pulse. To test this, I created a square wave using the excellent Circuit Gear Waveform Generator.

I uploaded this program and connected the output of the waveform generator to the board (connect the Red wire to the Arduino pin 3, and the black to the Arduino digital ground). The program started printing the count of the HIGH pulses. As I adjusted the frequency of the square ware, the print out was getting faster or slower depending on the frequency of the wave. But as I started changing the amplitude of the square wave, I noticed than when the voltage difference between the LOW and HIGH of the square wave was less than 4 volts, the pulseIn function could no longer read the HIGHs. The documentation for the HIGH confirmed that the difference between the LOW and HIGH must be at least greater than 3 volts. In my case, anything less than 4 volts just didn’t work.

To show a waveform on the Circuit Gear, connect the output of the generator (leftmost connector) to one of the oscilloscope inputs (the two on the right). You’ll need something that can connect to BNC connectors, either alligator clip leads or adaptors or scope probes.

In other words, there is no internal connection between the output of the generator and the input of the oscilloscope, you have to add that.

(If you had another oscilloscope, you could also connect the output of the generator to that oscilloscope and view the waveform.)

Here is the pulse counter program for the Arduino:


int pulsePin = 3;
unsigned long counter = 0;
unsigned long duration = 0;
unsigned long timeout = 1000000; // in microseconds

void setup() {
  pinMode(pulsePin, INPUT);
  // enable the 20K pull-up resistor to steer
  // the input pin to a HIGH reading.
  digitalWrite(pulsePin, HIGH);
  Serial.begin(9600);
  Serial.println("Here we go again");
}

void loop() {
  duration = pulseIn(pulsePin, HIGH, timeout);
  if (duration == 0) {
    Serial.print("Pulse started before the timeout.");
    Serial.println("");
  } else {
    counter++;
    Serial.print(counter);
    Serial.print(", ");
    Serial.print(duration);
    Serial.println("");
  }
}

6 Responses to “Circuit Gear, Arduino and Counting Pulses”

  1. Shahram Javey Says:

    I should add, that the way to add pulses is to use the Arduino interrupt feature instead of a regular GPIO.

  2. Timecode | wer bastelt mit Says:

    [...] circuit-gear-arduino-and-counting-pulses Dieser Eintrag wurde veröffentlicht unter Arduino. Permalink in die Lesezeichen aufnehmen. ← Arduino Info-Pool LikeSei der Erste, dem dieser post gefällt. [...]

  3. Thomas Varghese Says:

    can u tell me how i can use the arduino to count pulses.
    i am doing a project for my electronics alevel,, i am building a Wind speed detector..the pulses are coming from a reflective opto switch, this is the rpm count and i have to divide this by the number of wind blades…..this is my sketch so far.. but it keeps reading RPM as 250/245….so, the speed as 2.35mph…..even though i haven’t connected the output of the reflective opto switch to the digital pin 2.

    #include

    // initialize the library with the numbers of the interface pins
    LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

    volatile byte rpmcount;

    unsigned int rpm;
    float speed;

    unsigned long timeold;

    void setup()
    {
    lcd.begin(16, 2); // Set up the LCD’s number of columns and rows:
    lcd.setCursor(0, 0); // Sets the posiion of the cursor
    lcd.print(“Hello World”); // Print a message to the LCD.
    lcd.setCursor(0, 1); // Sets the posiion of the cursor
    lcd.print(“WSD2011″); // Print a message to the LCD.
    delay(4000); // Delay for opening message
    lcd.begin(16, 2); // Clear Screen
    attachInterrupt(0, rpm_fun, RISING); // input signal, pin 2
    rpmcount = 0;
    rpm = 0;
    timeold = 0;
    }

    void loop()
    {
    if (rpmcount >= 30) {
    //Update RPM every 20 counts, increase this for better RPM resolution,
    //decrease for faster update
    // calculate the revolutions per milli(second)
    rpm = ((30*1000/(millis() – timeold))*rpmcount)/6;
    timeold = millis();
    rpmcount = 0;
    // WHEELCIRC = 2 * PI * radius (in meters)
    // speed = (rpm * WHEELCIRC * “minutes per hour”) / “meters per miles”

    // simplify the equation to reduce the number of floating point operations
    // speed = 60* rpm* WHEELCIRC* 1/ 1609.344 —- In mph

    speed = (60* rpm* 0.251)/1609.344;

    lcd.setCursor(0, 0); // Sets the posiion of the cursor
    lcd.print(“RPM:”); // Print a rpm lable to the LCD.
    lcd.setCursor(6, 0); // Sets the posiion of the cursor
    lcd.print(rpm,DEC); // Print rpm value
    lcd.setCursor(0, 1); // Sets the posiion of the cursor
    lcd.print(“Speed:”); // Print a speed lable to the LCD.
    lcd.setCursor(7, 1); // Sets the posiion of the cursor
    lcd.print(speed,2); // Print speed value
    lcd.println(” mph”); // Speed unit
    }
    }

    void rpm_fun()
    {
    rpmcount++;
    //Each rotation, this interrupt function is run twice
    }

  4. Thomas Varghese Says:

    oh…. forgot to mention….even though the output is not connected a jumper wire has to be connected to show the readings…!!!!!!!!!

  5. Thomas Varghese Says:

    oh…. forgot to mention….even though the output is not connected a jumper wire has to be connected to show the readings…!!!!!!!!!

  6. rain gear for men Says:

    rain gear for men…

    [...]Circuit Gear, Arduino and Counting Pulses « n o t e 1 9 . c o m[...]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: