0

I have an ATTiny85 set up to detect a magnet passing over a hall sensor. Every 2 detection's I want it to light up an LED.

I decided to do the code using the arduino IDE and the code is as follows:

const int hallPin = 2;     // the number of the hall effect pin
const int ledPin =  1;      // the number of the LED pin

// variables will change:
volatile int hallState = 0;         // variable for storing the hall counter

void setup() {

  pinMode(ledPin, OUTPUT);
  pinMode(hallPin, INPUT);
  digitalWrite(hallPin, HIGH);

  // Attach an interrupt to the ISR vector
  attachInterrupt(0, pin_ISR, FALLING);
}

void loop() 
{
  if(hallState>1)
  {
      digitalWrite(ledPin,HIGH);
      delay(2000);
      digitalWrite(ledPin,LOW);
      delay(2000);
      hallState=0;
  }
}

void pin_ISR() 
{
    hallState++;
}

I've dry run this a bunch of times and cant see anything wrong with it. I use the digitalWrite on the hall pin to enable the internal pullup. But the circuit just doesnt work.

Here is the circuit:

Diagram of circuit

I connected a multi meter to the hall effect sensor on its own just to check if it was working. I added an external pull up using a 10k resistor and it worked as expected 5v when no magnet present and almost 0 volts when a magnet is brought in range.

So my guess is my interrupt routine isnt working in my code, what could I be doing wrong? I've read in the documentation that pin 0 is the correct interrupt pin on the attiny85

Edit: Adding Schematic

enter image description here

16
  • I would do some testing on different pieces of the project. – Entrepreneur May 20 '17 at 19:49
  • I would do some testing on different pieces of the project. It looks like your LED might be connected backward. I don't use that part so I wonder is "loop()" really the normal "main()" entry point? I would try the code in loop() by replacing "if(hallState>1)" with "if(1)" just to make sure the LED will actually blink. Do you need to enable interrupts or is that implied somehow? – Entrepreneur May 20 '17 at 19:56
  • Hi, thanks for the comment. I actually already tested the LED works by commenting out the IF statement and replacing the loops contents to turn the LED on for 2 seconds then off for 2 seconds. this worked fine and the LED did exactly that. This leads me to believe its the interrupt code thats a bit flaky – Festivejelly May 20 '17 at 20:26
  • Have you tried writing it as a normal ISR and enabling the interrupt yourself instead of using attachInterrupt()? – Ignacio Vazquez-Abrams May 20 '17 at 20:48
  • @IgnacioVazquez-Abrams Not yet, I shall read up on how to do that and see if I can get that to work. Thanks for the suggestion. – Festivejelly May 20 '17 at 21:11
1

You main problem is that you have wired up your MCU wrong.

Pin 5 (PB0) is not the external interrupt pin. Pin 7 (PB2) is the external interrupt 0 (INT0) pin.

As a result your code will not do anything when the hall sensor changes because it is not looking at it.


I would suggest not trying to use the attachInterrupt() abstraction. Instead I would simply enable the external interrupt directly:

GIMSK |= _BV(INT0);

And use a proper ISR handler:

ISR(INT0_vect) {
    //Your ISR code here.
}
2
  • Thats interesting, I think i've been getting confused between the pin numbers and the physical pin locations. – Festivejelly May 20 '17 at 22:23
  • You were of course correct. Thanks for clearing that up. It makes a lot more sense now. I've plugged it into pin 5 because I read somewhere attachInterrupt had to be set to 0 so I figured the wire had to go to physical pin 5 (pb0). This is apparently not the case. I guess that function simply enabled the interrupt with the default value. – Festivejelly May 20 '17 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy