GPIO - Use GPIO Interrupt To Control LED
AmebaD [AMB21 / AMB22 / AMB23 / BW16] x 1
LED x 1
Button x 1
In this example, we use a button to trigger interrupt and control the LED. When we press and release the button, the LED dims, press and release the button again, and the LED lights.Note that in the Arduino example “Button and LED”, LED only lights when the button is pressed and hold, when we release the button, the LED dims.
Open the example,
“Files” → “Examples” → “AmebaGPIO” →
AMB21 / AMB22 Wiring Diagram:
AMB23 Wiring Diagram:
BW16 Wiring Diagram:
BW16-TypeC Wiring Diagram:
we set Pin 12 to
, this means that an interrupt occurs when the voltage of this pin changes from GND to 3V3. Therefore, we connect the other side of the button to 3V3, so as to trigger interrupt event when the button is pressed.
On the other hand, we can set pin 12 to
, this means that an interrupt occurs when the voltage of this pin changes from 3V3 to GND. In this case, the other side of the button is connected to GND.Next, we need to specify the funtion to be execute to handle the interrupt:
The second parameter is a function pointer, with prototype:
void button_handler(uint32_t id, uint32_t event)
In this handler, every time we press and release the button, we trigger an interrupt, and change the status of the LED.