Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Gremlins - Relays. AC. CAPS. RB0

Status
Not open for further replies.

RMIM

Member
Hi All,

I was building a simple circuit to get to grips with interrupts. The circuit was a simple 10 minute timer that would keep a relay energised for 10mins if you pushed a switched once, 20mins if you pushed the button twice etc.

Eventually, I got it to simulate well and it worked perfectly on the bread board. (Here it was powered by 5v supplied from my laptop usb).

I then transferred the circuit to the real world:
Some 12v power adaptor to 7805 (2 capacitors thrown in).
Circuit still on bread board.
Relay contacts connect to mains (but no load).
100nf capacitor across the pins of 16f628a

The circuit was not reliable at all. Instead of timing for 10min on one push, it would time for 20min on occasion. I suspected some kind of switch bounce and tried to fix in software. No luck.

I thought it might be the relay drawing too much current (70mA) (relays go through transistor) and the supply not being adequate. I had no large capacitors so thought I was stuck.

I noticed I had some latch relays EA2-5T. I thought these would not draw too much (in fact 25mA) and noticed you only had to pulse it for 2ms, if I read the spec correctly.

Modified code to work with latch relay with 2 coils.
No luck, same problem.

I wired some leds to the circuit to try and see where the program was. I noticed it kept jumping to the interrupt as if someone had pushed the button.

Played with the switch wire (negative one side, RB0 other side) and noticed if I just pulled the wire out and push it back in it would be like pushing the switch.

Tried to throw some 100nf caps I had all over the place, nothing.

Was about to give up, then connected +ve from pic to RB0 with the 100nf cap - to my amazement circuit now works 100%. I pulled the cap out, problem return. Put the cap back in circuit works perfectly. Did this about 10 times as I was so stunned after fighting with circuit for 2days. I then tried the -ve to RB0 with the cap and it also worked perfectly. I have left it like this.


SO

1. I'm hoping someone can explain what is going on as I hope to build many circuits in the future, all of which will use relays and ac on the contact side - I don’t want to lose days fighting to try and get it to work.

2. I have never read to put caps across RB0 and -ve, yet in my case it has fixed a severe problem 100%. But then again I have not done that much reading.

3. Would I be right in saying that latch (EA2-5T) relays are a better than relays as you only have to pulse them, so use less energy? Also I'm not using a transistor, I'm pulsing the latch relay direct from the pics, makes me happy as less components. Safe? (only draws 25mA to 27mA for 3ms)

I'm also not using a diode with the latch relay, is this correct? The coil has a certain polarity you have to pulse it with. It has 2 separate coils. I did not see anything in the spec sheet to say you have to use diode (flyback diode). Safe?

I have been running the circuit for a while like this - everything seems 100% thanks to the cap across RB0 and -ve


Thanks.

edit: pullups are on.
 
Last edited:
The cap is acting as a weak pull down that puts the pin in a default state but you don't normally use caps for this. We use either a pull up resistor (between pin and Vdd) or a pull down resistor (between pin and Vss). If you don't have the internal weak pull ups enabled you will need either an external pull up or pull down resistor. This places the pin in a default state of either 1 (pull up) or 0 (pull down).

Without the pull up or pull down, the pin is in an undefined state (i.e. "floating") until the switch closes when using a normally open switch. If using a normally closed switch with no pull up or pull down resistor, the pin will float when the switch opens.

You will then need to modify the INTEDG bit in the OPTION_REG to tell it whether to interrupt on the rising edge of RB0 or interrupt on the falling edge of RB0. This bit is defaulted to 1/interrupt on rising edge of RB0. If using a -

Pull Up Resistor From Vdd to RB0 w/normally open momentary switch between RB0 and Vss - INTEDG = 0/interrupt on falling edge
Pull Up Resistor From Vdd to RB0 w/normally closed momentary switch between RB0 and Vss - INTEDG = 1/interrupt on rising edge
Pull Down Resistor from RB0 to Vss w/normally open switch between RB0 and Vdd - INTEDG = 1/interrupt on rising edge
Pull Down Resistor from RB0 to Vss w/normally closed switch between RB0 and Vdd - INTED = 0/interrupt on falling edge

Make sense?
 
Last edited:
Make sense?

thanks for the reply

my pullup was on. it still was not working. i even disabled the pullup and used an external 10k resistor. Still no good. But the cap fixed problem. My software pullup is on, if a take cap away problem returns.
 
Can you post up your full source code?
 
Last edited:
Can you post up your full source code?

Code:
LIST	p=16F628a		;tell assembler what chip we are using
	include "P16F628a.inc"		;include the defaults for the chip
;	__CONFIG   _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT
	

	cblock 20h
	Ncoin
	d1
	d2
	d3
	d4
	b1
	b2
	b3
	f1
	f2
	f3
	t1
	t2
	t3
	t4
	one1
	one2
	one3
	h1
	h2
	h3
	ms1
	ms2
	endc


	org 000h
	goto SETUP

	org 004h
	goto INTERRUPT


SETUP:
	;;;;;;;;;;;;;;BANK 1 BELOW;;;;;;;;;;;;;;

	BSF STATUS,5 ;BANK 1

	BcF OPTION_REG,7 ;RBPU: PORT B PULL UPS 1=OFF 0=ON
	BCF OPTION_REG,6 ;INTEDG: INTERRUPT EDGE
	BCF OPTION_REG,5 ;T0CS: BIT5 CLOCK SOURCE, EXTERNAL = 1  INTERNAL CYCLE = 0
	BCF OPTION_REG,4 ;T0SE: EDGE TRIGGER FOR EXTERNAL TRIGGER
	BCF OPTION_REG,3 ;PSA: ASSIGNS PRESCALER TO Timer0=0 watch dog=1
	BcF OPTION_REG,2 ;PS2:
	BcF OPTION_REG,1 ;PS1:
	BcF OPTION_REG,0 ;PS0: pre-scaler


	movlw 	b'00000001'		
   	movwf 	TRISB			;set PortB INPUTS/OUTPUTS
	movlw	b'11111111'
	movwf 	TRISA			;set PortA INPUTS/OUTPUTS

	;;;;;;;;;;;;;;BANK 0 BELOW;;;;;;;;;;;;;;

	BCF STATUS,RP0 ;BANK 0

	BSF INTCON,GIE 	;Enable general interrupts GIE=bit 7
	BCF INTCON,PEIE 	;bit6 PERIPHERAL INTERRUPTS
	BCF INTCON,T0IE	;bit5 TMR0 OVERFLOW INTERRUPT 1=ENABLE
	BSF INTCON,INTE	;bit4 EXTERNAL INTERRUPT RB0 1=ON 0=OFF
	BCF INTCON,RBIE 	;bit3 PORT CHANGE RB INTERRUPT 0=OFF
   	BSF INTCON,T0IF 	;bit2 TMR0 OVERFLOW INTERRUPT FLAG, MUST BE CLEARED IN SOFTWARE
	BCF INTCON,INTF 	;bit1 EXTERNAL INTERRUPT FLAG for RB0, must clear in software*** before *RETFIE*
	BSF INTCON,0		;bit0 RB <7:4> pin have changed state = 1 (must be cleared in software)

	
	CLRF PORTA
	CLRF PORTB
	CLRF Ncoin 
	
	BcF PORTB,1 ;flashes led so i know pic is starting up
	call onesec
	BsF PORTB,1
	call onesec
	BcF PORTB,1
	call onesec
	BsF PORTB,1
	call onesec
	BcF PORTB,1
	call onesec
	BsF PORTB,1
	call onesec
	BcF PORTB,1

	BsF PORTB,2
	call onesec
	BcF PORTB,2
	call onesec
	BsF PORTB,3
	call onesec
	BcF PORTB,3
	


	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;





MAIN:

	BTFSS Ncoin,0
	goto $+2
	goto Decrease 
	
	BTFSS Ncoin,1
	goto $+2
	goto Decrease
	
	BTFSS Ncoin,2
	goto $+2
	goto Decrease 
	
	BTFSS Ncoin,3
	goto $+2
	goto Decrease 
	
	BTFSS Ncoin,4
	goto $+2
	goto Decrease 
	
	BTFSS Ncoin,5
	goto $+2
	goto Decrease 
	
	BTFSS Ncoin,6
	goto $+2
	goto Decrease 
	
	BTFSS Ncoin,7
	goto $+2
	goto Decrease 


	goto MAIN


 


INTERRUPT:

	
	BSF PORTB,1 ;flashes led so i know it's in interrupt
	
	INCF Ncoin
	

	movlw	0x08 ;pause to debounce switch
	movwf	b1
	movlw	0x2F
	movwf	b2
	movlw	0x03
	movwf	b3
bounce_0
	decfsz	b1, f
	goto	$+2
	decfsz	b2, f
	goto	$+2
	decfsz	b3, f
	goto	bounce_0

			;3 cycles
	goto	$+1
	nop

	
	BCF PORTB,1
	
	BCF INTCON,INTF
	RETFIE




ON:
	BsF PORTB,2 ;latch relay on
	call ms
	BcF PORTB,2

	RETURN

OFF:
	BsF PORTB,3 ;latch relay off
	call ms
	BcF PORTB,3

	RETURN

	







	
Decrease:	

	DECF Ncoin

	call ON

	call fiveSec
	;call tenmin

	call OFF	

	call onesec

	goto MAIN



fiveSec:

			;4999993 cycles
	movlw	0x2C
	movwf	f1
	movlw	0xE7
	movwf	f2
	movlw	0x0B
	movwf	f3
five_0
	decfsz	f1, f
	goto	$+2
	decfsz	f2, f
	goto	$+2
	decfsz	f3, f
	goto	five_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

tenmin:

	movlw	0xA9
	movwf	t1
	movlw	0x41
	movwf	t2
	movlw	0xFA
	movwf	t3
	movlw	0x04
	movwf	t4
ten_0
	decfsz	t1, f
	goto	$+2
	decfsz	t2, f
	goto	$+2
	decfsz	t3, f
	goto	$+2
	decfsz	t4, f
	goto	ten_0

			;8 cycles
	goto	$+1
	goto	$+1
	goto	$+1
	goto	$+1

			;4 cycles (including call)
	return



onesec:
			;999990 cycles
	movlw	0x07
	movwf	one1
	movlw	0x2F
	movwf	one2
	movlw	0x03
	movwf	one3
onesec_0
	decfsz	one1, f
	goto	$+2
	decfsz	one2, f
	goto	$+2
	decfsz	one3, f
	goto	onesec_0

			;6 cycles
	goto	$+1
	goto	$+1
	goto	$+1

			;4 cycles (including call)
	return




halfsec:
			;499994 cycles
	movlw	0x03
	movwf	h1
	movlw	0x18
	movwf	h2
	movlw	0x02
	movwf	h3
halfsec_0
	decfsz	h1, f
	goto	$+2
	decfsz	h2, f
	goto	$+2
	decfsz	h3, f
	goto	halfsec_0

			;2 cycles
	goto	$+1

			;4 cycles (including call)
	return

ms:
			;2993 cycles
	movlw	0x56
	movwf	ms1
	movlw	0x03
	movwf	ms2
ms_0
	decfsz	ms1, f
	goto	$+2
	decfsz	ms2, f
	goto	ms_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return


	end
 
Last edited:
the problem only happens on the other circuit which has ac on the relay contacts, otherwise it works perfectly. but like i said, as soon as you add cap to RB0 and -ve, everthing works.
 
Last edited:
if anyone could give me some advice on those latch relays, as I like them a lot and plan to buy a batch load. do i need to use diodes with them? etc. how many ms I have to pulse them for? I'm using them with mains ac (220v, uk mains) and light loads (ac solenoid, thermostats which all must be under 500mA (ac) I'm guessing)

thanks
 
Last edited:
Hi,

Think your choice of latching relays is being born out of initial poor design.

Once you have proven your software and refined your hardware I cannot see any real point in having latching relays, which cost a lot more over standard types and the choice of physical size and contact currents is quite limited.
Its very rare to see any web based hobby micro projects using them.

Typical ciruits use 5v for logic and 12v for the relays etc, the interface being something like an octal relay driver - UNL2803A which contains the necessary diodes.

Be aware that those light loads you mention , when switching on or off, can cause problems with the micro resetting.

Attention the good pcb design and keeping any long interconnects shielded, paricularly switch inputs, are often needed.

Not trying to put you off in any way, just that the above highlights many of the pitfalls that can sometimes cause headaches when using main equipment with micros.

btw - what is your project for ?
 
btw - what is your project for ?

Hi Wp100,

The above circuit is a temporary fix before I attempt to make the real project, which I have started a thread on entitled 'coin timer pulses pics'. It will take me some time to learn all the elements to get the project going. (pulse input from coin validator, pulse input from gas/elec meter, 3 separate timers).

It's a project I hope to make for secondary billing.

At the moment I have some purpose bought timers (mechanical) that have started to fail and are limited in many ways. In total I have 2 timers and 1 coin electric meter.

I plan on having 3timers (washing machine, shower, central heating) and 2 secondary meters (just readers, no coin mechanism) (gas and electric using purpose bought units that have pulse output). I plan to marry all these timers and meters into one unit with coin validator so I have only one central unit.


The above ten minute timers was because my mechanical 10 minute timer had failed, not even WD40 got it going again - so it was free showers for everyone - gas bill through the roof.

