Count, PulseIn or use both?

darb1972

Senior Member
Hi All

Is there such a thing as software block? Something a bit like writer's block? :confused:

I have a project that feels like it might never start unless I can decide on the best way to monitor a pulse.

Here's the summary of the project (I hope this makes sense):

I want to monitor 2 different mains (power) lines for activity. I have a device (one for each mains line) that acts as an opto-coupler and wave shaper (it is designed to take the mains, in my case 240V AC, reduce amplitude (via resistors) and produce a nice and relatively clean 5V logic level pulse equivalent to the frequency and duty cycle (50Hz 50% duty cycle). The devices will feed into an 08M2 as I then only need two outputs to drive some further logic. Only one input at any one time will be active. The active (mains) input will remain active until such time as it switches over to the opposite mains input. In other words, only one mains voltage will be present at any one time. Once an input is detected, the 08M2 will drive an output (high logic level only) for a brief period and then ignore that input and wait for the inputs to change state. Basically it toggles from monitoring one input to the other.

The hardware side of things isn't an issue. What I would like some advice about is code (not my strong point). At switch on (for the very first time), the 08M2 will have NO idea whether input one or input two will have a mains drive pulse on it. Should I use PulseIn or Count to monitor both pins? I think the Count command is somewhat blocking until it times out. Is that correct? I'm not sure how to monitor both inputs (initially). Should I use PulseIn to monitor both pins instead? Need some pointers here.

I also wanted to try and incorporate the Count command to count the pulses for a brief period to ensure that I'm getting a mains driven signal and not just a bit of noisy rubbish (particularly from the initial switching process). Will the 08M2 happily count pulses as slow as 50Hz??? It doesn't have to be super accurate, I just want to confirm I have received a 50Hz signal before the 08M2 outputs the applicable logic level.

Any initial advice to get me started would be appreciated.

I'm not expecting anyone to write code for me (actually, it's probably good if you don't as I'll learn more doing it myself). I'm just looking for some general advice on the commands above (or maybe some bright spark can suggest something else).

Thanks in advance.
 

hippy

Technical Support
Staff member
You don't need PULSIN or COUNT; you simply need to know if a signal is present and that can be determined by whether a signal is ever present within a certain time period.

If your inputs are guaranteed to be low when there is no signal and goes high at a 50Hz or 60Hz rate when it is, you know you will see at least one high during some time period if a signal is present. And you don't have to check excessively frequently if the signal will be high for a few milliseconds at a time.

How long you check for will determine the responsiveness when a signal is absent. How quickly you check will determine the responsiveness when a signal is present.

You can however use COUNT for a short period of time because that will allow you to determine if there were the expected number of pulses. That it is a blocking command shouldn't really matter. The COUNT command can count as low as one pulse every 65s.
 

fernando_g

Senior Member
Hmmm.....
why don't you use interrupts?
Interrupt on either of the conditions being true, and then on the interrupt routine itself, test which of the two inputs is actually high.

You say that "only one output will be high at the same time". Are you completely sure that this is always the case, even during fault conditions or abnormal events? If not, in the same interrupt routine you should figure out a failsafe way to deal with that condition.
 

The bear

Senior Member
@darb1972,
Is the project a back-up generator, that cuts in automatically during a mains power failure?

How about a cheapo 'scope permanently connected to the supply/s?

Regards, Bear..
 
Last edited:

darb1972

Senior Member
Hello Gents

Thanks for the replies.

@Hippy & fernando. Thanks for the comments/feedback. Hippy I think I'm following your comments but I also like fernando's idea of using an Interrupt to detect the initial activity on the input/s. I must admit that when I think of Interrupts I tend to only think of the X2 series and not the M2 series. The more limited Interrupt function of the M2 series should suffice for my project. It's a good suggestion. I could then use the Interrupt routine to test the active input, and, as you suggested towards the end of your comments, I could use the COUNT command to confirm the presence of a 50Hz pulse as opposed to having an Interrupt triggered by random noise/spikes. I appreciate the suggestions!!!

@ Bear. No. Not even close (but good try)!!! I'll try to explain. The "client" (yet another love job) approached me as they had purchased a new projector screen to replace the old one and have some "interfacing" issues. They had to purchase a particular screen to replace the faulty one so it would fit in the recessed roof cavity. Essentially when the screen is retracted it is not visible to the audience. The old screen was driven directly by a Clipsal "Star Serve" system. The client has a remotely located touch screen to take the screen up/down. 240V power to drive the old screen down (to its limit switch) and a separate 240V to drive it back up.

The NEW screen has a separate 240V supply and uses a logic level to drive internal circuitry on the screen to take it up or down. This is where I come in. I need to convert the Clipsal Star Serve "signal" into a logic level to drive the new screen.

Does that make sense Bear?

I'll post the schematics and code when I'm done (unless I come up with questions beforehand).


Regards Brad
 

hippy

Technical Support
Staff member
Interrupts seem overly complicated. I don't see a problem with COUNT blocking. There are 50 mains cycles in 1 second, 25 in 500ms, 12 in 250ms and 6 in 125ms. Counting for 125ms and checking if the value is greater than 4 would seems responsive and reliable enough to me.

Count MAINSA, 125, b0 : If b0 > 4 Then Aon
Count MAINSB, 125, b0 : If b0 > 4 Then Bon
 

The bear

Senior Member
@ darb,
Thanks for the explanation, I'm going back to hibernating.

Regards, Bear.. zzzzzzzzzzzzzzz
 

darb1972

Senior Member
@Hippy. Hello Hippy. All I can say is WOW!!! I'm blown away by how simple the solution is when compared to the code I thought was needed to achieve the same result. I have a nasty habit of over complicating things so your simple yet elegant suggestion is very welcome. I'll work on the remainder of it, test it in a real life scenario and post back when done. I'm very grateful for your suggestion and time. Thank you!!!

