1

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

  • I would do some testing on different pieces of the project. –  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? –  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 –  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. –  May 20 '17 at 21:11
  • Please add a schematic. – uint128_t May 20 '17 at 21:18
  • @uint128_t I have added the schematic as an edit at the bottom. –  May 20 '17 at 21:39
  • Your LED is backwards, for a start. Have you verified with a DMM/scope that the output of the hall-effect sensor is changing state? You will also want to make sure (by reading the source code) that pins are being mapped the way you think they are. – uint128_t May 20 '17 at 21:46
  • The LED is fine in the circuit its just backwards in the diagram. (short pin connected to ground) I've used a multi meter to verify the hall effect sensor voltage drops low when a magnet is brought in the vicinity. As I said above i've tested using a 2 second timer in the loop turning the LED on and off, so there problem is not there. Its within the interrupt but im not sure where exactly. The problem with the ATTINY85 is theres no serial debug :( –  May 20 '17 at 22:02
  • "its just backwards in the diagram" - then draw a proper diagram. Draw a schematic of what you have actually connected up, otherwise it just wastes our time. – Tom Carpenter May 20 '17 at 22:16
  • Pull-up resistor on reset is where? Decoupling capacitors on the supply rails? – Tom Carpenter May 20 '17 at 22:19
  • Are you sure attachInterrupt(0, ... is correct? i.e., that 0 is the right interrupt? Maybe try with digitalPinToInterrupt(pin), as the documentation suggests. – marcelm May 20 '17 at 22:21
  • @marcelm interesting thought. Though the Atmel documentation states that the interrupt can only be on pin 0 –  May 20 '17 at 22:24
  • @Festivejelly the Atmel documentation will state nothing about "pin 0". That is an Arduino-esc abstraction of which there are many different version depending on what core library you use. The Atmel documentation will talk about "INT0" which is the alternative function of one of the pins (for the ATTiny85, this is on PB2). – Tom Carpenter May 20 '17 at 22:26
  • @TomCarpenter I see that makes sense now. I made the erroneous assumption that meant pin0. With this in mind I should be able to fix this... –  May 20 '17 at 22:29
  • Okay so I think my code was okay. Its just for the attiny85 attachinterupt you still feed in 0 as the pin value but the actual pin is pb2 or physical pin 7... phew confusing. It still wasnt working so I added a pullup resistor between the positive hall pin and the digital out of the hall and voila! It now works. Thanks to all you guys pointing out my shoddy understanding of how the pins work. –  May 20 '17 at 22:40

1 Answers1

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.
}
Tom Carpenter
  • 480
  • 2
  • 8
  • Thats interesting, I think i've been getting confused between the pin numbers and the physical pin locations. –  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. –  May 20 '17 at 22:47