Last active
March 23, 2021 18:23
-
-
Save cathalmccabe/cf4aefff2035963004ab1572de9c00cf to your computer and use it in GitHub Desktop.
arduino_io example of toggling GPIO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Example overlay showing how to toggle Arduino pins \n", | |
"\n", | |
"This example uses the PYNQ-Z2 with the `base` overlay, and the `Arduino_IO` class.\n", | |
"\n", | |
"## Import BaseOverlay and Arduino_IO" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from pynq.overlays.base import BaseOverlay\n", | |
"from pynq.lib.arduino import Arduino_IO\n", | |
"\n", | |
"base = BaseOverlay(\"base.bit\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Get handles to Arduino pins\n", | |
"\n", | |
"The Arduino pins are connected to a MicroBlaze microprocessor in the base overlay. `base.ARDUINO` is a handle for this processor and needs to be passed as a parameter to the Arduino_IO class. \n", | |
"Pins D0-D19 on the Ardunio interface can be selected by index (0-19), and the direction of the pin input or output is specified with 'in' or 'out'. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"arduino_pin_d0 = Arduino_IO(base.ARDUINO, 0, 'out')\n", | |
"arduino_pin_d1 = Arduino_IO(base.ARDUINO, 1, 'in')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can now call `.read()` or `.write()` for each pin" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"arduino_pin_d0.write(1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"arduino_pin_d1.read()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Add a toggle button widget " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import ipywidgets as widgets\n", | |
"from IPython.display import display" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Add button widget, and define function to turn pin \"on\"/\"off\" when button is clicked." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Start \"off\":\n", | |
"arduino_pin_d0.write(0) \n", | |
"\n", | |
"# Create the toggle button\n", | |
"toggleButton = widgets.ToggleButtons(\n", | |
" options=['off', 'on']\n", | |
")\n", | |
"\n", | |
"# create function to be called on button change\n", | |
"def toggle_pin(button_value):\n", | |
" if(button_value.new is \"off\"):\n", | |
" arduino_pin_d0.write(0)\n", | |
" else:\n", | |
" arduino_pin_d0.write(1)\n", | |
"\n", | |
" \n", | |
"# Observe the button and call the toggle_pin function when it is clicked. \n", | |
"# Pass the *value* of the button to the function (this will be \"on\" or \"off\")\n", | |
"toggleButton.observe(toggle_pin,'value')\n", | |
"\n", | |
"# Display the button\n", | |
"display(toggleButton)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment