HackSpace_-_October_2019

(Rick Simeone) #1

TUTORIAL


Motorised wing


import time
import board
import pulseio
import touchio
from adafruit_motor import servo

Next, we’ll set up pin A1 as a capacitive touch
input named buttonTouch. This will act as a ‘button’
to control our wing. We’ll also make pin D5 a PWM
output, and set it up as a servo named my_servo.
Finally, we’ll create a variable called wingUp. We’ll
use this variable to track the state of our wing in the
code. The value of this variable will be True when
the wing is up, and False when the wing is down.
We’ll assume that the wing is down when the board
is turned on, so we’ll set the initial value of wingUp
to False. We’ll also create a variable named upAngle
for the open position of the servo. You can tune this
value as needed to make your wing open fully.

# Create a touch capacitive input on Pin A1
buttonTouch = touchio.TouchIn(board.A1)

# Create a PWMOut object on Pin D5
pwm = pulseio.PWMOut(board.D5, duty_cycle=2 **
15, frequency=50)

# Create a servo object, my_servo
my_servo = servo.Servo(pwm)

# Track the state of our wing, false when down,
true when up
wingUp = False

# Position of servo when wing is fully open. Not
to exceed max angle of servo.
upAngle = 60

Follow Figure 1 (previous page) to mock up the
main portion of your circuit. At this point you don’t
need to connect the switch or the LiPoly backpack,
just connect the servo to the ItsyBitsy M0 on pin 5,
and an open wire on pin A1. That should be enough
to program the ItsyBitsy and test the servo.

PROGRAM WITH CIRCUITPYTHON
Connect the ItsyBitsy M0 to your computer with a
micro USB cable. The board will appear as a drive
named CIRCUITPY. Head to hsmag.cc/KNXgNL and
follow the instructions to update your board with the
latest stable release of CircuitPython.
While you’re there, grab the latest library bundle
as well from hsmag.cc/HZqwVA. For this code,
we’ll only need the ‘adafruit_motor’ library. Copy
it from the library bundle into the lib folder on the
CIRCUITPY drive.
CircuitPython can be written and edited in any
text editor, but we highly recommend using a
code editor like Mu Editor. To learn more about
Mu, and for help getting started with CircuitPython,
visit hsmag.cc/jJfvcN.
Once your board is updated, and the motor library
is installed, you’re ready to code! Open your editor
and create a new code.py file on your ItsyBitsy. (If
your ItsyBitsy already has a code.py file, delete it
and start afresh.)
As always, our code starts with importing the
libraries we’ll be using. pulseio allows us to use
PWM (pulse-width modulation) to control our servo,
and touchio gives us access to the capacitive touch
capabilities of the board. The ‘adafruit_motor’
library is a big one, and we’ll only need the servo
portion of it.

Above
A cuttable trace
enables a switch
Above Right
The LiPo battery
connector powers
our project
Free download pdf