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:
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
1 Answer 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.
}
-
Thats interesting, I think i've been getting confused between the pin numbers and the physical pin locations.Festivejelly– Festivejelly2017年05月20日 22:23:27 +00:00Commented May 20, 2017 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– Festivejelly2017年05月20日 22:47:58 +00:00Commented May 20, 2017 at 22:47
attachInterrupt()
?