Author Topic: Help with very simple digital counter needed  (Read 4746 times)


  • Full Member
  • ***
  • Posts: 10
    • View Profile
Help with very simple digital counter needed
« on: November 23, 2011, 05:13:55 PM »
Hello -

Like many I am a longtime developer with newbie status in the embedded space  :wink:. I have a SBC65EC with the IOR5e and am trying to do something (ok, a few things) that I hoped would be relatively simple. Maybe it is :-)

I have a pair of water meters with reed switches. I need to count the closures of the switches in memory and occasionally write the memory value to flash with a time stamp (leading to challenge #2 - getting RTC to work with NTP).  I would like to trend this data visually later.

I had put RTC aside for the moment (I have almost got real time clock to work but am seeing odd values coming from the clock after trying to stitch together all the guidance I could find on the forums. NTP should have worked but not sure how to debug from here).

How can we reliably count the state changes of a digital IO? Is the timing of the SBC65EC so fast that this is just a non-issue? Perhaps so! Can anyone comment? We may have several state changes per second.

Basic Code Examples Desired Are As Follows:
- Basic but complete write/read log function that saves DI or AI values to flash or program area space with time stamp
- Read log example that dumps results to a web page. With date or other filter would be nice
- Complete RTC example with NTP support and web page that displace date and time
- Temperature read example using AI and recommended external component?
- Anyone got a Dallas 1-wire solution? 3rd party I found no longer offers a solution for SBC65EC

My hope was that these simple things could be done with existing example code. Hoping to prove that the above are possible with this platform before investing much more time/effort/money. Please PM me if you are offering a solution to all of above. May be willing to pay for a few hours of help.

Thanks for reading!


  • Full Member
  • ***
  • Posts: 24
    • View Profile
Re: Help with very simple digital counter needed
« Reply #1 on: December 10, 2011, 10:33:22 PM »
Hi Marc
 In order to measure if your reed switches are on/off.. you need to ensure that
they have a DC voltage of 0--5 or 1-26 volts to be used on the IOR5E.

If you are accessing some high voltage equipment, please be aware that
voltage in excess of 40 volts can stop your heart.

So I will be speculating on what the reed relay does based on the assumption
you are going to solder or connect wires directly to one end of it.

Since its unclear what your equipment does, I would rather you check when the
reed relay is ON there is some kind of LIGHT on your equipment. If that is so, I highly
recommend you use an LDR (Light dependent resistor) and glue it to the light and
use A0 or A1 to measure the voltage difference. This way, its NON INTRUSIVE and
it will work without you getting blamed for soldering wires to the reed relay if
somethine happens to  the equipment.

Try to find an indirect way of sensing the ON/OFF logic without direct connection.
worst you could use a Hall effect sensor to check for current flow without directly
connecting to this reed relay.  You could glue a hall effect IC to to the side of the
reed relay to sense current flow when it is on.

I am assuming that once the Reed Relay closes, you can measure a voltage
of at least 5 volts on the equipment's Ground and one side of the relay.

Use a multimeter to check that first and that the voltage is POSITIVE.

Once you know that the voltages are there and within range
you can connect the output of the reed switch to your IOR5E analogue input say A0
port ( a special circuit may be needed) because you are connecting to the reed relay directly
and when it is off, the SBC A0 port could interfere with it.. Ask the forum or other electronic
PIC forum for an answer.

You could  connect it DIRECTLY to the OPTOISOLATOR (OI) inputs which accepts up to 30 Volts.

I don't know the trigger voltage of the OI, but I am guessing its at least 3. volts.

Connect the Ground of the IOR5E to the ground of your equipment.

1. Go to the IOR5E web page system config and configure say A0 to receive a voltage
2. Set the voltage dividers to 26 volts range for safety
3. Review the analog readout page to see if values are present when the reed contacts
and uncontacts.
4.  Note the analogue values when its contacted and not in contact.
5. You may need to modify the web page with the % command for displaying raw analogue
6. Program the SBC65 for internal reference voltage and activate A0..A3 for analogue input
using the system config web page.

Same for the OI, check the OI values on the IOR5E web page and observe it with the
reeds OFF or ON.

Once you decide which input you want to use you can proceed to the next stage
which is logging.

I recommend you use the OI inputs for safety as the chips cost
only $5 to replace and you have 4 of them. If you mess up .. the A.. pins lead directly
to the Microchip IC and the IC and you could brick the entire board.

Next check your equipment you are messing around with. Is it still functioning properly?
Is the reed relay oscillating on/off after your connection to the SBC65? If you connect
a voltage to the IOR5E's voltage divider, you COULD experience a 0.3 volt drop because
of the low value of the resistors on the IOR5E

Connecting one side of a reed relay to the SBC65 or IOR5E OI could drop voltage on the
equipment and cause problems.
Since  you have decided to attach a time/date stamp to each reed count, external logging is
out of the question.

To move ahead quickly you need to use  a Serial   SD card datalogger called OPENLOG
•OpenLog runs at 3.3-5V at 9600bps by default so its safe for SBC65
However it does not support SDHC for now.
Openlog only supports a 2GIG MSD card. Any bigger it wont work.

That is you need use the 3 pin connector and send out serial to this datalogger.
pls dont forget to change the baud rate as per openlog's instructions.

Solder the openlog wires to the appropriate pins on the 3 pin serial connector
and connect the 5V power on the IOR5E to the openlog to power it.
you COULD make yourself a custom connector and connect it to the SBC65 EC
when its flipped upside down. So that you still have access to the serial cable
for debugging. This will be serial 1. For Serial 2 the pins (d1 and d6) are not accessible
on the IOR5E. According to the schematics , it seems Not Connected.
Just connect openlog and the serial port cable at once. The openlog will
ignore invalid commands and at the same time you can debug your code
via the serial out and hyperterminal.

Code for sending to openlog can be found in google's GENTLE NAV code which is
compatible with MPLAB

Working RTC code can be obtained from a IOR5E weather station at

So once you are able to log, you can check your logging using a PC to read the SD card.

If you are successful up to this stage, then the job would be to use UDP protocol
to send out the logged data to your PC. You would have to have an internal routine to erase
the SD card once the UDP code has finished grabbing the data.

Some kind of verification method would also have to be introduced to ensure your UDP data transfer is correct.

You can develop a UDP call to push your data to an SQ LITE database on a windows or linux pc
 and use many data analysis tools to view your data locally or on the web.

For example your udp code should only handle 1 openlog entry at a time so as to give
time for other processes to take place. At the next call you can erase the logged data
you just sent.

You must be careful when placing any NEW loops in the SBC65EC code. You must store your
data in global variables and wait for the SBC65 Loop  to call your code to finish what you are doing.
Otherwise you WILL miss any new Reed status.

The SBC65ec code is NOT event driven. Its just a giagantic while loop that
calls a bunch of functions in a sequential order. if you have waiting code in your while/for loops
the SBC65ec will hang if that wait never ends or miss data while waiting in your loop.

That is the more difficult part that is very different from regular programming on the PC.
Say that your timestamp is YYMMDD.HH.MM.SS.0 (off)  and YYMMDD.HH.MM.SS.1 (on)

You may have to program a way to send up to X number of characters say each time
the SBC65 calls your function, you only send 5 characters and you finish sending the other
characters each time. However when the function exits another reed event may take
place from the reed going OFF to ON..  So maybe you want to only send data when the
reed is ON. All othertimes, the reed is taken to be in OFF state.

To reduce data furthur you may want to change the date to a JULIAN number and convert
the timestamp all to SECONDS so that it saves you transmission time.

You may have to implement a software stack and pop the items to be sent as your code
runs.. Maybe another off event may allow you 3 seconds and in this 3 seconds you send
the outstanding ON states.

It all depends on the RATE of your reed ON or OFF states (which ever you want to store or collect
data on) . If the rate is too fast, your SD card will fill up and become full and you start to lose data.
and Maybe its NOT the kind of project that is suitable for embedded system.

My advice is to work out how you want to store the data at the destination and how
you want to process this on the SBC65. Work out the process first, now that you have
been warned about the limited time you have to process the data.

If your reed relay events take place very far apart in time and never within a few milliseconds
then you can be more lazy with your code. But coding for tight timing will make you a better
programmer and you can cater for critical situations later.

DS 1307. I find the DS1307 to be very accurate. My recommendation is that depending on
how much free time you have, you COULD ask your PC to get the NTC time and use a http command to the SBC65 and update your DS1307.

Alternatively you could look at Microchip's latest web server code and copy the NTC update
from there.

You can connet a LM75 directly to the A0 ..A1 of the IOR5E

Displaying values to a web page served by the SBC65 - Depends on what you want
to display. If you want to show Accumulated values of ON states, you could store that
value into the SBC65 EEPROM OR save it to openlog and display that.

If you want elaborate stuff, Its not going to be easy on the SBC65 if the data is huge.
Nowadays, people want to view summary information on their mobile. so a small Java script
page that ACTS on the total accumulated value per day or per hour and displays color codes
to warn of problem is a better way to develop your program.

Try to code all the complex math in the java script so that you don't have to re-build your MPLAB
project. All you need to do is to modify a javascript page and upload the web page to the SBC65.
The Javascript will have access to 32 bit math in today's devices or take care of large numbers
for you. It also has ways to display and do simple graphs. All these calculations takes place
IN YOUR BROWSER not on the SBC65. It just Serves up the javascript  for your browser to access
and replace tags with actual values so your code can act on it.

You could also use the example SNMP module and use any free SNMP monitoring tool like PRTG to graph and
do data analysis if you can dispense with the datetime stamp. If you chuck away he date time stamp the
SNMP code that will query the SBC65 will happen every 1 min (Free prtg limitation) or 30 seconds (paid version)
The value would be the total accumulated Reed ON events during that 1 min. Date and time will be PC date and time
as recorded by PRTG network monitor.

if you want to avoid SNMP or complex coding, provide PRTG with a Value  REED1.HTM and REED2.HTM each of which will
show the accumulated values for each relay on event. Then PRTG will query the SBC65 every 1 min and record
the event for you. You can then do graphical data analysis on your pc. This avoids the DS1307 and the Openlog device
and gets you up and running very quickly. Of course any network break down will cause data loss. unless you keep accumulating the values until each query is made and you have a small UPS to guard your SBC65.

so Just ensure that you have a reasonable way to measure your reed states. If there is hundreds of reed on states per second then I think this is not suitable.. You should then switch to  measuring some other consequence of the reed relay clicking like temperature rising above a certain value or something or the RPM of a motor going above certain value.

OR you could change your code to meaure the RATE of reed relay switching above or below which an alarm must be raised.


« Last Edit: December 11, 2011, 12:44:41 AM by eagleeye »


  • Full Member
  • ***
  • Posts: 10
    • View Profile
Re: Help with very simple digital counter needed
« Reply #2 on: February 12, 2012, 07:28:44 AM »
Notifications did not reach me for some reason. Thanks so much for the very detailed reply!! :lol:
I'll review this carefully...