Counting events with Arduino and PCF8583

Hey!

I’ve been away for some time. It’s not that I had stopped tinkering, but work and laziness have kept me away from the blog. During these months I have been working mostly on a new weather station (yes, yet another weather station or YAWS). The project was a step forward in a lot of aspects, from carpentry to remote reporting hacking cheap chinese routers and 3G dongles, from new libraries for Arduino to a bidirectional HTTP to MQTT bridge in node.js…

The result has been kind of a fail… mostly due to improper casing, humidity has been a major enemy. Anyway there are quite a few aspects to talk about, so I’ll try to write a series of posts about different hardware and software components involved in the project.

Counting the wind and rain

Two of these components share the same interface: when an event happens a pulse is triggered. The anemometer is a cheap plastic model I bought from todoelectronica.com. It’s specifications state than a 10km/h wind correlates with 4 pulses per second, so it is a simple pulse counter.

Anemometer

Anemometer

The rain gauge is a little more tricky… I bought a wireless rain gauge at ebay but I really didn’t need the wireless feature, it was just a convenient model to hack. The sensor has a seesaw with two small compartments where rain drops fall when passing through the top hole. The seesaw has a small magnet in the middle. When the water weight moves the seesaw down the magnet closes a reed switch triggering the pulse. The water evacuates and the other compartment starts receiving the water.

Wireless Rain Gauge

Wireless Rain Gauge

I hacked the rain sensor disabling all the circuitry and soldering a couple of wires to the reed switch with a load resistor in series. By “disabling the circuitry” I mean cutting the lines to the switch to avoid spurious currents to create noise but leaving the circuit, so it fits nicely inside the sensor.

I decided to use a standard interface for the cables, a 3.5 stereo jack (a mono jack would have been enough but I didn’t have any around). They plug into the main board of the weather station and the signals go to the event counter pins of a couple of PCF8583.

The PCF8583 is very similar to the PCF8563 but it adds several nice features: hardware configurable I2C address with 2 different addresses available (you can throw 2 of these IC in your project without mush hassle), 240 bytes of free RAM, more alarm options and an event counter mode.

According to the datasheet, “The event counter mode is used to count pulses externally applied to the oscillator input (OSCO left open-circuit).”. The count is stored as BCD values in 3 registers. BCD stands for Binary-coded decimal, the most common implementation is to split a byte into 2 groups of 4 bits and use each group to represent a decimal number from 0 to 9. So, for instance, 37 would be encoded as “0011 0111”. Thus, 3 registers or 3 bytes allow a maximum of 1 million events. This IC is very convenient for any project where you want to remove the responsibility of counting events from your microprocessor, and it would have been a very good choice for my smartmeter pulse counter.

To easily access the IC features I have written an Arduino library for the PCF8586. The library is not complete, there are some methods to be implemented yet. But the core time and event counter methods are already there.

To library comes with a simple exemple of usage (I have to add more examples) but to use it as an event counter you will only have to write something like:


#include

// declare an instance of the library for IC at address 0xA0
// (A0 pin connected to ground)
PCF8583 counter(0xA0);

void setup {

    // configure serial link to debug
    Serial.begin(9600);

    // configure PCF8586 to event counter mode and reset counts
    counter.setMode(MODE_EVENT_COUNTER);
    counter.setCount(0);

}

void loop {

    // report the events every second
    Serial.println(counter.getCount());
    delay(1000);

}

Issues

There are a couple of issues to take into consideration.

First: the counter counts double. That’s it: for every pulse it looks like it counts the rising AND the falling edges so you end up with double the counts. I have not been able to find a reason for this, nor even in the datasheet. I have tested it with a controlled set up, cleaning the input with an RC filter and a Schmidt Trigger and it counts exactly double it should…

Second: the maximum external clock frequency in event counter mode is rated to 1MHz. In the real world of my projects noise is an issue with frequencies way bellow that mark, so I only accept sensible values depending on what I’m measure. For instance, the above anemometer sends 4 pulses per second for a 10km/h wind. Fortunately, where I live there are no hurricanes and I have not checked but I doubt that there have ever been gusts of more than 150km/h. That sets a maximum of 60Hz. So in my code I have a check to not trust any value over 60 counts per second.

Final note

Finally, there are some other libraries for the PCF8586, like this one by Ryan Mickfor Netduino or this other one by Eric Debill for Arduino, that are worth checking as a reference.

As always, any comment will be more than welcomed.

It’s nice to be back 🙂

CC BY-SA 4.0 Counting events with Arduino and PCF8583 by Tinkerman is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

12 thoughts on “Counting events with Arduino and PCF8583

  1. Pingback: Ciseco XRF modules & LLAP Protocol | Tinkerman

  2. Francesco

    Good morning,

    my name is Francesco.

    A year ago i bought a led panel “sure electronics P4 32X8 3208 red led
    dot matrix unit board spi like” and the “Arduino UNO” but i
    can’t
    understand how to use the panel program.

    I would like to add some buttons and potentiometers to the system.
    For example: pressing the button “1” display lights up with the
    saved
    name wich runs for 30 seconds at least. Pressing another button display
    lights up with another saved name wich also runs for 30 seconds at least
    and so on for the other buttons wich have to recall the saved words.
    I also would like to know how to add two potentiometers: on for slide
    speed control and one just for time control.

    I’m waiting for your answer and i thank you in advance.

    Cordially,
    Francesco

    Reply
    1. xose Post author

      Hi Francesco

      First I would recommend you to focus on every aspect of your project separately.
      That’s something I do all the time. My background is as software developer and I have the habit of “unit test” the different “pieces” of my code isolated.
      So first add a button and test it. Then remove the button and add a potentiometer and test it, make sure you understand what inputs and outputs you have.

      As for the display, it’s based on the HT1632C driver by Holtek. The same I use for my Rentalito. But the code I use will not work from scratch with your display because the geometries (32×16 vs 32×8) are different and the drivers memory is organized differently (you might want to check the datasheet for the HT1632C). Off course you could try to tweak it but maybe it will be easier to start reading the entry for the HT1632C at the Arduino playground and the HT1632 library for Arduino by gauravmmm.

      Hope this helps.
      Xose

      Reply
  3. Mark

    Hi Xose,

    I have been building a wether station also and found you have the only lib ray for the PCF8583 that implements the counter. I have looked at the counting by 2 problem you lib has and I think you may have an error with the Function modes numbers. I suspect you may be one bit out. These numbers seem to work better;

    // Function modes
    #define MODE_CLOCK_32KHZ 0x00
    #define MODE_CLOCK_50HZ 0x10
    #define MODE_EVENT_COUNTER 0x20
    #define MODE_TEST 0x30

    Regards,

    Mark

    Reply
    1. xose Post author

      Hi Mark

      Thank you for you comment, but… aren’t those the same numbers I use?
      I mean: 0x00 is octal for 0, 0x10 is octal for 8, 0x20 is octal for 16 and 0x30 is octal for 24…
      am I wrong?

      UPDATE: Yes! I was very wrong. Don’t know why I was thinking about octal instead of hexa…
      Thanks Mark and Bob for the point.

      Reply
      1. Bob Buecher

        Hi Xose,

        Thanks for this great library! It is just what I need to make a wildlife counter. I just received an order of parts that includes a few of the PCF8583 chips today. Guess what i’ll be doing tonight!

        I think that Mark may be correct about the constants used in the library. No, I haven’t looked at the library yet but values are usually given in hexadecimal rather than octal.

        Bob

        Reply
  4. Alex

    Hi Xose,

    Is it possible to have both RTC time and external event counter via single PCF8583 instance? Could you show your schematic?

    Alex

    Reply
    1. Xose Pérez Post author

      It’s been a while since I last played with this IC. I’m not 100% confident but I would say you cannot use both, so you have to choose between event counter and time features.
      And sorry, don’t have any schematic, I used it in a couple of projects but they no longer exist 🙁

      Reply
      1. Alex

        Hi Xose,

        Thank you for response, it looks like you are right – it is single function only.
        Do you know any other IC that supports event counting and RTC in the same time?

        Best regards,
        Alex

        Reply
        1. Xose Pérez Post author

          Not that I have in mind. Actually in the PCF8583 datasheet there is an application circuit with two PCF8583 using different functions, it really doesn’t take much more.

          Reply
  5. Aleksandr

    THANK you very much for the library. Standard counter works fine. Do experiments for obtaining a counter with pre-dividing by 50, but with a big bit in the clock mode 50 Hz. 🙂
    RUS. Большое СПАСИБО за библиотеку. Стандартный счётчик работает отлично. Провожу эксперименты по получению счётчика с предварительным делением на 50, но с большой разрядностью в режиме часов 50Гц. 🙂

    Reply

Leave a Reply (all comments are moderated, be patient)