@Bear. I think I'd prefer to be in the middle of our winter and hibernate too. I can't sleep very well at the moment. Our nights are around 20 to 25 degrees (that's in Celsius) and the days have been pushing 40 degrees. It's made worse by the humidity of somewhere around 90% to 100%. Tomorrow we are meant to get some much needed rain but it's still going to remain at 38 degrees!!! Thank God for air conditioning!!!
 

darb1972

Senior Member
Hi All

I had to get back onto this project in an effort to get it finished very soon.

Please see attached. I have included my schematic and code. I have also included some screenshots of an item of concern.

By all accounts, the project all works flawlessly. Thanks to Hippy for some tips with the code.

The schematic is currently made up on an AXE91 Project Board. I’m using a 14M (new) to use it up as this is a fairly simple project in terms of a micro-controlling task.

Please refer to the screenshots. My concern is with the switching noise created from at the inputs to the PICaxe 14M. This is a result of the switching of the 240V AC 50Hz inputs. It is definitely as a result of switching the 240V inputs and NOT anything to do with my test setup. I was able to simulate the same issue by switching the inputs by means of a conventional 240V mains switch AND also (in a separate test) from using the contacts on a relay. I am confident that I will get the same issue in a real life (I can’t test at the moment as the projector screen is about 5m off the floor). The system runs off a 240V Clipsal C-Bus system which (I believe) uses normal (electro-mechanical) relays.

The switching noise must be substantial in amplitude because the input resistors to the HCPL-3700 drop the signal to around 5V peak to peak for the sinusoidal input. Unfortunately, the HCPL-3700 seems to pass this noise on, despite having what should be internal circuitry that should clamp the output to limit such switching noise.

The normal output of the HCPL-3700 is a clean 5V peak to peak square wave (when not switched).

At its worst, when the switching noise is captured on my DSO (on the input pin/s to the PICaxe 14M) the peak to peak switching noise level has been up as high as 15V (roughly around 5V above and 5V below the normal 5V square wave output).

Frankly I have tried just about everything I can think of to remove this switching noise. I have tried different low pass filters (R/C and R/L) at the outputs of the HCPL-3700 which feed the 14M. I have installed a diode and a 5.1V Zener from the HCPL-3700 outputs to Vcc to try and clamp any signal exceeding Vcc and -0.7V below Gnd.

I have also tried installing a capacitor on the primary side of the HCPL-3700 (according to the spec sheets and application notes). This causes the output of the HCPL-3700 to change from a square wave to a DC level. Even with this change, the switching noise still makes it through to the 14M.

None of what I have tried seems to have dampened or removed the switching noise. Even the VDRs on the 240V side don’t seem to clamp the switching noise.

The noise does seem to change amplitude with different switching attempts. My guess is that this depends on when/where the switching occurs within the sinusoidal cycle. I gather the noise is at its lowest amplitude when the switching occurs somewhere around the zero-crossing point of the sinusoidal cycle.

There is one thing I haven’t tried (to date). I haven’t tried using two complete L/C mains filters on the 240V side of the project. While I could introduce such filters on both inputs, I am trying to avoid such a move at these filters are typically very bulky. Based on all of my other attempts I have my doubts these filters would remove the switching noise.

While the system (currently) works flawlessly on my test bench, I am concerned about the longer-term damage that might occur to the inputs of the 14M due to the amplitude of the switching noise at the 14M inputs.

I am also perplexed as to why all of my attempts to clamp/dampen or (preferably) remove the switching noise has failed.

Can anyone offer ideas as to how to filter/remove the switching noise OR does anyone have any comments on the amplitude of the noise being fed into the 14M???

I would appreciate any feedback.

Regards
Brad

Here's the code:

Code:
'''''''''''''''''''''''''''''''''''''
'	Projector Screen Controller	'
'	Version V1.0 15/04/2017		'
'	PICAXE 14M uController		'
'''''''''''''''''''''''''''''''''''''

'Directives

#picaxe 14M
#no_data


'Symbols

symbol MainsCountA = C.1
symbol MainsCountB = C.2
symbol PulseCount = b0
symbol CountPeriod = 250
symbol MinimumCount = 9
symbol MainsOutA = B.1
symbol MainsOutB = B.2
symbol LED_OutA = B.3
symbol LED_OutB = B.4


Initialise:

PulseCount = 0
Low MainsOutA
Low MainsOutB
Low LED_OutA
Low LED_OutB
Pause 100

'Search Inputs for an initial Pulse (Mains Input)
'Loops while there is no input detected

Search_Inputs:


Do

Count MainsCountA, CountPeriod, PulseCount
	If PulseCount > MinimumCount Then SwitchA_On

Count MainsCountB, CountPeriod, PulseCount
	If PulseCount > MinimumCount Then SwitchB_On
	
Loop
	

'Routines


SwitchA_On:


	High MainsOutA
	High LED_OutA
	Pause 1000
	Low MainsOutA
	Low LED_OutA
	Pause 500
	Goto Monitor_InputB


SwitchB_On:


	High MainsOutB
	High LED_OutB
	Pause 1000
	Low MainsOutB
	Low LED_OutB
	Pause 500
	Goto Monitor_InputA


Monitor_InputA:	'Monitor Input A and ignore Input B

Do

Count MainsCountA, CountPeriod, PulseCount
	If PulseCount > MinimumCount Then SwitchA_On
		
Loop

	
Monitor_InputB:	'Monitor Input B and ignore Input A

Do

Count MainsCountB, CountPeriod, PulseCount
	If PulseCount > MinimumCount Then SwitchB_On
		
Loop
 

Attachments

darb1972

Senior Member
For anyone wanting to view the DSO screenshots I might have to come up with another way to display them.

I have tried uploading them to the forum as JPEGs and GIFFs and neither will work. I don't know why that would be the case as they are both compliant file formats and only very small in size.

I have also tried inserting them into this post but that doesn't work either.

Stuffed if I know what's going on with trying to upload the files.
 

hippy

Technical Support
Staff member
At its worst, when the switching noise is captured on my DSO (on the input pin/s to the PICaxe 14M) the peak to peak switching noise level has been up as high as 15V (roughly around 5V above and 5V below the normal 5V square wave output).
It does seem odd you are getting such high voltages at the PICAXE pins. The input of the HCPL-3700 does seem to be completely opto-isolated from the outputs which uses 0V and 5V power rails which also power the PICAXE.

I would suspect that noise is being introduced onto the power or signal rails by some other means rather than through the opto-isolators.

What are Input A and B, what are your Z1 and Z2 zero-ohm resistors for, and are they fitted ? They look suspiciously like they connect Neutral to Earth which could cause some interesting issues and dangers especially if the input is mains.

Also, what is J4 and the two MOSFET's driving ?
 

AllyCat

Senior Member
Hi,

My guess is that you either have a "missing" earth connection (e.g. to each 'scope probe) or an "earth loop". Which it might be will depend on your test setup and particularly the 'scope.

For example, I have the two "extremes", which can both use a standard BNC Probe: a tiny Velmann battery-operated LCD scope (i.e. totally "floating") and the DP-SE(=PCB Scope) which of course must have a "common earth" with the Computer and the PICaxe Download cable. They may produce completely different waveforms (but hopefully not magic smoke), so it's a case of horses for courses.

Cheers, Alan.
 

darb1972

Senior Member
Hello Gents

@ Hippy. Indeed it is very odd. I'm reasonably certain that the noise is coming from the switching. I have now put the screenshots into a word file and then converted them to a PDF. It is now attached as the JPEGs and GIFFs wouldn't attach. Inputs A and B are 240V from a Clipsal C-Bus system. They have a common Neutral with "Active A" to drive the (old) screen in one direction (deploy or retract) and "Active B" to drive the (old) screen in the opposite direction. The Zero Ohm links are not installed on the test bed and are only there for my benefit on the PCB if I need to link the Neutral from one channel to the other. They can be omitted when the PCB is populated. Even if the old screen had an Earth (I can't remember for certain), it has no relevance to my design as I am only interested in the common Neutral and switching Actives.

J4 and the MOSFETs drive some external inputs to the NEW screen. These inputs are only low level contacts. 240V cannot be driven directly into these inputs. The new screen essentially requires one wire to be pulled to ground for the screen to deploy and another to retract.

@ Alan. I'll have to double check my grounding but I believe it is all good. My DSO uses a fairly advanced probe with good grounding. I'm only using one probe at a time. I will swap this probe tonight to make sure it's not a grounding issue.


The attached screenshots are a "one shot" capture of the noise at the moment of switching. They do not contain the nice square wave I get when the circuit is running.

I'll check a few more things and come back to you both.

Thanks Gents.
 

Attachments

Goeytex

Senior Member
There is no series resistance between the Picaxe outputs and the VN2222LL Mosfet gates. Unless you need nanosecond switching speeds suggest adding series resistors. 1K should be good.

There is no series resistor from the output of the OPTO to the Picaxe Input. The Internal ESD diodes and the added 1N4148 cannot do much clamping without series some resistance. Suggest 4.7K to 10K. The 5.1V Zeners are not really needed and are not a very good choice for transient noise supression

The schematic does not show a bypass capacitor specific to the Picaxe. Is one in place?

I assume your scope shots are at the inputs of the 14M. Where is your scope ground lead attached? Suggest you read with the scope ground as close to Picaxe Gnd pin as possible. And with the probe directly on the Picaxe Pin being measured. Same noise seen ?

Also what does the scope show you when connected directly across the Picaxe Power Pins ? Or From 9V Vdd To Ground ? Is there switching noise on these as well ?

What is Diode D4 supposed to do? Appears to be a Schottky Rectifier but without much purpose. It will not do much for transient noise. If you want/need to suppress transient noise at that point then consider a series resistor combined with a Transorb/TVS Diode instead.

The 5V Regulator is an LM7805ACZ rated at 100ma. Is 100ma enough for the circuit ?

The unused inputs pins on the Picaxe are left floating. These should be tied to Gnd or to Vcc. Few will actually do this, but it is recommended by Microchip, and when issues like this come up, you know you have followed recommendations.
 
Top