The ten minute timer works a solenoid on the hot water supply. I have installed the project and it seems to be working.


--------------

You say that latch relays are not really used. I don’t mind they cost a bit more. I thought they were great as they let the power supply rest: you pulse on, you pulse off. Whereas if I had to have a relay energised for 6 hours - that was my thinking (gives my 7805 a rest). 70mA for 6hrs Verses pulse for 3ms then 3ms to turn off at 27mA.

I was afraid someone would say they might cause my circuit to rest - so far they seem ok. How can they do that?

Do I need flyback diodes for these latch relays?

These relays will not be used in final project. Perhaps solid state relays will be used.

But above all - the project is just for me to learn electronics
 
Hi,

Interesting project - close to my heart actually, a coin controlled timer for a sports hall was my first completed project as a bench technician but using standard ttl chips rather than a micro which was too early/expensive then.

Regarding the latching relay - they have a standard coil/s like any relay so must have the diode fitted.
Running a relay direct from the micro is testing its port to the near limit.
A driver chip as mentioned is the simple answer when using several relays or a simple res & transistor for just the odd relay.

Having a relay powered on the same 5v power line is not a good idea for several reasons, so suggest you use 12v relays of either normal or latching types.
SSRs can of course run direct from the pics port, but again even more expensive.

Just quickly looking at your other post, everything do-able but would forget about using timers like that.
Create a RTC either using the pic chips TM1 with a 32k xtal or an external RTC like the DS1307 and simply count /compare off that.

Would think you would need a lcd so you can monitor and control things, so doubt the 18 pin 628 would be enough, perhaps the 876A or 877A would be better.
A 18F2520 or 4520 even better for Assemblers
 
Regarding the latching relay - they have a standard coil/s like any relay so must have the diode fitted.

The reason why I thought I might not need them as the coil input has a particular polarity - it not like other relays where it not marked where your positive or negative go. I better run and go fix that.

Why does the coil have a particular polarity? I thought it must include a diode. I can't see anything on the spec sheet.

https://www.electro-tech-online.com/custompdfs/2011/11/ea2.pdf

Yes I had planned to include some LCD's/LED displays.

Thanks

edit: can I view this voltage spike the coil produces with an oscilloscope?
 
Last edited:
Your code could use LOTS of stream lining. Also, you don't have an interrupt context save/restore routine in your interrupt handler.

Another issue is your relative jumps. You didn't set a default radix in the list directive so the assembler automatically will default all values without a specified radix to hex. For any goto instructions that use a relative jump (i.e. a "current line plus or minus a value") that jump more than 10 lines, the value will actually be in hex and not decimal.

I'm working on rewriting your code to make it much more compact plus including the context save/restore routine. For your delay routines, you only need 2...one fixed and one variable. I'll be including those as well. So give me a little bit and I'll have some new source code for ya.
 
Last edited:
Your code could use LOTS of stream lining.

Thanks for the reply.
Yes please point me in the right direction. I have just started out in the pic world. I was hoping someone would show me the proper way of doing things.

I have also read from the spec sheet my latch relays need a minimum of 10ms not the 2ms I thought.
 
Last edited:
Try this code -

Code:
		LIST		p=16F628A, r=dec, w=-302		;tell assembler what chip we are using
		include 	<P16F628A.INC>				;include the defaults for the chip
		__CONFIG   	_LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT
 	
 
		cblock  	20h
				Ncoin
				d1
				d2
				d3
				d4
				b1
				b2
				b3
				f1
				f2
				f3
				t1
				t2
				t3
				t4
				one1
				one2
				one3
				h1
				h2
				h3
				ms1
				ms2
				COUNT
		endc
 
		cblock		0x70
				W_TEMP
				STATUS_TEMP
				PCLATH_TEMP
		endc

 
		org 		000h
		goto 		SETUP
 
		org 		004h
		goto 		INTERRUPT
 
 
SETUP
		;;;;;;;;;;;;;;BANK 1 BELOW;;;;;;;;;;;;;;
 
		BSF 		STATUS,5 		;BANK 1
 
		clrf		OPTION_REG
 
 		movlw 		b'00000001'		
   		movwf 		TRISB			;set PortB INPUTS/OUTPUTS
	 
	;;;;;;;;;;;;;;BANK 0 BELOW;;;;;;;;;;;;;;
 
		BCF 		STATUS,RP0 		;BANK 0
 
		movlw		b'10001001'		;enable RB0 interrupt and interrupt
		movwf		INTCON			;on PORTB 4:7 change
 
 		CLRF 		PORTA
		CLRF 		PORTB
		CLRF 		Ncoin 

		movlw		6			;flash led 3 times on startup
		movwf		COUNT
		movlw		b'00000010'
		xorwf		PORTB,F
		movlw		5			;set delay for one second
		call		Delay
		decfsz		COUNT,F
		goto		$-5
 
		movlw		4			;pulse RB2 twice
		movwf		COUNT
		movlw		b'00000100'
		xorwf		PORTB,F
		movlw		5
		call		Delay			;set delay for one second
		decfsz		COUNT,F
		goto		$-5
 
	 
	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 
