Author Topic: Unexpected external interrupt on ATTiny85 while operating relay module  (Read 3951 times)

mehulhirpara

  • Newbie
  • *
  • Posts: 7
Hi All,

I am new to ATTiny85 based development and have been working on a experimental project - turn on light/fan with motion detection using ATTiny85, PIR Sensor and Relay (5V) Module.

I have connected PIR sensor module (OUT pin) to PB2/P2 (external interrupt pin) and Relay Module is connected to PB0/P0.

Code flow:
1. Configure PB0 as output and PB2 for external interrupt (RISING edge)
2. Initialize and configure Timer0 with pre-scaler as 1024. I am using timer0 to turn off relay module.
3. Now, wait for the interruption
4. On interruption, i.e. motion detection, turn on relay module by setting PB0 to LOW and start timer0 inside ISR.
5. Once timer0 is started and after expected time (say 10sec), on ISR (TIMER0_OVF_vect) set PB0 to HIGH to switch off relay.

Now, my observation is after expected time relay gets off and immediately gets on again due to unexpected interrupt received on PB2. There is no motion detection during this time.

This problem is not observed when I replaced relay with an LED(+ 220ohms reg). It seems while operating relay, specifically while switching off, causing instability with board.

ATTiny85 Board: https://ae01.alicdn.com/kf/HTB1yIilHFXXXXbmXpXXq6xXFXXXq/221354416/HTB1yIilHFXXXXbmXpXXq6xXFXXXq.jpg

Relay Module: https://core-electronics.com.au/media/catalog/product/cache/1/image/650x650/fe1bcd18654db18f328c2faaaf3c690a/0/1/018-db-relay.jpg

PIR Sensor: https://images-na.ssl-images-amazon.com/images/I/41fWDsh14LL._SX342_.jpg

Power Supply: USB Charger to ATTiny85 Board. Relay and PIR sensors are connected to Board's VIN and GND.

Please let me know if I am missing anything on connections or any where else.

Thanks,
Mehul



PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Unexpected external interrupt on ATTiny85 while operating relay module
« Reply #1 on: December 27, 2017, 07:49:58 pm »
Everything you've done *sounds* right... even using the rising edge trigger to get around the fact that that sort of motion sensor leaves the output high for a few seconds, depending on what the delay is. Have you tried triggering it just using a wire instead of the motion sensor, just in case you're getting some garbage input from it? Might be worth also putting a LED on the output for the motion sensor so you can see when it goes high/low (if it has enough drive to make the LED light).  Only other think then is to show your code if you can, as it could be something unexpected in your code.

mehulhirpara

  • Newbie
  • *
  • Posts: 7
Re: Unexpected external interrupt on ATTiny85 while operating relay module
« Reply #2 on: December 30, 2017, 04:26:46 am »
@PeterF, Thanks for the reply.

Code mechanism to prevent multiple interrupts is already implemented by setting flag and not triggering timer again if it is already running.

I did some more reading and experiments on this issue and following are the observations:

1. Found a link https://www.raspberrypi.org/forums/viewtopic.php?t=118848 that discuss similar issue and it says software workaround might help in this case i.e. after interruption wait for some time and then check the status on interrupt line. For me, motion sensor set interrupt PIN high for 2.5 seconds. I tried this type of workaround but so far no success.

2. Similar code and circuitry, works well with my other development board - Particle Photon - https://www.particle.io/products/hardware/photon-wifi-dev-kit

3. If I disable interrupt on INT0, just before operating relay, then it works but with that I cannot sense any further interrupt. If I enable interrupt again after operating relay then issue starts again.

@PeterF, as you suggested, I will try putting an LED between PIR and board to see interrupts are due to internal stability or from PIR.

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Unexpected external interrupt on ATTiny85 while operating relay module
« Reply #3 on: December 31, 2017, 02:51:24 am »
No problem, just lucky I wandered past again sooner rather than later ;)

Watch out using RPi material... that is a full-blown SBC computer which has GPIO control, so there will be all sorts of flakey things happening there due to the operating system, multiple programs in the background, drivers, etc, as opposed to a microcontroller which is only needing to do one thing... run your program.

If you think it could be noise, maybe just have a flag that if it is set the ISR won't do anything, and the flag is cleared a second after the relay has switched. But if that was the software fix, it's probably just a workaround for the real fix of using some filter capacitors to get rid of the noise.

If you want a software solution only, this thread might be worth a read (think the most complete code is in the last post)... looks like the OP had problems with excessive noise from the PIR sensor which kept it retriggering.

mehulhirpara

  • Newbie
  • *
  • Posts: 7
Re: Unexpected external interrupt on ATTiny85 while operating relay module
« Reply #4 on: January 05, 2018, 10:37:40 pm »
Problem is solved!!!     :)

Thanks for your time.

I kept an LED between PIR input and ATTiny85 and found that too behaving strange. I observed PIR is getting triggered whenever relay gets operated. Later, I was thinking about why LED functions correctly and relay not? Is ATTiny85 VIN and GND capable of supplying enough current to PIR and Relay board? and then I decided to give a try on separate power supply for relay board rather than drawing from VIN. I powered up relay board with another USB, connected all ground points and it WORKED !!!!

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Unexpected external interrupt on ATTiny85 while operating relay module
« Reply #5 on: January 13, 2018, 04:30:43 pm »
Great to hear it's working for you! I just realised when replying to another comment what the problem is likely to be... the protection diode on the Digispark to protect a voltage connected to the VIN pin feeding back into the USB... it means the VIN pin will be lower than the USB voltage by some 0.6/0.7v, and will also be limited to 500mA, which is probably not enough to latch the relay properly, so there is a good chance the board is losing power when then relay trips (i.e. a brownout), and then start back up again.. hence the intermittent behaviour.

Sqirry

  • Newbie
  • *
  • Posts: 1
Re: Unexpected external interrupt on ATTiny85 while operating relay module
« Reply #6 on: February 23, 2018, 01:13:07 pm »
Hey there, I'm newbe too.
Glad it works for you. Would you like to share your code for me to learn.

I will use EXT INT to get rev Signal from an Engine, convert it and give a analog out to a rev gauge.
Not clear which is the correct use of the ext INT for the digispark. Uno works.

Sqirry

mehulhirpara

  • Newbie
  • *
  • Posts: 7
@Sqirry, are you looking for reference code for external interrupt usage?