Check out the new MinnowBoard.org website for the most up-to-date information on the MinnowBoard Turbot and the MinnowBoard.org Community.

Projects/Maker Interrupt

From MinnowBoard Wiki
Jump to: navigation, search

Input and Interrupts

The GPIO pins on the MinnowBoard MAX can be configured to receive data, such as a button press. Additionally, we can use these inputs to trigger an interrupt in a program, activating an Interrupt Service Routine (ISR). To construct and operate a simple circuit that demonstrates this capability, gather the following:

Required:

  • 1 push button
  • 5 Male-Female wires
  • 4 Male-Male wires
  • 3 LEDs (varying colors are nice, but not necessary)
  • MinnowBoard MAX with access (directly, or through serial or SSH), Python and mraa
  • Breadboard

Optional:

  • 2 220Ω resistors


Hardware

Buttons

A button provides you with a physical interface to connect two points in a circuit. The button itself has a very simple operation, as demonstrated by the following schematic of the button and one of the LEDs: Switchwork.png

Pressing a button completes the circuit and lights up the LED. In this example circuit, we'll also complete a circuit with one of the GPIO pins to trigger an interrupt. We will use a total of 3 LEDs, two of which will be controlled by the interrupt, and one that will be following a standard blinking loop.

Base buttons.png

Pull up resistors

GPIO pin 26 on the MinnowBoard will be controlling the interrupt signal. GPIO pins 25 and 23 will be the standard LEDS, with pin 25 receiving special instructions for the interrupt. If you have wired it up correctly, but the lights are dim, you might want to add a pull-up resistor to your circuit. This is done by wiring a 220Ω resistor to each LED's input pin to the 3.3V power line, as seen here: Resist buttons.png

Software

Assuming you've configured the OS on your MinnowBoard MAX with Python and the mraa GPIO library, operation of this circuit is very simple. Run the test_isr.py script with python test_isr.py. At this point, if you don't push the button, the LED attached to GPIO pin 23 will simply blink on and off. The button will light up the LED where connected, and change the state of the LED connected to GPIO pin 25.

If no lights come on, double check your wiring.

Code

The code itself is unorthodox, but not too complex.

# Import mraa and time for GPIO access and use of the sleep function
import mraa                      
import time                      

# Calling an interrupt requires that we specify a function to run when the
# interrupt is triggered. This class simply holds our GPIO that flips upon
# interrupt. The LED is held in the led object, and the current state
# of the LED is held in status                       
class Counter:                   
  status = False                 
  led = mraa.Gpio(25)            
  led.dir(mraa.DIR_OUT)          
                                 
# Create an instance of the class
c = Counter()                    

# This is the function that will be called when the interrupt happens. It will 
# flip the status as it is held by the class and apply it to the GPIO pin.                   
def test(args):    
  c.status = not c.status
  c.led.write(c.status)  

# Initialize the pin to be used as input                       
x =mraa.Gpio(26)
x.dir(mraa.DIR_IN)

# This tells the pin to watch for interrupts. mraa.EDGE_BOTH is what we're watching
# for; we can specify looking for the pin to go HIGH or LOW, but here we don't care
# as long as it changes. The second argument is the function to call, and the third can
# hold arguments that can be passed into the function (you can pass more than one)
x.isr(mraa.EDGE_BOTH, test, test)
                                 
# When the default pin, is specified, it runs a normally blinking pattern
y = mraa.Gpio(23)
y.dir(mraa.DIR_OUT)
                   
# This runs the rest of the program, but it can be interrupted 
# as you can see by triggering the interrupt pin
while True:
  y.write(1)                   
  time.sleep(1)                  
  y.write(0)                     
  time.sleep(1)