MAIN
 
		movlw		0			;if Ncoin > 0, branch to Decrease Ncoin
		xorwf		Ncoin,W
		btfsc		STATUS,Z
		call		Decrease 
 
		goto 		MAIN			;else check again
 
 
INTERRUPT

;software stack for context save
 
		movwf		W_TEMP			;push W
		swapf		STATUS,W		;push STATUS
		banksel		0			;bank 0
		movwf		STATUS_TEMP
		movfw		PCLATH			;push PCLATH
		movwf		PCLATH_TEMP

		BSF 		PORTB,1 		;flashes led so i know it's in interrupt
 
		INCF 		Ncoin,F			;increment Ncoin
 
		call		Delay200mS
  
 		BCF 		PORTB,1
 
		btfss		PORTB,RB0		;has switch been released?
		goto		$-1			;no, check again
		call		Delay200mS		;yes, debounce

		bcf		INTCON,INTF		;clear RB0 interrupt flag

;context restore from software stack

		movfw		PCLATH_TEMP
		movwf		PCLATH
		swapf		STATUS_TEMP,W
		movwf		STATUS
		swapf		W_TEMP,F
		swapf		W_TEMP,W
		RETFIE
 
 
 
 
ON
		BsF 		PORTB,2 		;latch relay on
		call 		Delay200mS
		BcF 		PORTB,2
 
		RETURN
 
OFF
		BsF 		PORTB,3 		;latch relay off
		call 		Delay200mS
		BcF 		PORTB,3
 
		RETURN
 
Decrease	
 
		DECF 		Ncoin,F
 
		call 		ON
 
		movlw		25			;set delay for 5 sec
		call		Delay
	 
		call 		OFF	
 
		movlw		5			;set delay for 1 sec
		call		Delay
 
		return
 
;master delay loops
 
Delay200mS	movlw		0xFF
		movwf		b1
		movwf		b2
	
		decfsz		b1,F
		goto		$-1
		decfsz		b2,F
		goto		$-3
		return

Delay		movwf		b3
		call		Delay200mS
		decfsz		b3,F
		goto		$-2
		return

 
OneMin		movlw		120			;init TMR1 interrupt counter
		movwf		b1		
		movlw		b'00110001'		;TMR1 on, 1:8 prescale
		movwf		T1CON			;one increment = 8 cycles
		clrf		TMR1H			;init TMR1 high byte
		clrf		TMR1L			;init TMR1 low byte
		bcf		PIR1,TMR1IF		;clear TMR1 interrupt flag
		btfss		PIR1,TMR1IF		;TMR1 interrupt flag high?
		goto		$-1			;no, check again
		decfsz		b1,F			;decrement interrupt counter
		goto		$-6			;do again if interrupt counter > 0
		return					;1 minute lapsed. done

TenMin		movlw		10			;init one minute counter
		movwf		b2
		call		OneMin			;run one minute delay
		decfsz		b2,F			;decrement counter
		goto		$-2			;run 10 times
		return					;10 minutes passed
		

		end
 
Last edited:
Did the code work?

well I have not worked trough it yet.
but i did just copy paste build and flash - no.

RB1 keeps flashing.

button does nothing.

relay does nothing

so something is up somewhere. (reflashed with my code, and circuit works. so it not the circuit).
 
Last edited:
OK...you must've copied the code before I had a chance to correct it (I caught and corrected a mistake in the code a few minutes after I posted it). On the flash loop I originally had "goto $-6", which would've gotten it stuck in the flash loop and is what your version of my code is doing (which explains why it just repeatedly flashes and nothing else works). I've since corrected it to "goto $-5". So re-copy/paste the current code and it should work.
 
Last edited:
Why does the coil have a particular polarity?
Latching relays come in two flavours...single or double coil. The double coil type is 'set' by energising one coil and 'reset' by energising the other. The single coil type is 'set' by energising its coil with one polarity and is 'reset' by energising it with the reverse polarity; hence it needs a polarity marking.
 
Status
Not open for further replies.

Latest threads

Back
Top