Setting specific Arduino Pins to High/Low

Hi,

Does anybody have any example on how to set individual Arduino pins to High or Low? The examples that are offered in the ‘base’ folders all require a grove shield which I do not own at the moment.

Currently, I am trying to use a custom IP overlay, and output the output values of the custom IP to try to read it on an Oscilloscope, with the help of a DAC. If anyone has ideas, tips, or examples, it would be much appreciated.

1 Like

Hi,
You can use the Arduino_IO class for this.

See this notebook, or Markdown below.

https://gist.github.com/cathalmccabe/cf4aefff2035963004ab1572de9c00cf

Import BaseOverlay and Arduino_IO

from pynq.overlays.base import BaseOverlay
from pynq.lib.arduino import Arduino_IO

base = BaseOverlay("base.bit")

Get handles to Arduino pins

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.
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’.

arduino_pin_d0 = Arduino_IO(base.ARDUINO, 0, 'out')
arduino_pin_d1 = Arduino_IO(base.ARDUINO, 1, 'in')

We can now call .read() or .write() for each pin

arduino_pin_d0.write(1)
arduino_pin_d1.read()

Add a toggle button widget

import ipywidgets as widgets
from IPython.display import display

Add button widget, and define function to turn pin “on”/“off” when button is clicked.

# Start "off":
arduino_pin_d0.write(0)      

# Create the toggle button
toggleButton = widgets.ToggleButtons(
    options=['off', 'on']
)

# create function to be called on button change
def toggle_pin(button_value):
    if(button_value.new is "off"):
        arduino_pin_d0.write(0)
    else:
        arduino_pin_d0.write(1)

 
# Observe the button and call the toggle_pin function when it is clicked. 
# Pass the *value* of the button to the function (this will be "on" or "off")
toggleButton.observe(toggle_pin,'value')

# Display the button
display(toggleButton)